How can I find all the methods that contain a specific string within a Perl codeset? -
i've been given tricky task write log entry @ points within large million+ line code base.
the points need log can found list of 500+ template types. template type string "end_assignment_affiliate" or "interview_accepted".
i'm trying work out how write perl script take list of 500 templates , search through code find methods make use of each specific template string (i hope use list of methods find entry points system each template type).
so example may have
sub asub { my($arg) = @_ ... if ($template eq 'interview_accepted') { ... }
i want determine method asub contains interview_accepted. interview_accepted may contained within multiple subroutines.
it's quite easy grep code base message type , find line number in files message exists, i'm having hard time trying identify containing method.
clearly if can programmatically more robust, repeatable , quicker.
does know of modules or tricks can use achieve this?
update
i'm playing using file::readbackwards find string, , point find first sub [name] { point. i'm wondering though if there more elegant solution?
my cpan module devel::examine::subs
can has()
method or function. here's sample script using oo version, want. enter directory want search (recursively) argument one, , search term argument two:
#!/usr/bin/perl use warnings; use strict; use 5.18.0; use devel::examine::subs; use file::find; $des = devel::examine::subs->new(); $dir = $argv[0]; $search = $argv[1]; find({ wanted => \&check_subs, no_chdir => 1 }, $dir, ); sub check_subs { if (! -f or ! /(?:\.pm|\.pl)$/){ return; } $file = "$file::find::name"; @has = $des->has({file => $file, search => $search}); return if ! @has; "\n$file:" ; "\t$_" @has; }
called perl des.pl business-isp/ template
results in example output:
business-isp/lib/business/isp/reports.pm: income_by_item renewal_notices business-isp/lib/business/isp/gui/accounting.pm: _display_plan_stats process_renew display_add_plan email_invoice process_purchase display_payment_form client_delete _contact_info_table show_plan display_uledger add_plan business-isp/lib/business/isp/gui/base.pm: start _header display_config _render_error _blank_header _footer
update: i've modified script can used in loop bunch of search terms. populate template names @searches
array, , specify directory structure search in $dir
.
#!/usr/bin/perl use warnings; use strict; use 5.18.0; use devel::examine::subs; use file::find; $des = devel::examine::subs->new(); $dir = 'business-isp/'; @searches = qw(template other); $search (@searches){ "\n***** searching for: $search *****\n"; find({ wanted => sub { check_subs($search) }, no_chdir => 1 }, $dir ); } sub check_subs { $search = shift; if (! -f or ! /(?:\.pm|\.pl)$/){ return; } $file = "$file::find::name"; @has = $des->has({file => $file, search => $search}); return if ! @has; "\n$file:" ; "\t$_" @has; }
update: here's script uses new has()
method lines
parameter set. retrieves entire line search hits, along line number it's on:
#!/usr/bin/perl use warnings; use strict; use 5.18.0; use devel::examine::subs; use file::find; $des = devel::examine::subs->new(); $dir = 'business-isp/'; @searches = qw(date); $search (@searches){ "\n***** searching for: $search *****\n"; find({ wanted => sub { check_subs($search) }, no_chdir => 1 }, $dir ); } sub check_subs { $search = shift; if (! -f or ! /(?:\.pm|\.pl)$/){ return; } $file = "$file::find::name"; %subs = $des->has({file => $file, search => $search, lines => 1}); return if not %subs; print "\n$file:\n\n"; $sub (keys %subs){ print "$sub:\n"; $line_info (@{$subs{$sub}}){ while (my ($k, $v) = each (%$line_info)){ print "\tline num: $k, line data: $v\n"; } } } }
output:
business-isp/lib/business/isp/sanity.pm: validate_data: line num: 168, line data: $self->validate_value({ audit: line num: 72, line data: $date = $self->date({ => $schedule }); line num: 77, line data: # update audit list if process claiming line num: 86, line data: date => $self->date({ => 'day' }), line num: 108, line data: date => { -like => "$date%" }, line num: 123, line data: $executed_date = $executed->date; line num: 126, line data: "process $process has run $schedule cycle on $executed_date"; validate_renew: line num: 304, line data: $self->validate_value({ validate_value: line num: 193, line data: # return if validate_value disabled! line num: 204, line data: print "sanity validate_value_debug: $tag, $value\n"; business-isp/lib/business/isp/gui/accounting.pm: confirm_payment: line num: 1312, line data: $date = $self->string_date(); line num: 1316, line data: $self->pb_param( date => $date ); display_invoice: line num: 1867, line data: $date = $invoice->[0]->{ date }; line num: 1928, line data: $template->param( date => $date );
Comments
Post a Comment