Language::Expr::Manual::Syntax - Description of the Language::Expr language
version 0.15
Language::Expr language is very simple. It has just enough features to support mathematical/logical/string operations, arrays, hashes, variables, and function calls.
Language::Expr is (intentionally) not Turing-complete (lacks assignment and loops).
It should be trivial to implement an interpreter or code generator from the parser. In fact, Language::Expr is meant to be easily convertible to Perl, PHP, and Javascript (among others like Python and Ruby).
undef
true false
1 -2.3 inf nan 0x1f # hexadecimal, = 31 in decimal 0o17 # octal, = 15 in decimal 0b100 # binary, = 4 in decimal
Single-quoted strings, e.g. 'single quoted'. Supported escape sequences:
\' literal single quote \\ literal backslash
Double-quoted strings, e.g. "double quoted". Supported escape sequences:
\' literal single quote \" literal double quote \\ literal backslash \$ prevent variable interpolation \t tab \n newline \r linefeed \f formfeed \b backspace \a bell \e escape \0 or \03 or \033 octal char \x7 or \x7B hex char \x{263a} wide hex char
Difference with Perl: Perl supports a few other escape sequences, but they are not commonly found in other scripting languages (e.g.: named Unicode character or the \l, \L, et al), so they are not included.
Double-quoted strings will also interpolate variables, e.g.:
"I have $num apples" "This is Foo::Bar version ${perl:/Foo/Bar/VERSION}"
[] [1, 2, "str"]
Difference from Perl: dangling comma at the end is not allowed.
{} {a => 1, "b c" => 2+3}
Difference from Perl: you must always use "=>" to separate key and value, not comma. Dangling comma at the end is not allowed.
Many operators are taken from Perl, along with their precedence levels and associativity, but here are the differences:
No assigment operators.
Because assignment is not supported.
No smart-match "~~" operator or the other more esoteric or Perl-specific operators.
These include Perl's "..", "...", "->", etc.
Currently no "and", "or", "not" operators.
Use &&, ||, ! instead. Perl supports an extra, low-precedence set of logical operators mostly to separate statements, which we do not have.
"xor" becomes "^^" (to be consistent with "^" as bitwise-xor).
Currently no tertiary "?:" operator.
Use if() function instead (availability depends on interpreter/compiler).
Hash value is accessed using [] instead of {}.
Which, BTW, is also the way it is done in Python, Ruby, and PHP.
Comparison operators can be chained (except <=> and cmp).
Below is list of supported operators, ordered from lowest precedence, along with their associativity.
left => left || // ^^ left && left | ^ left & left == != <=> cmp eq ne < > <= >= ge gt le lt nonassoc <=> cmp left << >> left + - . left * / % x right ! ~ unary+ unary- right ** left hash[s], array[i] left term (variable, str/num literals, (paren), func())
left =>
left || // ^^
left &&
left | ^
left &
left == != <=> cmp eq ne < > <= >= ge gt le lt
nonassoc <=> cmp
left << >>
left + - .
left * / % x
right ! ~ unary+ unary-
right **
left hash[s], array[i]
left term (variable, str/num literals, (paren), func())
There are two syntax of variables:
$alphanum123 (including $_) $. $..
and:
${anything goes except closing curly brace}
Difference from Perl: The simple syntax does not allow namespaces/package names (e.g. $foo::bar). In fact, the compiler/interpreter is allowed to have its own namespace scheme. Data::Schema and Data::Template::Expr uses a namespace + filesystem-like path scheme, e.g.:
${../foo} ${/foo/bar/baz} ${schema:/foo/bar/baz} ${data:../../baz}
Examples:
rand() length("foo")
Difference from Perl: parentheses are required.
The language define just a few functions:
This is similar to Perl's map() (but notice the required parentheses), it will form a new array composed from the result of EXPR. EXPR will be evaluated for each element of ARRAY (stored in $_). The original value of $_ will be restored after EXPR completes.
This is similar to Perl's grep() (but notice the required parentheses), it will form a new array composed from the elements of ARRAY when EXPR evaluates to true (like in Perl, empty string '', the number 0, boolean false, undef are considered false). EXPR will be evaluated for each element of ARRAY (stored in $_). The original value of $_ will be restored after EXPR completes.
false
This is similar to Perl's sort() (but notice the required parentheses), it will return the ARRAY sorted using comparison in EXPR. EXPR will be evaluated for each element of ARRAY ($a and $b will be set with two values to be compared). The original value of $a and $b will be restored after EXPR completes.
Steven Haryanto <stevenharyanto@gmail.com>
This software is copyright (c) 2011 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.
To install Language::Expr, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Language::Expr
CPAN shell
perl -MCPAN -e shell install Language::Expr
For more information on module installation, please visit the detailed CPAN module installation guide.