What the grep addition does is filter the ouput of readdir so that @filenames only winds up with filenames that are "normal files", ie, not directories. Meaning that "." and ".." and any other sub-dirs are excluded from the list! ergo, no need for the logic test on ne "." and ".."! Of course you already see that the filenames are just the raw names and the dir path needs to be prepended when you use those names, even in this grep.opendir(DIRHANDLE, $testdir) or die "couldn't open directory: $testdir +"; @filenames = grep {-f "$testdir/$_"} readdir(DIRHANDLE); closedir(DIRHANDLE);
Perl grep is way cool and is often used to filter lists. If the last statement in the grep evaluates to "true" the input on right is passed to output on left.
another point: no need to use . concatenation op in many string cases. Perl will interpolate a $var inside of double quotes (like die statement above) and -f "$testdir/$_", the quotes are needed otherwise Perl would think that this is a division.
In reply to Re: This is why Perl is so frustrating
by Marshall
in thread This is why Perl is so frustrating
by eggmatters
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |