Hello nuns and monks!
did you heard the big news? WebPerl by haukex is here!
I smell it as the best thing happened to Perl since years (alongside with MCE ..) and I already used to produce PM examples: very useful and shining.
But as I'm really lazy I wrote the below snippet that try to automate the process of copying perl code to the page, creating input files, etc..
Imagine that you have simple_reader.pl as follow:
use strict;
use warnings;
open my $fh,'<', 'input1.txt' or die;
while (<$fh>) {print if /b/}
and input1.txt containing:
aaa
bbb
ccc
then you can just run:
perl webperlizator.pl -s simple_reader.pl -i input1.txt
https://webperl.zero-g.net/democode/perleditor.html#%7B%22script_fn%22
+%3A%22simple_reader.pl%22%2C%22script
%22%3A%22use%20strict%3B%5Cnuse%20warnings%3B%5Cn%5Cnopen%20my%20%24fh
+%2C%27%3C%27%2C%20%27input1.txt%27%20or
%20die%3B%20%5Cnwhile%20%28%3C%24fh%3E%29%20%7Bprint%20if%20%2Fb%2F%7D
+%22%2C%22inputs%22%3A%5B%7B%22text%22%3
A%22aaa%5Cnbbb%5Cnccc%5Cn%22%2C%22fn%22%3A%22input1.txt%22%7D%5D%2C%22
+cmdline%22%3A%22perl%20simple_reader.pl%22%7D
or even perl webperlizator.pl -s simple_reader.pl -i input1.txt --browse to open it directly in the browser!
have fun and thanks haukex!!
update newer version, with more features, is on my github
use strict;
use warnings;
use URI::Escape;
use Getopt::Long;
use JSON::MaybeXS qw(encode_json);
my (@infiles, @outfiles, $script, $lineofcode, $browse, $help);
unless ( GetOptions (
"script=s" => \$script,
"line|oneliner|code|c=s" => \$lineofcode,
"inputfiles=s" => \@infiles,
"outputfiles|o=s" => \@outfiles,
"browse" => \$browse,
"help" => \$help
))
{
print "GetOpt::Long returned errors (see a
+bove), available options:\n\n".help();
exit;
}
if ($help){ print help(); exit 0;}
my $json = {};
if ($lineofcode){
$$json{cmdline} = "perl $lineofcode";
}
elsif ($script){
open my $fh, '<', $script or die "unable to read $script!";
while (<$fh>){
$$json{script} .= $_ ;
}
$$json{script_fn} = $script;
$$json{cmdline} = "perl $script";
}
else{
die "Please feed at least one script using -script or a line of pe
+rl code via -code\n\n".help();
}
if ( $infiles[0] ){
$$json{inputs}=[];
}
foreach my $in (@infiles){
open my $fh, '<', $in or die "unable to read $in!";
my $file = { fn => $in};
while (<$fh>){
$$file{text}.=$_;
}
push @{$$json{inputs}},$file;
}
if ( $outfiles[0]){
$$json{outputs} = \@outfiles ;
}
my $url = 'https://webperl.zero-g.net/democode/perleditor.html#'.(uri_
+escape( encode_json( $json ) ));
if ($browse){
if ($^O =~/mswin32/i) {exec "start $url"}
else{ exec "xdg-open $url"}
}
else{
print $url;
}
####
sub help{
return <<EOH;
$0 USAGE:
--script file|--code line [--inputfile file [--inputfile file] --o
+utputfile file [--outputfile file] --browse]
$0 -script script.pl
$0 -script script.pl [ -inputfile file1.txt -inputfile file2.txt
+-outputfile file3.txt -browse]
$0 -code "-e 'print qq(Hello WebPerl!)'"
$0 -code "-e 'print qq(Hello WebPerl!)'" [ -i infile1.txt -i infil
+e2.txt -o outfile3.txt -browse]
--script -s accept a perl program filename as only argument.
Both --script and --code make no sense: just specify one.
--code -c is intended to be used to pass a oneliner. The execu
+table name, aka perl, will be
prepended automatically. Any perl switch must be explicitly passed
+ also -e
For example:
webperlizator.pl -code "-le 'print qq(Hello WebPerl!)'"
webperlizator.pl -code "-lne 'print \"found a b\" if /b/' file1.tx
+t" -i file1.txt -b
Pay attention on quotes suitable for you OS.
--inputfiles -i is for input files; more than one can be feed
--outputfiles -o is for output file and more than one can be passe
+d in
--browse -b open the default browser, hopefully, pointing to the W
+ebPerl right page
--help -h prints this help
EOH
}
L*
PS updated to handle onliners in a better way
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.