Math::E...::Parser(3)

πŸ“¦
version 0.3.2 Β· 2 versions
, in fedora 43

πŸ’– Math::Expression::Evaluator::Parser πŸ’–

okay bestie, so this page is all about Math::Expression::Evaluator::Parser β€” that's the Perl module from the User Contributed Perl Documentation corner of the man pages, and she is literally the girl who takes your messy little math string and turns it into a whole tree structure. slay. 🌸


✨ NAME ✨

Math::Expression::Evaluator::Parser β€” parse mathematical expressions. that's it, that's the job, and she does it gorgeously πŸ’…


✨ SYNOPSIS ✨

use Math::Expression::Evaluator::Parser;
my $exp = '2 + a * 4';
my $ast = Math::Expression::Evaluator::Parser::parse($exp, {});
# $ast is now something like this:
# $ast = ['+',
#            2,
#           ['*',
#            ['$', 'a'],
#            4
#           ]
#          ];

so you hand her a string, she hands you back an AST. mutual exchange, no cap. 🫢


✨ DESCRIPTION ✨

This module parses a mathematical expression in usual notation, and turns it into an Abstract Syntax Tree (AST). yes queen, tree-ify that math. 🌳✨

If you want a simple interface and want to actually evaluate these ASTs, go use Math::Expression::Evaluator. that's the vibes-y wrapper, this one is the raw parser doing the heavy lifting.

🚨 okay important thing, bestie, listen up: the description of the AST structure below matches the current implementation, but it really is an implementation detail that's subject to change without further notice. in particular, a possible addition of meta information (like file and line numbers) might require a change of structure. so don't go building your whole personality around it, mmkay? 😭

The AST is a tree that consists of nested array refs. The first item is a string (until now always a single character πŸ’€) and denotes the type of the node. The rest of the items in the array is a list of its arguments.

For the mathematical symbols "+", "-", "*", "/", "^" (exponentation) this is straight forward, but "/" and "-" are always treated as prefix ops, so the string '2 - 3' is actually turned into "['+', 2, ['-', 3]]". not me side-eyeing that design choice but okay, iconic, we respect her.

Other AST nodes are:

  • '$' β€” "['$', $var_name]" represents a variable.
  • '{' β€” "['{', $expr1, $expr2, ... ]" represents a block, i.e. a list of expressions.
  • '=' β€” "['=', $var, $expr]" represents an assignment, where $expr is assigned to $var.
  • '&' β€” "['&', $name, @args]" is a function toll to the function called $name.

(yes it says "toll" in the original and honestly? we're keeping it, character-for-character, that's the rule. 😌)


✨ METHODS ✨

🌈 parse

"parse" takes a string and a hash ref, where the hash ref takes configuration parameters. Currently the only allowed option is "force_semicolon". If set to a true value, it forces statements to be forced by semicolons (so "2 3" will be forbidden, "2; 3" is still allowed).

πŸ’€ heads up: "parse" throws an exception on parse errors. so wrap her in an eval or a Try::Tiny or whatever your coping mechanism of choice is, bestie.

and that's the whole page, babe. πŸŽ€ this was brought to you by perl v5.42.0, dated 2025-07-25, filed under Math::E...::Parser(3) in the User Contributed Perl Documentation section β€” go forth and parse responsibly, queen πŸ‘‘βœ¨

πŸ“¦ where this page comes from