Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

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


In reply to Helper script to format code and output to perlmonks markup by LanX

Title:
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?
Username:
Password:

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

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








    Results (41 votes). Check out past polls.

    Notices?