in reply to why scalar(@array) is not numeric?
Note that the error indicated
"number of files: 4" isn't numeric
rather than
"4" isn't numeric
Subtracting 2 is not the way to go. Some directories on some file systems don't have "." and "..". It's better to just filter them out.
# Filter out "." and ".." my @array = grep !/^\.\.?\z/, readdir(DIR);
# Filter out all "hidden" files my @array = grep !/^\./, readdir(DIR);
There's no reason to use a global variable for your directory handle. Change
opendir (DIR, "/some/path"); my @array = grep !/^\.\.\z/, readdir(DIR); closedir (DIR);
to
opendir (my $dh, "/some/path"); my @array = grep !/^\.\.\z/, readdir($dh); closedir ($dh);
Finally, you should have some error checking, if only something minimalistic for opendir
opendir (my $dh, "/some/path") or die("opendir: $!\n");
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: why scalar(@array) is not numeric?
by ~~David~~ (Hermit) on Jul 17, 2009 at 19:12 UTC |