I'm learning perl right now and I'm having a bit of a problem. I'm making a function that returns a list of the files (no directories) in a dir. It also has an option do do it recursively so the files in the subdirs also get in the list. The problem is that it won't work as it should when it's being recursive. It will only return the files of it's "first stop".
sub getdir {
my ($dirpath, $recurse) = @_;
my ($file, @dirlist);
if ($dirpath !~ /[\/\\]$/) {
$dirpath .= "/";
}
if (!opendir(DIR, $dirpath)) {
return 0;
}
while (defined($file = readdir(DIR))) {
next if $file =~ /^\.\.?$/;
$file = "$dirpath$file";
if ($recurse && -d $file) {
push (@dirlist, &getdir("$file/", 1));
}
elsif (! -d $file) {
push(@dirlist, "$file");
}
}
closedir(DIR);
return @dirlist;
}
Name:
Anonymous2005-07-30 14:24
you can use globs to traverse dirs
also
use File::Basename; #see perldoc File::Basename
sub what {
for my $file (<$path/*>) {
if ($file -d $file) {
} else {
}
}
Name:
Anonymous2005-07-30 15:44
>>1
DIR is a global variable
just make it `my $dir` and it will work
Name:
Anonymous2005-07-30 16:14
>>2
What is a glob? Sounds almost like blog backwards, which scares me.
>>3
I suspected something like that; the warning messages usually point that out. I read on some page that variables and handles work differently so I didn't think it would be possible.
Real programmers are so goth they shit machine code.
Name:
Anonymous2005-08-20 9:06
Real programmer = compiler?
Name:
Anonymous2005-08-20 9:35
>>18
Real programmers directly output signals to a processor.
Name:
Anonymous2005-08-20 18:04
Real programmers are sentient processors.
Name:
Anonymous2005-08-20 19:16
There was an autistic guy in my class that we used to call "the human compiler."
It wasn't as cool as it sounds though; it was a reference to his uncanny ability to complain loudly every time the lecturer made a trivial error such as forgetting a semicolon.
Name:
Anonymous2008-04-19 18:47
Fuck FORTRAN.
Learn a useful language like The Algorithmic Language Scheme.