The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Data::Typed::Expression - Parsing typed expressions

VERSION

Version 0.003

SYNOPSIS

  use Data::Typed::Expression;
  use Data::Typed::Expression::Env;
  
  my $env = Data::Typed::Expression::Env->new({
      vertex => {
                  id => 'int',
                  lon => 'double',
                  lat => 'double'
          },
          arc => {
                  from => 'vertex',
                  to => 'vertex',
                  cost => 'double'
          },
          graph => {
                  arcs => 'arc[]',
                  vertices => 'vertex[]'
          },
          'int' => undef, 'double' => undef
  }, {
          G => 'graph',
          i => 'int'
  });
  my $expr = Data::Typed::Expression->new('G.arcs[G.v[i]+1]');
  
  $env->validate($expr);
                                                                    

DESCRIPTION

When I was writing a LaTeX paper on mathematical model of an optimization problem, I was in a need to use C-like expressions to illustrate ideas I was writing about. I felt really uncomfortable beacuse I couldn't easily validate the expressions I was using. Hence this module.

The module can parse standard C expressions (or rather a small subset of them) and validate them in the context of some types. Validation step checks if the types of values on which artihmetics is performed are numeric, whether array indices are of int type and if compund types (struct-s) have components referenced by the expression.

The idea was born on this Perlmonks thread: http://perlmonks.org/?node_id=807424.

METHODS

new

Creates a new expression object. The only argument is a string containing expression to be parsed.

The method dies if the expression can't be parsed (i.e. is invalid or to complicated).

Usefulness of an object itself is limited. Pass the object to e.g. Data::Typed::Expression::Env to check type correctness of the expression.