Qgoda

 view release on metacpan or  search on metacpan

lib/Qgoda/Command/Javascript.pm  view on Meta::CPAN

			} elsif ('perl' eq $lc_format) {
				$input_data = eval $input_data;
				die $@ if $@;
			} elsif ('storable' eq $lc_format) {
				$input_data = Storable::thaw($input_data);
			}
		};
		if ($@) {
			die __x("error reading input data from '{filename}': {error}!\n",
			        filename => $input_filename, error => $@);
		}
	}

	my @global = (@{$options{global}}, $q->nodeModules);
	my $js = Qgoda::JavaScript::Environment->new(
		no_output => $options{no_output},
		no_console => $options{no_console},
		global => \@global
	);

	$js->exchange(input => $input_data) if defined $input_data;

	$js->run($code);

    return $self;
}

1;

=head1 NAME

qgoda javascript - Execute JavaScript code inside Qgoda

=head1 SYNOPSIS

    qgoda javascript [<global options>] [-i|--input=FILENAME]
                     [-g|--global=MODULE_DIR1] [-g|--global=MODULE_DIR2]
                     [--input-data=DATA]
                     [--input-format=JSON|YAML|Perl|Storable]
                     [--no-stdout] [--no-stderr]
                     [FILENAME...]

Try 'qgoda --help' for a description of global options.

=head1 DESCRIPTION

Executes JavaScript code inside Qgoda, using the embedded JavaScript
engine L<Duktape|https://duktape.org/>. This is not meant as a serious
JavaScript interpreter or command-line frontend to Duktape but rather
a debugging aid for Qgoda.

You have to pass a filename containing JavaScript code as an argument
or prove the code on standard input.

Note that Duktape is B<not> NodeJS! Not all constructs accepted by NodeJS
are accepted by Duktape!

Qgoda, however, implements a module resolver so that you can use constructs
like:

    const _ = require('lodash');

The module is resolved in the same way that NodeJS would resolve it.
See the option I<--global> below for more information.

=head1 OPTIONS

=over 4

=item I<-i, --input=FILENAME>

Data stored in B<FILENAME> is assigned to the global JavaScript variable
C<__perl__.input>.  This is the common convention for JavaScript code in
Qgoda for injecting input variables to JavaScript code.

=item I<--input-format=FORMAT>

The input format used for input data.  Must be one of:

=over 4

=item I<JSON>

See L<https://www.json.org/>.

=item I<YAML>

See L<http://yaml.org>.

=item I<Perl>

The input will be evaluated as Perl code.  You can produce such input
manually or with L<Data::Dumper> or L<Data::Dump>.

=item I<Storable>

Data that can be deserialized with L<Storable>::thaw().

=back

The default is "JSON".  Case does not matter.

=item I<--input-data=DATA>

Like I<--input> but the data is not read from a file but from the string
B<DATA>.  The data format is determined by I<--input-format>.

=item I<-g|--global=DIRECTORY>

Prepend B<DIRECTORY> to the list of global directories to search for
JavaScript modules.  The default is the Qgoda package directory, that
is the same path where F<Qgoda.pm> is installed but without the trailing
F<.pm>.

The B<DIRECTORY> should be specified I<without> the trailing F<node_modules>.

=item I<--no-console>

Do not use Qgoda's console polyfill but that of Duktape itself.  Note that
the Duktape console polyfill only supports C<console.log()>!



( run in 0.779 second using v1.01-cache-2.11-cpan-5a3173703d6 )