behindalens has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,
I appreciate any help or guidance to this question as I am a novice to programming or anything resembling cs. I need to fill an array with the file paths of files, lets say its .txt files in this case. In a shell I would use the command:
find . -name "*.txt"

Can I run this within a perl script so I can fill an array with these filepaths (to be used in my script).
I appreciate the help!
  • Comment on Is there a unix find equivalent that can be processed inside a perl script?
  • Download Code

Replies are listed 'Best First'.
Re: Is there a unix find equivalent that can be processed inside a perl script?
by broomduster (Priest) on Sep 18, 2008 at 22:39 UTC
    See File::Find, which is part of the Perl distribution. There's also find2perl, which shows how a particular find command can be converted into Perl code that uses File::Find.
Re: Is there a unix find equivalent that can be processed inside a perl script?
by kyle (Abbot) on Sep 18, 2008 at 22:39 UTC
Re: Is there a unix find equivalent that can be processed inside a perl script?
by merlyn (Sage) on Sep 19, 2008 at 01:52 UTC
      What, no magazine article on the subject? ;-)

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Is there a unix find equivalent that can be processed inside a perl script?
by jwkrahn (Abbot) on Sep 19, 2008 at 00:52 UTC
    chomp( my @filepaths = qx[find . -name "*.txt"] );
Re: Is there a unix find equivalent that can be processed inside a perl script?
by repellent (Priest) on Sep 19, 2008 at 05:22 UTC
    use File::Find; my @filepaths; find sub { push @filepaths, $File::Find::name if /\.txt$/ }, '.'; # $_ is set to the current file basename

    Update: Made a fix. Thanks, ikegami.
      Not quite.