CodeGeneration - Maple Programming Help

# Online Help

###### All Products    Maple    MapleSim

Home : Support : Online Help : Programming : Input and Output : Translation : CodeGeneration/Perl

CodeGeneration

 Perl
 translate Maple code to Perl code

 Calling Sequence Perl(x, cgopts)

Parameters

 x - expression, list, rtable, procedure, or module cgopts - (optional) one or more CodeGeneration options

Description

 • The Perl(x, cgopts) calling sequence translates Maple code to Perl code.
 - If the parameter x is an algebraic expression, then a Perl statement assigning the expression to a variable is generated.
 - If x is a list, Maple Array, or rtable of algebraic expressions, then a sequence of Perl statements assigning the elements to a Perl array is produced.  Only the initialized elements of the rtable or Maple Array are translated.
 - If x is a list of equations $\mathrm{nm}=\mathrm{expr}$, where $\mathrm{nm}$ is a name and $\mathrm{expr}$ is an algebraic expression, then this is understood as a sequence of assignment statements.  In this case, the equivalent sequence of Perl assignment statements is generated.
 - If x is a procedure, then a Perl class is generated containing a function equivalent to the procedure, along with any necessary import statements.
 - If x is a module, then a Perl class is generated, as described on the PerlDetails help page.
 • The parameter cgopts may include one or more CodeGeneration options, as described in CodeGenerationOptions.
 • For more information about how the CodeGeneration package translates Maple code to other languages, see Translation Details. For more information about translation to Perl in particular, see PerlDetails.

Examples

For a description of the options used in the following examples, see CodeGenerationOptions.

 > $\mathrm{with}\left(\mathrm{CodeGeneration}\right):$

Translate a simple expression and assign it to the name $w$ in the target code.

 > $\mathrm{Perl}\left(x+yz-2xz,\mathrm{resultname}="w"\right)$
 $w = -2 *$x * $z +$y * $z +$x;

Translate a list and assign it to a Perl reflist with the name $w$ in the target code.

 > $\mathrm{Perl}\left(\left[\left[x,2y\right],\left[5,z\right]\right],\mathrm{resultname}="w"\right)$
 $w = [[$x,2 * $y],[5,$z]];

Translate a computation sequence.  Optimize the input first.

 > $\mathrm{cs}≔\left[s=1.0+x,t=\mathrm{ln}\left(s\right)\mathrm{exp}\left(-x\right),r=\mathrm{exp}\left(-x\right)+xt\right]:$
 > $\mathrm{Perl}\left(\mathrm{cs},\mathrm{optimize}\right)$
 $s = 0.10e1 +$x; $t1 = log($s); $t2 = exp(-$x); $t =$t2 * $t1;$r = $x *$t + $t2; Translate a procedure.  > f := proc(x, y, z) return x*y-y*z+x*z; end proc:  > $\mathrm{Perl}\left(f\right)$  #!/usr/bin/perl sub f { local($x, $y,$z) = @_;   return($y *$x - $y *$z + $x *$z); }

Translate a procedure containing an implicit return.  A new variable is created to hold the return value.

 > f := proc(n)   local x, i;   x := 0.0;   for i to n do     x := x + i;   end do; end proc:
 > $\mathrm{Perl}\left(f\right)$
 #!/usr/bin/perl sub f {   local($n) = @_; local($x, $i,$cgret);   $x = 0.0e0; for ($i = 1; $i <=$n; $i++) {$x = $x +$i;     $cgret =$x;   }   return($cgret); } Translate a procedure iterating over a list and concatenating a string at each step.  > f := proc(L::list) local x, s := ""; for x in L do s := cat(s, x); end do; end proc:  > $\mathrm{Perl}\left(f\right)$  #!/usr/bin/perl sub f { local($L) = @_;   local($x,$s, $cgret);$s = "";   foreach $x (@{$L})   {     $s =$s . $x;$cgret = $s; } return($cgret); }

Translate a module.

 > m := module() export p; local q;     p := proc(x,y) if y>0 then trunc(x); else ceil(x); end if; end proc:     q := proc(x) sin(x)^2; end proc: end module:
 > $\mathrm{Perl}\left(m,\mathrm{resultname}=\mathrm{t0}\right)$
 #!/usr/bin/perl use POSIX; package m;   sub p   {     local($x,$y) = @_;     if (0 < $y) { return(int($x));     } else {       return(ceil($x)); } } sub q { local($x) = @_;     return(sin($x) ** 2); } Translate a linear combination of hyperbolic trigonometric functions.  > $\mathrm{Perl}\left(2\mathrm{cosh}\left(x\right)-7\mathrm{tanh}\left(x\right)\right)$ $cg = 2 * cosh($x) - 7 * tanh($x);

Translate a procedure with no return value containing a printf statement.

 > f := proc(a::integer, p::integer)   printf("The integer remainder of %d divided by %d is: %d\n", a, p, irem(a, p)); end proc:
 > $\mathrm{Perl}\left(f\right)$
 #!/usr/bin/perl sub f {   local($a,$p) = @_;   printf("The integer remainder of %d divided by %d is: %d\n", $a,$p, $a %$p); }

Compatibility

 • The CodeGeneration[Perl] command was introduced in Maple 18.
 • For more information on Maple 18 changes, see Updates in Maple 18.

 See Also