I would like a routine that returned one array containing all information on
each file and sub-directory, including type, date/time etc.
I'm running Active Perl on Win NT.
Any suggestions?
Andy Turpin
: Oh.. Maybe one more thing: A stat is a fairly expensive operation. It is
On windows systems the stat should be no more expensive than the name
lookup as the information is stored in the same place.
If you were working at a lower level then you would get all the file
details during the same disk access.
> Please does anyone know of good alternatives to opendir / readdir, as I find
> them rather limited.
File::Find
> I would like a routine that returned one array containing all information on
> each file and sub-directory, including type, date/time etc.
Unless you want to recursively decend directories File::Find is
overkill for something so simple:
sub get_dir {
my $dir = shift;
local *DIR;
opendir(DIR,$dir) or die "Cannot read directory $dir: $!";
map { [ $_, stat "$dir/$_" ] } readdir DIR;
}
Note: I should really be checking that stat() succedes.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
You'll have to write one yourself. opendir, readdir and closedir,
together with stat will do what you want. I wouldn't want this stuff to
come back in one array though.. You better have a bit more thought about
what your data structure should be.
Oh.. Maybe one more thing: A stat is a fairly expensive operation. It is
often much better to only do a stat on a file or directory for which you
think you are going to need the information. I guess that that is one of
the reasons why there isn't such a module as you suggest, and why i
wouldn't even want to think of writing one.
opendir(DIR, $dir) or die "opendir($dir): $!";
while(my $file = readdir<DIR>)
{
# weed out the bad ones
my @stat = stat "$dir/$file";
# maybe store the info somewhere?
}
closedir(DIR);
Can you tell me what is so horrible about having to write that code? I
count about 4 lines of significant code there....
Martien
--
Martien Verbruggen |
Interactive Media Division | life ain't fair, but the root
Commercial Dynamics Pty. Ltd. | password helps. -- BOFH
NSW, Australia |
> : > Please does anyone know of good alternatives to opendir /
> : > readdir, as I find them rather limited.
>
> : Oh.. Maybe one more thing: A stat is a fairly expensive operation.
>
> On windows systems the stat should be no more expensive than the
> name lookup as the information is stored in the same place.
Well.. You'd still need a separate stat for the file themselves, but
assuming you're right:
opendir(DIR, $dir) or die $!;
%files =
map { ("$dir/$_" => [stat "$dir/$_"]) }
grep { !/^..?$/ }
readdir(DIR);
closedir(DIR);
while (my ($k, $v) = each %files)
{
printf "$k => %s\n", $v->[1] || 'UNDEF';
}
or if you are interested in the order:
opendir(DIR, $dir) or die $!;
my @files =
map { ["$dir/$_" => [stat "$dir/$_"]] }
grep { !/^..?$/ }
readdir(DIR);
closedir(DIR);
foreach (@files)
{
printf "$_->[0] => %s\n", $_->[1]->[1] || 'UNDEF';
}
Of course anyone is free to wrap this in subs or classes/objects if
they feel like it, but I still maintain that the module would be
expensive to use, and anyone implementing that should make note of
that in the documentation.
Oh, and you should probably use File::Spec to make it more portable.
> If you were working at a lower level then you would get all the file
> details during the same disk access.
but you're not working at a lower level :)
Martien
--
Martien Verbruggen |
Interactive Media Division | 42.6% of statistics is made up on the
Commercial Dynamics Pty. Ltd. | spot.
NSW, Australia |
12 wrote:
>
> Please does anyone know of good alternatives to opendir / readdir, as I find
> them rather limited.
>
> I would like a routine that returned one array containing all information on
> each file and sub-directory, including type, date/time etc.
>
> I'm running Active Perl on Win NT.
>
> Any suggestions?
>
> Andy Turpin
--
_____cliff_rayman_____cliff_@_rayman_._com_____