JSPL::Function - Reference to a JavaScript Function
Functions in JavaScript are actually Function-objects. This class encapsulates them and allows you to invoke them from Perl.
Function
The basic way to invoke a JSPL::Function instance is with the method "call" in JSPL::Context.
my $func = $ctx->eval(q{ // Define a simple function function myfunc(arg1) { say("You sendme " + arg1); }; // And return a reference to it myfunc; }); $ctx->call($func => "some text"); # say 'You sendme some text'
You can use $ctx->call with the name of the Function as its first argument, but a JSPL::Function instance can hold a reference to an anonymous one:
$ctx->call
my $func2 = $ctx->eval(q{ function (a, b) { return a + b }; }); $ctx->call($func2 => 5, 6); # 11 $ctx->call($func2 => "foo", "bar"); # 'foobar'
Instances of JSPL::Function implement a short cut to avoid such verbose code:
$func2->(10, 20); # 30 $func->('a value'); # Say "You sendme a value"
Please read on.
This class inherits from JSPL::Object.
Function instances are JavaScript Objects and as such, they have some methods, and this module adds some more, usable from perl.
$func->call($somethis, $arg1, $arg2);
Call the underlaying JavaScript Function as an instance of the $somethis argument. All remaining arguments are passed as arguments to the function.
That is, inside the function this will be the value of $somethis.
this
This is the analogous to func.call(somethis, arg1, arg2) in JavaScript when func is a reference to the function to be called.
func.call(somethis, arg1, arg2)
func
This is different from $ctx->call($func, ...) that always uses the global object for this.
$ctx->call($func, ...)
$func->apply($somethis, \@arguments);
Call the underlaying JavaScript Function in the same way as "call" above, but use the elements of $array_arguments as the arguments to the call, $array_arguments must be an ARRAY reference.
$array_arguments
Analogous to func.apply(somethis, arguments) in JavaScript.
func.apply(somethis, arguments)
Call the underlaying JavaScript Function as a constructor.
Returns the prototype of the function as a JSPL::Object. Useful if the function is a constructor and you need to inspect or modify its prototype property.
prototype
Returns a CODE_REF that encapsulate a closure that calls the underlaying JSPL::Function.
The reference is cached, so every time you call CODE_REF, you obtain the same reference. This reference is the same one used for the "OVERLOADED OPERATIONS" below, so you seldom need to call this method.
All instances of Function have a few properties which can be used in Perl when the JSPL::Function is seen as a JSPL::Object
$func->{name}; # 'myfunc'
Retrieves the name of the function.
$func->{length}; # 1
Retrieves the number of arguments that the function expects.
Instances of this class overload &{} which means that you can use the instance as a code-reference directly.
&{}
$func->($arg1, $arg2, ...);
And inherit the overload %{} from "OVERLOADED OPERATORS" in JSPL::Object.
%{}
To install JSPL, copy and paste the appropriate command in to your terminal.
cpanm
cpanm JSPL
CPAN shell
perl -MCPAN -e shell install JSPL
For more information on module installation, please visit the detailed CPAN module installation guide.