SYNOPSIS
use Language::Expr;
my $le = Language::Expr->new;
# convert Expr to string Perl code
say $le->get_compiler('perl')->compile('1 ^^ 2'); => # "(1 xor 2)"
# convert Expr to JavaScript
say $le->get_compiler('js')->compile('1 . 2'); # => "'' + 1 + 2"
# evaluate Expr using the default interpreter
say $le->get_interpreter('default')->eval('1 + 2'); # => 3
# enumerate variables
my $vars = $le->enum_vars('$a*$a + sqr($b)'); # => ['a', 'b']
DESCRIPTION
Language::Expr defines a simple, Perl-like expression minilanguage. It
supports mathematical and string operators, arrays, hashes, variables,
and functions. See Language::Expr::Manual::Syntax for description of
the language syntax.
This distribution consists of the language parser
(Language::Expr::Parser), some interpreters
(Language::Expr::Interpreter::*), and some compilers
(Language::Expr::Compiler::*).
ATTRIBUTES
METHODS
new()
get_compiler($name) => obj
Get compiler named $name, e.g. perl, js.
get_interpreter($name) => obj
Get compiler named $name, e.g. default, var_enumer, dummy.
FAQ
Why yet another simplistic (restricted, etc) language? Why not just Perl?
When first adding expression support to Data::Schema (now Data::Sah), I
want a language that is simple enough so I can easily convert it to
Perl, PHP, JavaScript, and others. I do not need a fully-fledged
programming language. In fact, Expr is not even Turing-complete, it
does not support assignment or loops. Nor does it allow function
definition (though it allows anonymous function in grep/map/usort).
Instead, I just need some basic stuffs like mathematical/string/logical
operators, arrays, hashes, functions, map/grep/usort. This language
will mostly be used inside templates and schemas.
Why don't you use Language::Farnsworth, or Math::Expression, or
Math::Expression::Evaluator, or $FOO?
I need several compilers and interpreters (some even with different
semantics), so it's easier to start with a simple parser of my own. And
of course there is personal preference of language syntax.
What is the difference between a compiler and interpreter?
An interpreter evaluates expression as it is being parsed, while a
compiler generates a complete Perl (or whatever) code first. Thus, if
you $le->eval() repeatedly using the interpreter mode (setting
$le->interpreted(1)), you will repeatedly parse the expression each
time. This can be one or more orders of magnitude slower compared to
compiling into Perl once and then directly executing the Perl code
repeatedly.
Note that if you use $le->eval() using the default compiler mode, you
do not reap the benefits of compilation because the expression will be
compiled each time you call $le->eval(). To save the compilation
result, use $le->compile() or $le->perl() and compile the Perl code
yourself using Perl's eval().
I want different syntax for (variables, foo operator, etc)!
Create your own language :-) Fork this distribution and start modifying
the Language::Expr::Parser module.
How to show details of errors in expression?
This is a TODO item.
KNOWN BUGS
Due to possible bugs in Perl's RE engine or Regexp::Grammars or my
grammar, some syntax errors will cause further parsing to fail.
SEE ALSO
Syntax reference: Language::Expr::Manual::Syntax
Modules that are using Language::Expr: Data::Sah, Data::Template::Expr
(not yet released).
Other related modules: Math::Expression, Math::Expression::Evaluator,
Language::Farnsworth