translate Maple code to Perl code - Maple 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 nm=expr, where nm is a name and 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.

withCodeGeneration:

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

Perlx+yz2xz,resultname=w

$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.

Perlx,2y,5,z,resultname=w

$w = [[$x,2 * $y],[5,$z]];

Translate a computation sequence.  Optimize the input first.

cs:=s=1.0+x,t=lnsⅇx,r=ⅇx+xt:

Perlcs,optimize

$s = 0.10e1 + $x;
$t1 = log($s);
$t2 = exp(-$x);
$t = $t1 * $t2;
$r = $x * $t + $t2;

Translate a procedure.

f := proc(x, y, z) return x*y-y*z+x*z; end proc:

Perlf

#!/usr/bin/perl

sub f
{
  local($x, $y, $z) = @_;
  return($x * $y - $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:

Perlf

#!/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:

Perlf

#!/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:

Perlm&comma;resultname&equals;t0

#!/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.

Perl2coshx7tanhx

$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:

Perlf

#!/usr/bin/perl

sub f
{
  local($a, $p) = @_;
  printf("The integer remainder of %d divided by %d is: %d\n", $a, $p, $a % $p);
}

See Also

CodeGeneration, CodeGenerationOptions, PerlDetails, trademark, TranslationDetails


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam