SPUG: Directory Fun

Yitzchak Scott-Thoennes sthoenna at efn.org
Thu Mar 17 21:26:32 PST 2005


On Thu, Mar 17, 2005 at 02:30:45PM -0500, Perl wrote:
> sub test ($&@)
> {
>         $cnt += &$func($_)
>             for @dirs;
...
> #   For some reason this trashes the diretory list when it runs,
> #   so it must be last.  Probably a stupid programming error.

s/stupid/classic/ :)

> test 'glob', sub {
>     my  $dir = shift;
>     my  $cnt = 0;
> 
>     while (<$dir/$glb>) {
>         $cnt++;
>     }

That while implicitly assigns to $_, which the "for @dirs" is aliasing
to the elements of @dirs.  Always do something like:
   while (my $file=<$dir/$glb>) {
instead, or put a local *_; before the while (or a "my $_;" on 
5.9.2 and above).


More information about the spug-list mailing list