NAME
JS::Array - Reference to a javascript Array
DESCRIPTION
Arrays in javascript are actually Array
-objects. When returned to Perl side, they are encapsulated in this class and, by default, tied to make them usable as a normal Perl array-references.
...
my $arr = $ctx->eval(q|['foo', 'bar', 4]|);
print $arr->[1]; # 'bar'
print scalar @$arr; # 3
print shift @$arr; # 'foo'
print ref $arr; # 'ARRAY'
my $obj = tied @$arr;
print ref $obj; # 'JS::Array'
INTERFACE
When a javascript array, i.e an instance of Array, enters perl space the object is wrapped by reference as a instance of JS::Array.
For transparency, and if the AutoTie
context option is TRUE, they will be tied
to a perl ARRAY and instead of the JS::Array object, the array-reference is returned, so the regular perl ARRAY operations and funtions will see a normal ARRAY.
All those ARRAYs are alive, that is, they refer to the original javascript array, .so if you modify them on one side, you are modifying both sides.
...
my $arr = $ctx->eval(q{
var Arr = ['foo', 'bar', 'baz'];
Arr;
});
$arr->[1] = 'bor';
pop @$arr;
print $ctx->eval('Arr[1]'); # 'bor'
print $ctx->eval('Arr.length'); # 2
$ctx->eval(q{ Arr.push('fob') });
print $arr->[2]; # 'fob'
If you need the underlaying JS::Array object, it can be obtained using Perl's tied
operator.
my $jsarray = tied @$arr;
In javascript all arrays are objects, so this class inherits all JS::Object's features.
INSTANCE METHODS
- length
-
Returns the length of the array.
- shift
- pop
- push
- unshift
- sort
- reverse
-
All performs the standard javascript array methods of the same name.