Language::Expr - Simple minilanguage for use in expression
This document describes version 0.23 of module Language::Expr (in distribution Language-Expr), released on 2014-05-01.
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::*).
Whether to use the interpreter. By default is 0 (use the compiler, which means Language::Expr expression will be compiled to Perl code first before executed).
Note: The compiler is used by default because the interpreter currently lacks subexpression (map/grep/sort) support. But the compiler cannot by default directly use variables and functions defined by var() and func(). This slight inconvenience might be rectified in the future.
Store the Language::Expr::Interpreter::Default instance.
Store the Language::Expr::Compiler::Perl instance.
Store the Language::Expr::Compiler::JS instance.
Store the Language::Expr::Compiler::PHP instance.
Store the Language::Expr::Interpreter::VarEnumer instance.
Construct a new Language::Expr object, which is just a convenient front-end of the Expr parser, compilers, and interpreters. You can also use the parser/compiler/interpreter independently.
Define variables. Note that variables are only directly usable in interpreted mode (see SYNOPSIS for example on how to use variables in compiled mode).
Define functions. Dies if function is defined multiple times. Note that functions are only directly usable in interpreted mode (see SYNOPSIS for example on how to use functions in compiled mode).
Evaluate expression in STR (either using the compiler or interpreter) and return the result. Will die if there is a parsing or runtime error. By default it uses the compiler unless you set
interpreted to 1.
Also see compile() which will always use the compiler regardless of
interpreted setting, and will save compilation result into a Perl subroutine (thus is more efficient if you need to evaluate an expression repeatedly).
Convert expression in STR and return a string Perl code. Dies on error. Internally just call $le->compiler->perl().
Convert expression in STR and return a string PHP code. Dies on error. Internally just call $le->php_compiler->php().
Compile expression in STR into Perl subroutine. Dies on error. See also eval().
Inside the expression, you can use '$_', '$_', etc to access the subroutine's arguments, because compile() sets $_ to @_. Example:
my $sub = $le->compile('($_**2 + $_**2)**0.5'); say $sub->(3, 4); # 5
Enumerate variables mentioned in expression STR. Return empty arrayref if no variables are mentioned.
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.
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().
Create your own language :-) Fork this distribution and start modifying the Language::Expr::Parser module.
This is a TODO item.
Due to possible bugs in Perl's RE engine or Regexp::Grammars or my grammar, some syntax errors will cause further parsing to fail.
Syntax reference: Language::Expr::Manual::Syntax
Please visit the project's homepage at https://metacpan.org/release/Language-Expr.
Source repository is at https://github.com/sharyanto/perl-Language-Expr.
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Language-Expr
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
Steven Haryanto <firstname.lastname@example.org>
This software is copyright (c) 2014 by Steven Haryanto.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.