Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Helper script to format code and output to perlmonks markup

by LanX (Saint)
on Apr 04, 2021 at 21:03 UTC ( [id://11130822]=monkdiscuss: print w/replies, xml ) Need Help??

Hi

It's really tedious to need to correct code and example output after spotting a flaw.

That's why I just hacked PM.pl to automatically wrap code-tags around my combined source-file and output, so I just need one copy and paste.

Just put it into the same directory like your other scripts.

Activate it just with do "./PM.pl" inside your source file.

It requires a __DATA__ at the end of your source code and will warn you otherwise

NB: The do PM.pl; is omitted as is __DATA__ if it's the last line.

DEMO:
This
use v5.12; use warnings; do './PM.pl'; say for <DATA> __DATA__ 1 2 3

creates
<code> use v5.12; use warnings; say for <DATA> __DATA__ 1 2 3 </code> OUTPUT: <blockquote><i><code> 1 2 3 </code></i></blockquote>

which renders as

use v5.12; use warnings; say for <DATA> __DATA__ 1 2 3
OUTPUT:
1 2 3

PM.pl

The code is pretty self explanatory, you can adjust it to your needs

package PM; use v5.12; use warnings; use Scalar::Util qw/openhandle/; my ($pck) = caller() // __PACKAGE__; my $data = eval "\\*${pck}::DATA"; # *DATA of caller my $ctag = "code"; # avoid messup local $|=1; unless ( openhandle($data) ) { warn "__DATA__ missing, can't format source code"; } else { my $current_pos = tell $data; seek $data,0,0; say "<$ctag>"; while ( <$data> ) { print unless # ignore ... /^(do)\s.*PM/ # . do PM.pl or /^__DATA__/ && eof($data); # . __DATA__ on last line } say "</$ctag>"; seek $data,$current_pos,0; } say "OUTPUT: <blockquote><i><$ctag>"; END { say "</$ctag></i></blockquote>" }; __DATA__

UPDATE: fixed localization of non-buffering

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: monkdiscuss [id://11130822]
Approved by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-19 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found