The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

JSPL::PerlClass - Create native JavaScript classes in Perl

INTRODUCTION

If you known what a "native class" is you can jump to "INTERFACE" now. If not, let me introduce you to a few important concepts that you need to understand well if you think that you need the services provided by JSPL::PerlClass.

JavaScript, the language, doesn't really have classes. It is a prototype based object oriented language. When somebody talks about a "class", they are normally talking about a function that is meant to be called as a constructor, using the new operator.

The result of calling a constructor is a new object. This new object inherits the prototype property of the constructor as the head of its prototype chain. This way the new object "inherits" all properties and methods of its creator's prototype. All objects sharing the same prototype chain can be said to form a "class".

The SpiderMonkey engine uses the concept of native class to refer to the mechanism which you can use to install hooks into the normal processing of JavaScript object operations.

INTERFACE

Constructor

new ( %args )

Create a new native JavaScript class.

It expects the following arguments

name

The name of the class in javascript.

  name => "MyPackage",
constructor

A reference to a subroutine that returns the Perl object that represents the javascript object. If omitted a default constructor will be supplied that calls the method new on the defined package (or name if no package is defined).

  constructor => sub { MyPackage->new(@_); },
package

The name of the Perl package that represents this class. It will be passed as first argument to any class methods and also used in the default constructor.

  package => "My::Package",
methods (fs)

A hash reference of methods that we define for instances of the class. In javascript this would be o = new MyClass(); o.method().

The key is used as the name of the function and the value should be either a reference to a subroutine or the name of the Perl subroutine to call.

  methods => { to_string => \&My::Package::to_string,
               random    => "randomize"
  }
static_methods (static_ps)

Like fs but these are called on the class itself. In javascript this would be MyClass.method().

properties (ps)

A hash reference of properties that we define for instances of the class. In javascript this would be o = new MyClass(); f = o.property;

The key is used as the name of the property and the value is used to specify what method to call as a get-operation and as a set-operation. These can either be specified using references to subroutines or name of subroutines. If the getter is undefined the property will be write-only and if the setter is undefined the property will be read-only. You can specify the getter/setter using either an array reference, [\&MyClass::get_property, \&MyClass::set_property], a string, "MyClass::set_property MyClass::get_property" or a hash reference, { getter = "MyClass::get_property", setter => "MyClass::set_property" }>.

  ps => { length => [qw(get_length)],
          parent => { getter => \&MyClass::get_parent, setter => \&MyClass::set_parent },
        }
static_properties (static_ps)

Like ps but these are defined on the class itself. In javascript this would be f = MyClass.property.

flags

A bitmask of attributes for the class. Valid attributes are:

JS_CLASS_NO_INSTANCE

Makes the class throw an exception if JavaScript tries to instantiate the class.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 176:

=over without closing =back