How can I find all methods that contain a specific string in Perl code?

I was given the difficult task of writing a journal entry at specific points in a database with a lot of millions + rows.

The points that I need to register can be found in the list of 500 types of templates. The template type is just a string like "end_assignment_affiliate" or "interview_accepted".

I am trying to figure out how to write a perl script that will take a list of 500 templates and then do a code search to find methods that use each particular line of the template (I then hope to use a list of search methods for all the login points for each type of template) .

So, for example, I may have

sub aSub {
 my($arg) = @_
 ...
 if ($template eq 'interview_accepted') {
 ...
}

, aSub interview_accepted. interview_accepted .

grep , , .

, , , .

- - , ?

Update

File:: ReadBackwards, , [] {. , ?

+1
2

CPAN Devel::Examine::Subs has(). script OO, , . , () , :

#!/usr/bin/perl
use warnings;
use strict;
use 5.18.0;

use Devel::Examine::Subs;
use File::Find;

my $des = Devel::Examine::Subs->new();

my $dir = $ARGV[0];
my $search = $ARGV[1];

find({ wanted => \&check_subs, no_chdir => 1 }, 
       $dir,
);

sub check_subs {

    if (! -f or ! /(?:\.pm|\.pl)$/){
        return;
    } 

    my $file = "$File::Find::name";

    my @has = $des->has({file => $file, search => $search});

    return if ! @has;

    say "\n$file:" ;
    say "\t$_" for @has;
}

: perl des.pl business-isp/ template :

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: script, . @searches $dir.

#!/usr/bin/perl
use warnings;
use strict;
use 5.18.0;

use Devel::Examine::Subs;
use File::Find;

my $des = Devel::Examine::Subs->new();

my $dir = 'business-isp/';
my @searches = qw(template this that other);

for my $search (@searches){

    say "\n***** SEARCHING FOR: $search *****\n";

    find({ wanted => sub { check_subs($search) }, no_chdir => 1 }, 
           $dir
    );
}

sub check_subs {

    my $search = shift;

    if (! -f or ! /(?:\.pm|\.pl)$/){
        return;
    } 

    my $file = "$File::Find::name";

    my @has = $des->has({file => $file, search => $search});

    return if ! @has;

    say "\n$file:" ;
    say "\t$_" for @has;
}

UPDATE: script, has() lines. , , :

#!/usr/bin/perl
use warnings;
use strict;
use 5.18.0;

use Devel::Examine::Subs;
use File::Find;

my $des = Devel::Examine::Subs->new();

my $dir = 'business-isp/';
my @searches = qw(date);

for my $search (@searches){

    say "\n***** SEARCHING FOR: $search *****\n";

    find({ wanted => sub { check_subs($search) }, no_chdir => 1 }, 
           $dir
    );
}

sub check_subs {

    my $search = shift;

    if (! -f or ! /(?:\.pm|\.pl)$/){
        return;
    } 

    my $file = "$File::Find::name";


    my %subs = $des->has({file => $file, search => $search, lines => 1});

    return if not %subs;

    print "\n$file:\n\n";

    for my $sub (keys %subs){    

        print "$sub:\n";

        for my $line_info (@{$subs{$sub}}){
            while (my ($k, $v) = each (%$line_info)){ 
                print "\tLine num: $k, Line Data: $v\n";
            }
        }
    }
}

:

business-isp/lib/Business/ISP/Sanity.pm:

validate_data:
    Line num: 168, Line Data:  $self->validate_value({ 
audit:
    Line num: 72, Line Data:  my $date = $self->date({ get => $schedule }); 
    Line num: 77, Line Data:  # update the audit list if the process is claiming to be 
    Line num: 86, Line Data:  date => $self->date({ get => 'day' }), 
    Line num: 108, Line Data:  date => { -like => "$date%" }, 
    Line num: 123, Line Data:  my $executed_date = $executed->date; 
    Line num: 126, Line Data:  "Process $process has already run its $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 is 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:  my $date = $self->string_date(); 
    Line num: 1316, Line Data:  $self->pb_param( date => $date ); 
display_invoice:
    Line num: 1867, Line Data:  my $date = $invoice->[0]->{ date }; 
    Line num: 1928, Line Data:  $template->param( date => $date ); 
+2

.

http://perldoc.perl.org/functions/caller.html

perldoc -f caller

:

#!/usr/bin/env perl

use warnings;
use strict;

my $text = 'hello, world!';
print upper_case($text) . "\n";

sub upper_case {
    my ($str) = @_;

    #  0         1          2      3            4
    my ($package, $filename, $line, $subroutine, $hasargs,
    #  5          6          7            8       9         10
    $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash)
    = caller(0); # 0 = current scope, 1 = parent stack, 2 = 2-levels up, etc.

    warn "DEBUG: Called subroutine $subroutine with parameter '$str'\n";
    return join ' ', map {ucfirst} split /\s/, $str;
}

, , (, perltidy), .

,

sub aSub {
    my($arg) = @_
    ...
    if ($template eq 'interview_accepted') {
    ...
}

, :

sub bSub {
    my($arg) = @_
    ...
    if ($template =~ m|interview_a\w+|)
    ...
    }
}

. "eval", .

0

All Articles