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