Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

comment on

( #3333=superdoc: 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.

In reply to WebPerlizator - automating WebPerl (by haukex) page creation by Discipulus

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (1)
As of 2022-07-02 22:50 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (103 votes). Check out past polls.