Name

calc — evaluate the enclosed arithmetic expression or Perl block

ATTRIBUTES

Attribute Pos. Req. Default Description
interpolate     1 interpolate input?
reparse     1 interpolate output?

DESCRIPTION

The tag evaluates the enclosed arithmetic expression or a Perl block. The last expression evaluated (return value) is returned to the client page.

Note that Perl blocks can be of arbitrary content and complexity, and there really are no typical examples to show.

BEHAVIOR

This tag does not appear to be affected by, or affect, the rest of Interchange.

EXAMPLES

Example: Simple Perl code, a random arithmetic expression

Current magic number is: [calc]2+rand[/calc]

Example: Retrieving an Interchange session value

Welcome, your user name is [calc]$Tag->data(qw/session username/)[/calc]

Example: Setting and displaying a value

Order number is:
  [calc] $Session->{mv_order_number} = $Values->{mv_order_number} [/calc]

Example: Clearing the return value

You can clear the return value (that is, return nothing) by simply calling return with no arguments:

[calc] my $a = 5; return [/calc]

NOTES

The [calc] tag is lower-overhead variant of [perl], because it does not accept arguments, does not try to interpolate tag body (well, [calcn] tag only) , does not pre-open any database tables, and it doesn't do any extra wrapping.

The [calc] tag will remember variable values inside the page, so you can do the equivalent of a memory store and memory recall for a loop. In other words, variables you initialize or set in one [calc] block are also visible in all further [calc] blocks on the same page.

There is no reason to ever use this tag inside [perl] or [mvasp].

[calc] and [perl] are the two tags that play major role in any Perl programming within Interchange.

AVAILABILITY

calc is available in Interchange versions:

4.6.0-5.9.0 (git-head)

SOURCE

Interchange 5.9.0:

Source: code/SystemTag/calc.coretag
Lines: 13


# Copyright 2002-2007 Interchange Development Group and others
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.  See the LICENSE file for details.
# 
# $Id: calc.coretag,v 1.4 2007-03-30 23:40:49 pajamian Exp $

UserTag calc                hasEndTag
UserTag calc                Interpolate
UserTag calc                Version      $Revision: 1.4 $
UserTag calc                MapRoutine   Vend::Interpolate::tag_calc

Source: lib/Vend/Interpolate.pm
Lines: 2823

sub tag_calc {
my($body) = @_;
my $result;
if($Vend::NoInterpolate) {
  logGlobal({ level => 'alert' },
        "Attempt to interpolate perl/ITL from RPC, no permissions."
        );
}

$Items = $Vend::Items;

if($MVSAFE::Safe) {
  $result = eval($body);
}
else {
  init_calc() if ! $Vend::Calc_initialized;
  $result = $ready_safe->reval($body);
}

if ($@) {
  my $msg = $@;
  $Vend::Session->{try}{$Vend::Try} = $msg if $Vend::Try;
  logGlobal({ level => 'debug' }, "Safe: %s\n%s\n" , $msg, $body);
  logError("Safe: %s\n%s\n" , $msg, $body);
  return $MVSAFE::Safe ? '' : 0;
}
return $result;
}

Source: lib/Vend/Interpolate.pm
Lines: 2823

sub tag_calc {
my($body) = @_;
my $result;
if($Vend::NoInterpolate) {
  logGlobal({ level => 'alert' },
        "Attempt to interpolate perl/ITL from RPC, no permissions."
        );
}

$Items = $Vend::Items;

if($MVSAFE::Safe) {
  $result = eval($body);
}
else {
  init_calc() if ! $Vend::Calc_initialized;
  $result = $ready_safe->reval($body);
}

if ($@) {
  my $msg = $@;
  $Vend::Session->{try}{$Vend::Try} = $msg if $Vend::Try;
  logGlobal({ level => 'debug' }, "Safe: %s\n%s\n" , $msg, $body);
  logError("Safe: %s\n%s\n" , $msg, $body);
  return $MVSAFE::Safe ? '' : 0;
}
return $result;
}

AUTHORS

Interchange Development Group

SEE ALSO

perl(7ic), mvasp(7ic), calcn(7ic)

DocBook! Interchange!