use Text::Balanced qw(extract_codeblock); # get source code in this variable my $source = ....; # regexp finds where method is defined and sets "pos" (see perldoc -f pos), # than extract_codeblock extracts following codeblock if($source =~ /public \s+ void \s+ readExternal \s+ \( .*? \) \s+ /gx) { my $sub_source = extract_codeblock($source); print $sub_source; }