(jeffa) Re: Recursive opendir without
by jeffa (Bishop) on Apr 11, 2001 at 03:06 UTC
|
Recursion is fun.
Personally, I would learn about recursion from a text book
and use File::Find instead:
use strict;
use File::Find;
@ARGV = '.' unless @ARGV;
find sub {
# do something with $_
# which is the name of the current file
print "got one!\n" if m/\.mp3$/;
}, @ARGV;
Jeff
R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
L-L--L-L--L-L--L-L--L-L--L-L--L-L--
| [reply] [d/l] |
Re: Recursive opendir without
by Beatnik (Parson) on Apr 11, 2001 at 03:07 UTC
|
It's called my... look into it :) Also I did a similar thing in this node... purely for example purposes =)
Greetz
Beatnik
... Quidquid perl dictum sit, altum viditur. | [reply] |
|
I realise that i can't update my post in this section so...
You're right, I forgot the my in my question but that's not my real problem...
Thanks for your link.
| [reply] |
|
I do believe you can update your node... =)
Greetz
Beatnik
... Quidquid perl dictum sit, altum viditur.
| [reply] |
Re: Recursive opendir without
by ton (Friar) on Apr 11, 2001 at 03:12 UTC
|
use strict;
my $dir = "/";
my $file;
sub openNewDir {
my $dir = shift;
my $dh;
opendir ($dh, $dir);
while ($file = readdir ($dh)) {
next if (($file eq '.') || ($file eq '..'));
if (-d "$dir/$file") { openNewDir("$dir/$file"); }
else {
# Do something with the file
}
}
close ($dh);
}
openNewDir ($dir);
These are the important changes:
- Open to an anonymous typeglob ($dh) instead of a handle (DIR).
- Test -d on "$dir/$file" instead of "$file".
- Keep $dir in lexical scope by "my"ing it at the beginning of the subroutine.
- Concatonate the directory and file correctly for the recursive call.
- UPDATE: Skip the '.' and '..' directories.
Hope this helps...
-Ton
-----
Be bloody, bold, and resolute; laugh to scorn
The power of man... | [reply] [d/l] |
|
That is exactly what I wanted. I need to declare a my $dh.
But I steel don't understand why my opendir ($dir, $dir) doesn't work :(
Thank you very much.
BoBiOne
| [reply] [d/l] [select] |
|
I assume that you mean "why my opendir($dh,$dir) doesn't work". That is because when you opendir($dh,"/") and then readdir($dh), you'll get a file name like "usr" and then try to do opendir($dh,"usr") which isn't the same as opendir($dh,"/usr"). If your current working directory happens to be "/", then that call will work (by luck) and you'll read a file name like "home" and try opendir($dh,"home") when you should be doing opendir($dh,"/usr/home").
So you either need to chdir into each directory before you open the next one (and then chdir back out when you are done) or you need to keep track of the full path to each directory as you go so you can prepend it in several places.
-
tye
(but my friends call me "Tye")
| [reply] |
|
|
|
|
our @list;
findfile('*');
sub findfile{
my $glob = shift;
for ( glob $glob ){
push( @list, $_) if -f;
findfile( $_.'/*' ) if -d;
}
}
| [reply] [d/l] |
Re: Recursive opendir without
by idnopheq (Chaplain) on Apr 11, 2001 at 03:10 UTC
|
Will File::Find not achive what you are trying to do?
Perhaps a perldoc File::Find is in order, and/or checkout recipe 9.7 in the Perl Cookbook.
HTH
Dex | [reply] |
Re: Recursive opendir without
by little (Curate) on Apr 11, 2001 at 03:10 UTC
|
Do you really need to keep the directory handle open? Cant you just push its content in an arry or save that in any other way? You don't need to keep the dir open if you want to open a subdir of that. And you might think about using File::Find
Have a nice day
All decision is left to your taste | [reply] |
|
It's a good idea but is it really necesary ? Does an open file/dir handler use more ressource than @array ?
I am sure File::Find is THE way, as jeffa, idnopheq and you said, but my topic was "Recursive opendir without File::Find" (and it was troncate. ???).
Thanks again.
BoBiOne
| [reply] |