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
| [reply] |
|
|
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).
| [reply] |
|
|
| [reply] |
|
|
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!
| [reply] |
Re: perl find module
by biohisham (Priest) on Oct 27, 2009 at 11:21 UTC
|
| [reply] [d/l] |
Re: perl find module
by keszler (Priest) on Oct 27, 2009 at 10:37 UTC
|
| [reply] |
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
| [reply] |
|
|
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.
| [reply] |
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 | [reply] [d/l] [select] |
|
|
findrule dir1 dir2 dir3 -file -name ( .master )
| [reply] |
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);
| [reply] [d/l] |
|
|
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";
| [reply] [d/l] |
|
|
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);
| [reply] [d/l] |
Re: perl find module
by manoj123 (Initiate) on Oct 27, 2009 at 11:01 UTC
|
| [reply] |