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

Hi, I need to search a file within subdirectories with extenstion .master using perl find module. Could you please let me know how to achieve this with perl find module. -Rajini

Replies are listed 'Best First'.
Re: perl find module
by CountZero (Bishop) on Oct 27, 2009 at 11:01 UTC
    File::Find is the usual anwer to your question, but as its interface is rather arcane, File::Find::Rule is more easy to use.

    The documentation lists many examples which will directly apply to your question.

    Try it and if the solution still eludes you, let us know what you have tried and we will help you further.

    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

      but as its interface is rather arcane

      Let me respectfully disagree with that - the interface is flexible, well documented - and low-level. It is by no means arcane.

      The only obscure thing about File::Find is, IMHO, the name. It's not primarily a module to find files -- it's an iterator over files and directories (which you can use to find files, but also for various other things).

        I agree the interface is flexible, well documented and low-level, but the use of call-backs is something which is not immediately easy to understand, especially not for someone who is rather new to Perl and/or this type of programming. Hence, I call it "arcane" in the meaning of "obscure". I have to look-up the docs every time I want to use File::Find, there are too many "global" variables which contain all kinds of information some of which are to be read-only, others act as flags, ...

        File::Find::Rulen as a wrapper around File::Findn brings its interface nicely within the more familiar procedural or OO-environment.

        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

        It's usage isn't arcane if closures are used, but they aren't used in any example in the documentation. They aren't mentioned at all!
Re: perl find module
by biohisham (Priest) on Oct 27, 2009 at 11:21 UTC
    Check this node, Beginners guide to File::Find, it is not really difficult at all, in fact, I just learnt it from the documentation and perchance found this node above to be a good support too...


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: perl find module
by keszler (Priest) on Oct 27, 2009 at 10:37 UTC
Re: perl find module
by marto (Cardinal) on Oct 27, 2009 at 11:02 UTC

    Which module are you using, File::Find, File::Find::Rule or something else? Both of the modules I mentioned are well documented, File::Find::Rule even provides various examples of use. Are you experiencing a problem with code you already have?

    Martin

      Don't forget File::Finder, which has an interface much closer to find(1) if you're used to that.

      -- Randal L. Schwartz, Perl hacker

      The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

Re: perl find module
by leocharre (Priest) on Oct 27, 2009 at 13:34 UTC
    use File::Find::Rule; my $finder = File::Find::Rule->file; $finder->name( qr/\.master$/ ); my @abs_files = $finder->in('/home/myself');
    The interface can be a little funny, because the next example does the same..
    use File::Find::Rule; my @abs_files = File::Find::Rule->file->name( qr/\.master$/ )->in('/ho +me/myself');
    Anyhow, this is pretty much the same as in the examples for File::Find::Rule
      findrule dir1 dir2 dir3 -file -name ( .master )
Re: perl find module
by scorpio17 (Canon) on Oct 27, 2009 at 13:31 UTC
    # findmaster.pl # usage: findmaster.pl <path> # find files ending with '.master' use strict; use File::Find; if ($ARGV[0] eq "") { $ARGV[0]="."; } find (sub { if ($File::Find::name =~ /\.master$/i) { print "Found $File::Find::name\n"; } }, @ARGV);
      The variable $_ will contain the current file name.
      The variable $File::Find::dir will contain the current path.
      The variable $File::Find::file is the file name including the the path.
      So maybe you want to change the print line to this:
      print "Found file $_ in directory $File::Find::dir\n";

      Hi, Thanks for the reply. I am trying to fit in the below code, to search a file with the extension .master. Here how do i specify the directory which contains the master file.

      use strict; use File::Find; if ($ARGV[0] eq "") { $ARGV[0]="."; } find (sub { if ($File::Find::name =~ /\.master$/i) { print "Found $File::Find::name\n"; } }, @ARGV);

Re: perl find module
by manoj123 (Initiate) on Oct 27, 2009 at 11:01 UTC
    use File::Basename ?