The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

JSP::Stash - Perl namespaces reflector for javascript.

DESCRIPTION

Every perl namespace when exposed to javascript either automatically or by the methods of the JSP::Controller perl class is represented by an instance of a Stash.

In perl a particular namespace can be used either to simply collect a bunch of variables and subroutines or to implement a complete class. The ways in that you can use a Stash instance in javascript are diferent.

In fact a perl namespace can be exposed to javascript without binding the associated Stash to any property in your global object. Making it invisible and its use transparent. That whats happens when a perl object enters javascript land and you call its instance methods.

Javascript interface

TBD

Perl interface

The value returned by "add" in JSP::Controller and Stash instances entering perl land are wrapped as JSP::Stash objects.

my $ctl = $ctx->get_controller;
my $stash = $ctl->add('DBI'); # Expose to js the package 'DBI'.

Instance methods

allow_from_js ( BOOLEAN )
$stash->allow_from_js($bool);

Call this with a TRUE value to allow javascript code to make changes to the associated perl namespace. All namespaces are, by default, not modifiable from javascript.

For example:

# Expose the namespace 'ForJSUse'
my $stash = $ctl->add('ForJSuse');
# Make 'ForJSUse' modifiable by js code
$stash->allow_from_js(1);
class_bind ( BIND_POINT )
$stash->class_bind($prop_name);

Make the package visible in javascript as a class under the given property BIND_POINT.

For example:

require 'DBI';
$ctl->add('DBI')->class_bind('DBI');

Exposes the perl package 'DBI' and binds it to the property of the same name as a class, allowing to call its methods as static ones.

package_bind ( BIND_POINT )
$stash->package_bind($prop_name);

Make the package visible in javascript as a simple collection of values (normally soubroutines) under the property BIND_POINT.

For example:

require 'POSIX';
$ctl->add('POSIX')->package_bind('POSIX');

So you can call any subroutine in package 'POSIX' from javascript:

// In javascript land
var fd = POSIX.open('foo', POSIX.O_RDONLY());
// Yes, fd is a real file descriptor
POSIX.lseek(fd, 0, POSIX.SEEK_SET())
...
POSIX.close(fd);
set_constructor ( )
set_constructor ( CODE )
set_constructor ( SUB_NAME )

TBD

add_properties

TBD