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

Language::FormulaEngine::Formula

VERSION

version 0.08

SYNOPSIS

  $formula= $engine->parse($text_expression);
  
  $value= $formula->evaluate(x => 1);
  
  $formula2= $formula->simplify(y => 2);
  
  $coderef= $formula2->compile;

DESCRIPTION

This is a convenient way to carry around the details of a parsed formula and later evaluate it, simplify it, or compile it. It's simply a wrapper around the engine that created it + the parse tree.

ATTRIBUTES

engine

Reference to a Language::FormulaEngine instance.

orig_text

Original string of text that was parsed into this formula. This may be undef if the formula was generated. In that case, see "deparse" or "to_string".

parse_tree

Reference to the output of "parse" in Language::FormulaEngine::Parser

functions

A set of { $name => 1 } for each named function used in this formula.

symbols

A set of { $name => 1 } for each named variable used in this formula.

CONSTRUCTOR

Standard Moo constructor accepts any of the attributes above.

METHODS

evaluate

  $raw_value= $formula->evaluate;
  $raw_value= $formula->evaluate(\%alt_vars);
  $raw_value= $formula->evaluate($alt_namespace);

Evaluate the formula, optionally specifying variables or a different namespace in which to evaluate it.

simplify

  $formula2= $formula1->simplify;
  $formula2= $formula1->simplify(\%alt_vars);
  $formula2= $formula1->simplify($alt_namespace);

Simplify the formula by substituting known variable values and evaluating pure functions. You can optionally specify variables or a different namespace which should be used.

compile

  my $sub= $formula->compile;
  my $sub= $formula->compile($subname);

Return an optimized perl coderef for the formula. The signature of the coderef depends on the settings of the $formula->engine->compiler. Throws an exception if the compile fails.

deparse

Re-stringify the formula, using $self->engine->parser.

to_string

Return either orig_text, or deparse. This is used when stringifying the object.

AUTHOR

Michael Conrad <mconrad@intellitree.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by Michael Conrad, IntelliTree Solutions llc.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.