Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

WebPerlizator - automating WebPerl (by haukex) page creation

by Discipulus (Canon)
on Oct 19, 2018 at 11:10 UTC ( #1224330=CUFP: print w/replies, xml ) Need Help??

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 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 -s -i input1.txt %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

or even perl  -s -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 = ''.(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 $0 -script [ -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: -code "-le 'print qq(Hello WebPerl!)'" -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 }


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.

Replies are listed 'Best First'.
Re: WebPerlizator - automating WebPerl (by haukex) page creation
by haukex (Archbishop) on Oct 19, 2018 at 12:26 UTC

    Very nice, thank you!

    A note on the quoting of the "command line": The code that parses the command line is currently kind of simplistic. If an argument is surrounded by double quotes, then the only two things inside those quotes that get special treatment are double-backslashes (\\, aka "\\\\"), which produce a single backslash, and \", which produces a double-quote; all other characters, including single quotes, are not touched. The same goes for arguments surrounded by single quotes, where double quotes are not touched. (Update: I've now updated the documentation to describe this.)

    So something like perl -e 'print "Foo\'Bar"' is fine, the equivalent Perl script is print "Foo'Bar".

Re: WebPerlizator - automating WebPerl (by haukex) page creation
by haukex (Archbishop) on Oct 19, 2018 at 16:03 UTC

    I should also add that you can also edit everything directly in the editor:

    • Add a script and input/output files via the "Tools" menu
    • Delete files with the link at the top right of each file
    • Rename files simply by editing their name
    • The "Copy URL" function will get the URL of the current state of the editor into the clipboard
    • Update: Also, set options like the brand new "autorun" (applies to the copied link) and "merge STDOUT + STDERR" via the "Tools" menu
Re: WebPerlizator - automating WebPerl (by haukex) page creation
by morgon (Priest) on Oct 20, 2018 at 01:05 UTC

    WebPerl is way beyond cool...

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://1224330]
Approved by Ratazong
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2023-12-05 04:31 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (25 votes). Check out past polls.