NAME
Qgoda::JavaScript::Filter - Require JavaScript source files
SYNOPSIS
Use the first line in the following snippet as the first line of your JavaScript files:
//; use Qgoda::JavaScript::Filter('Qgoda::JavaScript::console');
/* Example Javascript code. */
console.log('Hello, world');
The one and only argument should be the package name you want the code to be available as, here Qgoda::JavaScript::console.
Getting the content of the JavaScript file goes like this:
require 'Qgoda/JavaScript/console.js';
my $code = Qgoda::JavaScript::console->code;
# Feed the code into an ECMAScript engine:
JavaScript::Duktape::XS->new->eval($code);
DESCRIPTION
Qgoda comes with several JavaScript files. Those in Qgoda::node_modules are loaded by JavaScript itself. Those under Qgoda::JavaScript are loaded by Perl.
METHODS
- import YOUR_PACKAGE
-
You have to "use()" the JavaScript module with the desired package name. Otherwise your code will be lost in lala land.
METHODS IN THE GENERATED PACKAGE
Apart from those defined by UNIVERSAL:
- code
-
A class method that returns the JavaScript code contained in the file.
LIMITATIONS
Perl source filters do not parse __DATA__ sections but this source filter starts a data section at the very beginning. Due to that fact the first line of your JavaScript code will be lost!
The filter therefore generates a syntax error, when your first line of JavaScript code does not begin with "//" or contains only whitespace.
For the same reason you always have to add 2 to the line number if the JavaScript engine reports an error. The first line is for the "use" directive, the second one for the lost line.
Why the hack (pun intended)?
You could of course just write your code in the data section of a regular Perl module. This is indeed what the filter does unter the hood. But since you mix JavaScript with Perl syntax, syntax highlighter can only highlight one portion of the code and that portion would unfortunately be the insignificant Perl code.
But more important is actually that you can syntax-check the JavaScript code the file is valid Perl and valid JavaScript at the same time.