NAME
JE - Pure-Perl ECMAScript (JavaScript) Engine
"JE" is short for "JavaScript::Engine."
VERSION
Version 0.006 (alpha release)
SYNOPSIS
use JE;
$j = new JE; # create a new global object
$j->eval('({"this": "that", "the": "other"}["this"])');
# returns "that"
$compiled = $j->compile('new Array(1,2,3)');
$rv = $compiled->execute; # returns a JE::Object::Array
$rv->value; # returns a Perl array ref
$obj = $j->eval('new Object');
# create a new object
$j->prop(document => $obj); # set property
$j->prop(document => {}); # same thing (more or less)
$j->prop('document'); # get a property
$j->method(alert => "text"); # invoke a method
# create global functions:
$j->new_function(correct => sub {
my $x = shift;
$x =~ y/AEae/EAea/;
substr($x,1,3) =~ y/A-Z/a-z/;
return $x;
} );
$j->new_function(print => sub { print @_, "\n" } );
$j->eval('print(correct("ECMAScript"))'); # :-)
DESCRIPTION
This is a pure-Perl JavaScript engine. All JavaScript values are actually Perl objects underneath. When you create a new JE
object, you are basically creating a new JavaScript "world," the JE
object itself being the global object. To add properties and methods to it from Perl, and to access those properties, see JE::Types
and JE::Object
, which this class inherits from.
If you want to create your own global object class (such as a web browser window), inherit from JE.
METHODS
- $j = JE->new
-
This class method constructs and returns a new global scope (
JE
object). - $j->compile( STRING )
-
compile
parses the code contained inSTRING
and returns a parse tree (a JE::Code object).The JE::Code class provides the method
execute
for executing the pre-compiled syntax tree. - $j->eval ( STRING )
-
eval
evaluates the JavaScript code contained in string. E.g.:$j->eval('[1,2,3]') # returns a JE::Object::Array which can be used as # an array ref
If an error occurs,
undef
will be returned and$@
will contain the error message. If no error occurs,$@
will be a null string.This is actually just a wrapper around C<compile> and the C<execute> method of the C<JE::Code> class.
If the JavaScript code evaluates to an lvalue, a JE::LValue object will be returned. You can use this like any other return value (e.g., as an array ref if it points to a JS array). In addition, you can use the
set
andget
methods to set/get the value of the property to which the lvalue refers. (See also JE::LValue.) E.g., this will create a new object nameddocument
:$j->eval('document')->set({});
- $j->new_function($name, sub { ... })
- $j->new_function(sub { ... })
-
This creates and returns a new function written in Perl. If $name is given, it will become a property of the global object.
For more ways to create functions, see JE::Object::Function.
- $j->upgrade( @values )
-
This method upgrades the value or values given to it. See "UPGRADING VALUES" in JE::Types for more detail.
If you pass it more than one argument in scalar context, it returns the number of arguments--but that is subject to change, so don't do that.
- $j->undefined
-
Returns the JavaScript undefined value.
- $j->null
-
Returns the JavaScript null value.
2 POD Errors
The following errors were encountered while parsing the POD:
- Around line 99:
=over without closing =back
- Around line 761:
'=end for me' is invalid. (Stack: =over; =begin for)