Now, we all know that while applies some magic when used with angular parens to improve its use as the Perl idiomatic iterator on filehandles:
and also (but less widely known):$ perl -MO=Deparse -e 'while (<>) {}' while (defined($_ = <ARGV>)) { (); } -e syntax OK
Now, we also know that the intepreter does its best applying some heuristics to decide whether those parens are meant to mean (no pun intended, heh!) readline or glob. But of course one most certainly wants the former within the clause of the while loop.$ perl -MO=Deparse -e 'while (my $line=<>) {}' while (defined(my $line = <ARGV>)) { (); } -e syntax OK
The point: however, what I've just unexpectedly found out is that the same magic applies also when the angular parens have the other meaning:
indeed:$ perl -MO=Deparse -e '@a=qw/a b c/; print while <@a>' @a = ('a', 'b', 'c'); use File::Glob (); print $_ while defined($_ = glob(join($", @a))); -e syntax OK
It appears to work, but is much more probable not to be what one would have meant in the first place. Thus it may mask a typo or a distraction, or more simply a cargo culted use, as it seems to me in the context where I saw this popping out.$ perl -le '@a=qw/a b c/; print while <@a>' a b c
So I wonder whether this is a (perhaps unavoidable) side-effect or if it is intentional, although I doubt about the latter possibility...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: C<while> magic also working with glob(): intentional or not?
by merlyn (Sage) on Sep 27, 2005 at 18:06 UTC | |
by Util (Priest) on Sep 27, 2005 at 18:43 UTC | |
by blazar (Canon) on Sep 28, 2005 at 08:39 UTC | |
|
Re: C<while> magic also working with glob(): intentional or not?
by Util (Priest) on Sep 27, 2005 at 18:39 UTC | |
by blazar (Canon) on Sep 28, 2005 at 08:05 UTC | |
|
Re: C<while> magic also working with glob(): intentional or not?
by chester (Hermit) on Sep 27, 2005 at 17:38 UTC |