perldoc -f open
perldoc perlopentut
perldoc -f opendir (and readdir)
Those should enlighten you.
Cheers,
KM | [reply] |
Besides the usual opendir(), readdir() and closedir() routine to read a directory's contents, you could also use glob(), but it's pretty slow. Opening a file for writing, like mentioned above, can be achieved with open().
opendir(DIR,"/somedir") || die $!;
@Files = readdir(DIR);
closedir(DIR);
Greetz
Beatnik .
.. Quidquid perl dictum sit, altum viditur. | [reply] [d/l] [select] |
This is fairly standard, but I assume you want to create the list of files so that it DOESN'T contain the filename of the list of files? (grin)
my $fn="files.txt";
open OUT,">$fn" || die $!;
print OUT grep(!/^$fn$/,map { $_.="\n"; $_ } <*>);
close OUT;
This assumes that the current working directory is the one you wish to index. If it is not:
my $dir="thedir";
my $fn="files.txt";
open OUT,">$dir/$fn" || die $!;
print OUT grep(!/^$fn$/,map { s/^$dir\///; $_.="\n"; $_ } <$dir/*>);
close OUT;
| [reply] [d/l] [select] |
This may be slightly off topic, it sounds to me you're creating something similar to a MANIFEST file. Let me explain what this is..
On CPAN, every module has a file in it called MANIFEST. It's become a standard part of most perl module distributions. Basically, it's is a list of all the files in the current (and lower directories), along with optional comments beside each file name. It's main purpose is to be a module-wide database of required files, so that during install, any missing files will immediately raise errors from the install process.
There is a module called ExtUtils::Manifest, which is part of the standard perl distribution, that takes care of reading the current directory, and creating a MANIFEST file for you. As well, inside a file called MANIFEST.SKIP, you can specify a list of regexes that match file names to skip.
While it's interface it's very elegant, it is functional, and may give you ideas if/when you decide to roll your own manifest writer. Here's some simple example code that will create a MANIFEST with the current directories' contents inside it:
#!/usr/bin/perl -w
use ExtUtils::Manifest qw(mkmanifest);
#Optional Configuration
$ExtUtils::Manifest::Quiet = 1; #Turn off warnings
$ExtUtils::Manifest::Verbose = 0; #Turn off status updates
$ExtUtils::Manifest::MANIFEST = 'MANIFEST'; #Filename to write
#Will make a MANIFEST file for all the files in your
#current directory, including the MANIFEST file
mkmanifest();
| [reply] [d/l] |