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:
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./sub\s+$name\s+{\s+(.*?(?=^}\s*$))/sm
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 :)
FIXES:#!/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__
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |