There's nothing wrong in using an external find command in a pipe open, especially if you're not concerned about portability across systems.

But then you're using perl to run what is fundamentally a shell script. In particular there's absoultely no need to fork out external grep's with that system.

Also you have:

open (FIND_CMD, "find /home/modules |"); chomp (my @findArr = <FIND_CMD>);
I recommend:
  1. using the three args form of open and lexical filhandles,
  2. checking errors,
  3. avoiding to slurp in a potentially big list, whereas we have a syntactically sweet enough while loop just to process it one item at a time.
my $pattern = '.pm$'; my $FilePattern = "sub user_method"; my $ResultFilePath = '/tmp/ModuleSearch'; foreach my $File ( @findArr ) { next if ( $File !~ m/$pattern/ );

This will match Foo.apm; you may want to learn about \Q (and \E) in perldoc perlop.

But since you're running an external find anyway, why not let it do the dirty job? (-name '*.pm')

system ("grep -iHl \"$FilePattern\" \"$File\" >> $ResultFilePath ") +;

I wish I had not seen that...

All in all it may have been:

#!/usr/bin/perl use strict; use warnings; open my $find, '-|', 'find /home/modules -name "*.pm"' or die "Can't run find(1): $!\n"; $\="\n"; while (<$find>) { chomp; open my $fh, '<', $_ or die "Can't open `$_': $!\n"; while (my $line=<$fh>) { print, last if $line =~ /sub user_method/; } } __END__

A compressed version (having a huge pile o' magic in it) of which could be:

#!/usr/bin/perl -ln use strict; use warnings; BEGIN { @ARGV='find /home/modules -name "*.pm"|' } local @ARGV=$_; /sub user_method/ and (print $ARGV), last while <> ; __END__

(both untested)


In reply to Re^2: Getting Filenames that contains a particular string recursively From a Directory by blazar
in thread Getting Filenames that contains a particular string recursively From a Directory by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.