JE::Object::Function(3pm) | User Contributed Perl Documentation | JE::Object::Function(3pm) |
JE::Object::Function - JavaScript function class
use JE::Object::Function; # simple constructors: $f = new JE::Object::Function $scope, @argnames, $function; $f = new JE::Object::Function $scope, $function; # constructor that lets you do anything: $f = new JE::Object::Function { name => $name, scope => $scope, length => $number_of_args, argnames => [ @argnames ], function => $function, function_args => [ $arglist ], constructor => sub { ... }, constructor_args => [ $arglist ], downgrade => 0, }; $f->(@args); $f->call_with($obj, @args);
All JavaScript functions are instances of this class. If you want to call a JavaScript function from Perl, just treat is as a coderef ("$f->()") or use the "call_with" method ("$f->call_with($obj, @args)") if you want to specify the invocant (the 'this' value).
The return value of the function will be upgraded if necessary (see UPGRADING VALUES in the JE::Types man page), which is why "new" has to be given a reference to the global object or the scope chain. (But see also "new_function" in JE and "new_method" in JE.)
A function written in Perl can return an lvalue if it wants to. Use "new JE::LValue($object, 'property name')" to create it. To create an lvalue that refers to a variable visible within the function's scope, use "$scope->var('varname')" (this assumes that you have shifted the scope object off @_ and called it $scope; you also need to call "new" with hashref syntax and specify the "function_args" [see below]).
- a global (JE) object - a scope chain (JE::Scope) object
@argnames is a list of argument names, that JavaScript functions use to access the arguments.
$function is one of
- a string containing the body of the function (JavaScript code) - a JE::Code object - a coderef
This will be run when the function is called from JavaScript without the "new" keyword, or from Perl via the "call" method.
self the function object itself scope the scope chain global the global object this the invocant args the arguments passed to the function (as individual arguments) [args] the arguments passed to the function (as an array ref)
If "function_args" is omitted, 'args' will be assumed.
If this is omitted, when "new" or "construct" is used, a new empty object will be created and passed to the sub specified under "function" as its 'this' value. The return value of the sub will be returned if it is an object; the (possibly modified) object originally passed to the function will be returned otherwise.
This is completely ignored if "constructor" is omitted.
This method is very badly named and will probably be renamed in a future version. Does anyone have any suggestions?
This method is very badly named and will probably be renamed in a future version. Does anyone have any suggestions?
You can use a JE::Object::Function as a coderef. The sub returned simply invokes the "call" method, so the following are equivalent:
$function->call( $function->global->upgrade(@args) ) $function->(@args)
The stringification, numification, boolification, and hash dereference ops are also overloaded. See JE::Object, which this class inherits from.
2021-01-08 | perl v5.32.0 |