Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Helper script to format code and output to perlmonks markup

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


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

That's why I just hacked 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 "./" inside your source file.

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

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

use v5.12; use warnings; do './'; say for <DATA> __DATA__ 1 2 3

<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
1 2 3

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 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?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2021-08-04 02:19 GMT
Find Nodes?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)

    Results (41 votes). Check out past polls.