Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: regexp for finding all function calls

by Anonymous Monk
on Jul 09, 2009 at 03:15 UTC ( #778464=note: print w/replies, xml ) Need Help??

in reply to regexp for finding all function calls

Something along the lines of ...
#!c:\Perl\bin\perl.exe # # This recursively searches .pl and .pm files for parenthesized functi +on calls. # $ARG[0] specifies the initial directory, # $ARG[1] specifies the function name, # for example: # c:\alpha\adx gettext # use File::Find; use Perl6::Slurp; use strict; use warnings; # Prowl the drive looking for files named .pl and .pm find({ wanted => \&FindFunctionCalls__, follow => 0 } ,$ARGV[0]); exit; sub FindFunctionCalls__ { # Don't trace me # $File::Find::dir is the current directory name, # $_ is the current filename within that directory # $File::Find::name is the complete pathname to the file. return unless (m/\.p(l|m)$/i); return if (/^(p|s)\./); my($s_Function)=($ARGV[1]); # Slurp my $s_Code=slurp $File::Find::name, {}; my $Count; # ... and search for "$s_Function" if (my $Count=($s_Code=~s/(?:^|\W)$s_Function\W/$&/g)) { # $s_ +Function occurred at least once # Remove the "use statement;" if any print "\nFound $Count in $File::Find::name\n"; # Balanced parentheses: my $np; # The initial balance parentheses expression with an embed +ded set is: # $np=qr/\( ([^()] | (??{$np})) *\)/x; # and quoted strings are "(?:[^"]|\")*" and '(?:[^']|\')* +' or ('|")(?:[^\1]|\\1)*\1 so $np=qr/ \( + # The opening "(" (( + # We'll want this hence the capturing () '(?:[^']|\')*?' + # a single quote string | "(?:[^"]|\")*?" + # a double quote string | [^()] + # not a parentheses | (??{$np}) )*) \) + # and the closing ")" /x; # Find the body of the function calls my($s_CompleteCall,$s_Before); while ($s_Code =~ m/(?:^|\W)($s_Function\s*$np)/gos) { # H +ave the entire call! # The complete call is $s_CompleteCall=$1; # and line number is print 1+(($s_Before=$`)=~ tr/\n/\n/).": $s_CompleteCal +l\n"; }; }; }; # __END__

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://778464]
[karlgoethebier]: now it's getting weird...
[GotToBTru]: 'getting'?
[Discipulus]: why karlgoethebier ?
[GotToBTru]: ugh .. what's the term for comparing versions of an algorithm to determine which is most efficient?
[hippo]: Benchmarking?
[GotToBTru]: hippo++

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (16)
As of 2017-06-22 12:46 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (519 votes). Check out past polls.