The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Code::Perl::Expr - Produce Perl expression from a tree

SYNOPSIS

  use Code::Perl::Expr qw( :easy );

  my $c = derefh(scal('hash'), calls('getkey'));

  print $c->perl; # ($hash)->{getkey()}

DESCRIPTION

Code::Perl::Expr handles the expressions part of Code::Perl. It can export convenience functions for constructing new Expr objects of all available types.

Rather than documenting each available Expression class in it's own file, they are documented here, along with easy constructor exported by this module.

EXPRESSION CLASSES

Code::Perl::Expr has been removed from the front of the class names, so for example SubName is really Code::Perl::Expr::SubName.

Number

  $class->new(Value => $number);

  number($number);

$number - a number

Number is for handling numbers.

  number(5)->perl # 5

String

  $class->new(Value => $string);

  string($string);

$string - a string

String is for handling strings. It produces a double quoted string, escaped so that it will stay on one line

  string('hello\nworld$')->perl # "hello\nworld\$"

Scalar

  $class->new(Name => $name);

  scal($name);

$name - a string

This handles a scalar.

  scal("var")->perl # $var

List

  $class->new(Value => \@exprs);

  list(@exprs);

@exprs - an array of Expression objects

List is for producing a comma separated list of expressions.

  list(scal("v"), string("a"), number(5))->perl # $v, "a", 5

Not

  $class->new(Expr => $expr);

  boolnot($expr);

$expr - an Expression object.

This applies a logical not to $expr

  boolnot(scal("is"))->perl # ! $is

Append

  $class->new(Exprs => \@exprs);

  append(@exprs);

@exprs - a list of Expression objects.

Append produces code that appends the expressions together using Perl's . string append operator.

  append(string("hello"), string(" world"))->perl # "hello"." world";

DerefHash

  $class->new(Ref => $hash, Key => $key);

  derefh($hash, $key);

$hash - Expression objects $key - Expression objects or string

derefh() will turn $key into a String object if it is a string.

DerefHash is for dereferencing a hash ref.

  derefh(scal("hash"), "key")->perl # $hash->{"key"}
  derefh(scal("hash"), calls("getkey"))->perl # $hash->{getkey()}

DerefArray

  $class->new(Ref => $hash, Index => $index);

  derefa($hash, $index);

$hash - an Expression objects $index - an Expression object or a number

derefa() will turn $index into a Number object if it is a number.

DerefArray is for dereferencing an array ref.

  derefa(scal("array"), 5)->perl # $array->[5]
  derefa(scal("array"), calls("getindex"))->perl # $array->[getindex()]

SubName

  $class->new(Value => $name);

  subname($name);

$name should be a string, naming a subroutine or a method. Used when creating a CallSub or CallMethod object.

CallSub

  $class->new(SubName => $sub, Args => $args);

  calls($subname, @args);

$sub, $args - Expression objects. $subname - an Expression object or a string

calls will turn $subname into a SubName object if it is a string. It will wrap @args in a List object.

CallSub will produce code to call the subroutine in $sub with passing the list in $args.

  calls("wangle", string("John"))->perl # wangle("John")

  callm(scal("sub_ref"), string("John"))->perl # &{$sub_ref}("John")

CallMethod

  $class->new(Object => $object, MethodName => $method, Args => $args);

  callm($object, $methodname, @args);

$object, $method, $args - Expression object. $methodname - an Expression object or a string

callm will turn $methodname into a SubName object if it is a string. It will wrap @args in a List object.

CallMethod will produce code to call the method in $method on the object in $object, passing the list in $args.

  callm(scal("obj"), "setName", string("John"))->perl # $obj->setName("John")

  callm(scal("obj"), scal("meth"), string("John"))->perl # $obj->$meth("John")

It is possible to pass in any Expression object in $method, however the only ones that make sense are SubName and Scalar objects.

Perl

  $class->new(Perl => $perl);

  perl($perl);

$perl - a string of Perl code.

Perl allows you to insert a piece of Perl code as is, without have to parse it and turn it into a Code::Perl structure.

  perl("2 + 2")->perl # 2 + 2

Holder

  $class->new(Expr => $expr);

  holder($expr);

$expr - an Expression object.

A Holder simply holds another expression inside. A Holder is useful when you want "parameterise" an expression. For example,

  $holder = holder();
  $exp = derefh(scal("hash"), derefa(scal("array", $holder));

  $holder->setExpr(number(10));
  print $exp->perl # $hash->{$array->[10]);

  $holder->setExpr(calls("getnum"));
  print $exp->perl # $hash->{$array->[getnum()]);

A single Holder can be placed in more than one position in your tree, allowing you make the same expression appear in multiple places in your code of you code and allowing you to change all of the occurrences at the same time.

AUTHOR

Written by Fergal Daly <fergal@esatclear.ie>.

COPYRIGHT

Copyright 2003 by Fergal Daly <fergal@esatclear.ie>.

This program is free software and comes with no warranty. It is distributed under the LGPL license

See the file LGPL included in this distribution or http://www.fsf.org/licenses/licenses.html.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 188:

=cut found outside a pod block. Skipping to next block.