| 1 | package Perlito5; | ||
| 2 | |||
| 3 | $VERSION = '9.028'; | ||
| 4 | |||
| 5 | 1; | ||
| 6 | |||
| 7 | =pod | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Perlito5 - a Perl5 compiler | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use Perlito5::Compiler; | ||
| 16 | use Perlito5::JavaScript2::Emitter; | ||
| 17 | use Perlito5::JavaScript2::Runtime; | ||
| 18 | use Data::Dumper; | ||
| 19 | |||
| 20 | my $perl5_source = ' print "hello, World!\n" '; | ||
| 21 | $Perlito5::PKG_NAME = 'main'; | ||
| 22 | $Perlito5::PROTO = {}; | ||
| 23 | my $ast = Perlito5::Grammar::exp_stmts($perl5_source, 0); | ||
| 24 | print "Syntax Tree:\n"; | ||
| 25 | print Dumper Perlito5::Match::flat($ast); | ||
| 26 | my $js_source = Perlito5::AST::CompUnit::emit_javascript2_program([ | ||
| 27 | Perlito5::AST::CompUnit->new( name => 'main', body => Perlito5::Match::flat($ast) ) | ||
| 28 | ]); | ||
| 29 | print "JavaScript source code:\n"; | ||
| 30 | print $js_source; | ||
| 31 | |||
| 32 | |||
| 33 | # in the command line: | ||
| 34 | |||
| 35 | perlito5 --help | ||
| 36 | |||
| 37 | perlito5 -Cjs program.pl | ||
| 38 | |||
| 39 | perlito5 -Cjava program.pl | ||
| 40 | |||
| 41 | =head1 DESCRIPTION | ||
| 42 | |||
| 43 | This program reads Perl5 source code and generates native code. | ||
| 44 | |||
| 45 | The compiler options are available with the command: | ||
| 46 | |||
| 47 | perlito5 -h | ||
| 48 | |||
| 49 | =head1 COOKBOOK | ||
| 50 | |||
| 51 | =head2 Obtaining a "perlito5.js" script that executes in the "node.js" JavaScript compiler | ||
| 52 | |||
| 53 | # Step 1: use perlito5 to compile perlito5 to JavaScript. | ||
| 54 | perlito5 --bootstrapping -Cjs `which perlito5` > perlito5.js | ||
| 55 | |||
| 56 | # Step 2: ensure that PERL5LIB points to the directory where Perlito5.pm is installed. | ||
| 57 | echo $PERL5LIB | ||
| 58 | |||
| 59 | # Step 3: set PERL5LIB if needed. | ||
| 60 | perldoc -l Perlito5 | ||
| 61 | # /usr/local/lib/perl5/site_perl/5.20.1/Perlito5.pm | ||
| 62 | export PERL5LIB=/usr/local/lib/perl5/site_perl/5.20.1 | ||
| 63 | |||
| 64 | # Step 4: install nodejs if needed. | ||
| 65 | |||
| 66 | # Step 5: this should work now: | ||
| 67 | nodejs perlito5.js -e ' print "hello, World!\n" ' | ||
| 68 | # hello, World! | ||
| 69 | |||
| 70 | |||
| 71 | =head2 Obtaining a Perl compiler script that runs in the browser | ||
| 72 | |||
| 73 | # Step 1: create a Perl script that loads the compiler and evaluates a string. | ||
| 74 | # Save this to "perlito5-browser.pl": | ||
| 75 | |||
| 76 | use Perlito5::Compiler; | ||
| 77 | use Perlito5::JavaScript2::Emitter; | ||
| 78 | use Perlito5::JavaScript2::Runtime; | ||
| 79 | sub eval_perl5 { | ||
| 80 | eval "$_[0]; 1" | ||
| 81 | or warn "Perl error: $@"; | ||
| 82 | } | ||
| 83 | |||
| 84 | # Step 2: use perlito5 to compile your "perlito5-browser.pl" script to JavaScript. | ||
| 85 | |||
| 86 | perlito5 --bootstrapping -Cjs perlito5-browser.pl > perlito5-browser.js | ||
| 87 | |||
| 88 | # Step 3: load the compiler in the browser; | ||
| 89 | # compile perl5 to javascript; | ||
| 90 | # eval() the javascript code. | ||
| 91 | # Note: the default implementation for "print" will invoke console.log(); | ||
| 92 | # you can override this by redefining p5pkg['Perlito5::IO'].print() | ||
| 93 | |||
| 94 | <html> | ||
| 95 | <head> | ||
| 96 | <script type="text/javascript" src="perlito5-browser.js"></script> | ||
| 97 | <script type="text/javascript"> | ||
| 98 | function hello() { | ||
| 99 | return p5pkg.main.eval_perl5([ | ||
| 100 | ' print "hello, World!\n" ' | ||
| 101 | ]); | ||
| 102 | } | ||
| 103 | </script> | ||
| 104 | </head> | ||
| 105 | <body> | ||
| 106 | <input type="button" value="Execute" onclick="hello()"/> | ||
| 107 | </body> | ||
| 108 | </html> | ||
| 109 | |||
| 110 | |||
| 111 | =head1 AUTHORS | ||
| 112 | |||
| 113 | Flavio Soibelmann Glock <fglock@gmail.com>. | ||
| 114 | |||
| 115 | =head1 SEE ALSO | ||
| 116 | |||
| 117 | L<http://fglock.github.io/Perlito> | ||
| 118 | |||
| 119 | =head1 COPYRIGHT | ||
| 120 | |||
| 121 | Copyright 2011-2017 by Flavio Soibelmann Glock and others. | ||
| 122 | |||
| 123 | This program is free software; you can redistribute it and/or modify it | ||
| 124 | under the same terms as Perl itself. | ||
| 125 | |||
| 126 | See L<http://dev.perl.org/licenses/artistic.html> | ||
| 127 | |||
| 128 | =cut | ||
| 129 |