UPDATE to the UPDATE:
just use B::Deparse :P

UPDATE:
My first code example would only grab the inner body of a subroutine that had NO inner blocks - not very useful. After toying with look aheads, i can up with this regex:

/sub\s+$name\s+{\s+(.*?(?=^}\s*$))/sm
Where $name is the name of the subroutine to be parsed. The catch is that the closing bracket of the subroutine HAS to be the first character on it's own line.

You don't have to use the __DATA__ and seek trick, you could always just open the file if you need to parse other scripts or modules.

Here is the whole program. Improvements are very, very welcome :)

#!/usr/bin/perl -w seek(DATA,0,0); my $slurp; { local $/; $slurp = <DATA>; } print_sub($slurp,'bar'); print_sub($slurp,'foo'); sub print_sub { my ($content,$name) = @_; my ($body) = $content =~ /sub\s+$name\s+{\s+(.*?(?=^}\s*$))/sm; print $body; } sub foo { if (1) { return $_[0] + 5; } else { return 0; } # a comment } sub bar { print "howdy"; } __DATA__
FIXES:
Thanks to Hofmator for pointing out cut and paste error. Thanks again for pointing out the paran regex in Camel 3.

Jeff

R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
L-L--L-L--L-L--L-L--L-L--L-L--L-L--

In reply to (jeffa) Re: getting the body text of a sub by jeffa
in thread getting the body text of a sub by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.