Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Searching Multiple directories???

by calebcall (Sexton)
on Oct 20, 2011 at 02:01 UTC ( #932541=perlquestion: print w/replies, xml ) Need Help??
calebcall has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write something that will search multiple directories. Say my script is called, I want to be able to pass it multiple arguments and have it search each one for something specific. Example:
# ./ /home/me /home/you
Right now It will only search through the first directory. Below is what I'm using right now to do this. I'm using Find::File::Rule which if I'm reading correctly, is supposed to be able to accept an array. I don't doubt that I'm doing something wrong. (I'm a complete newb at this stuff)
#!/usr/bin/env perl use strict; use warnings; use File::Find::Rule; my @files = find( file => name => [qw/ *.mkv *.avi / ], in => @ARGV ); foreach my $infile (@files) { print $infile . "\n"; }
I've tried manually building my array (i.e. changing @ARGV to @dirs and using my @dirs = ("/home/me","/home/you"); ) and it still does not work, so I'm thinking it's something with how I'm trying to use the directories. Any suggestions?

Replies are listed 'Best First'.
Re: Searching Multiple directories???
by GrandFather (Sage) on Oct 20, 2011 at 02:14 UTC

    Try passing an array reference as the value for 'in':

    in => \@ARGV

    Otherwise the first element is treated as the directory to search and the remaining elements are treated as parameters to find.

    True laziness is hard work
      Wow, something as simple as a \, that worked perfectly. I wish I knew what exactly that did, guess I need to go research what an array reference is :) Thanks!
        You could consider arming yourself with some of the many wonderful Perl books around as well:
        Effective Perl Programming - D'Foy et al
        Conway's OO Perl
        MJD's Higher Order Perl
        In particular I would recommend the Perl Cookbook.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://932541]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2018-01-20 11:09 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (226 votes). Check out past polls.