You have a precedence problem. Quoth B::Deparse your code parses as: print(((('number of files: ' . $size) - 2) . "\n"));. print takes a list so you should use a comma rather than the concatenation operator ..
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] [d/l] [select] |
thank you thank you thank you.
I spent a better half of today trying to figure it out.
I guess I'll read up on the . vs , :)
Thanks Again!
Theiss
| [reply] |
As id:Fletch says, using comma to provide a list to print elegantly solves the problem.
To make it clearer that it is purely a precedence issue though, you can fix your existing statement by making precedence explicit with brackets:
print "number of files: " . ($size-2) . "\n"; | [reply] [d/l] |
| [reply] [d/l] [select] |
-
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");
| [reply] [d/l] [select] |
I've always been partial to:
use File::Slurp;
my @files = read_dir( '/path/to/dir' ) ;
| [reply] [d/l] |