NAME

JavaScript::Any::Context - an environment where custom functions can be defined and Javascript evaluated

SYNOPSIS

  use JavaScript::Any;
  
  my $context = JavaScript::Any->new_context;
  $context->define( say => sub { print @_, "\n" } );
  $context->eval('say(1 + 2)');

DESCRIPTION

This is a Role::Tiny role defining the API for objects returned by JavaScript::Any->new_context.

Methods

$object->implementation

Returns the implementation class name as a string.

$object->define($str, $coderef)

Defines a custom Perl function which can be called by Javascript code.

The current implementation only allows the name of the function to consist of word characters. Defining a function called, for example, window.alert, would not be allowed because of the dot.

Roughly speaking, when the function is called by Javascript, whatever arguments are passed to it are available in @_ to your Perl code, and whatever value your coderef returns is returned as the Javascript function's return value. The exact details are currently specific to the backend implementation.

$object->define($str, $value)

Where $value is a non-reference value, defines a simple string or numeric variable. JavaScript is somewhat more typed than Perl, so be aware that currently whether $value is considered to be a string or a number by Javascript is currently undetermined behaviour.

$object->eval($str)

Evaluates a string of Javascript code and returns the result.

BUGS

Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=JavaScript-Any.

SEE ALSO

JavaScript::Any, Role::Tiny.

AUTHOR

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE

This software is copyright (c) 2017 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.