On Sun, Apr 22, 2012 at 02:43:25PM -0700, Father Chrysostomos via RT wrote:
> On Sun Jan 15 12:51:08 2012, sprout wrote:
> > I also propose that we add an :iter attribute, that will allow custom
> > subs to get the special defined() treatment that glob and readline
> > get inside a while() condition. (while(glob "foo") is equivalent
> > to while(defined(glob "foo")).)
>
> Actually, it's equivalent to while(defined($_ = glob "foo")).
>
> Interestingly, while($x=<foo>) did not get the special treatment until
> commit 4b161ae29769, which I think was somewhere on the 5.004 maint branch.
>
> Commit 4b161ae29769 also made while($x=each %foo) get wrapped in
> defined(), but did not affect while(each %foo), which gets neither
> defined() nor $_=.
>
> Was $_= omitted to avoid backward-compatibility problems, or was it an
> oversight? Was it simply an oversight that defined() was omitted in
> that case?
I think that $_ = being ommitted on while(each %foo) is not an oversight
at that time.
That commit you quote gets me back to this message from Larry:
As usual, when there are long arguments, there are good arguments for both
sides (mixed in with the chaff). In this case, let's make
while ($x = <whatever>)
equivalent to
while (defined($x = <whatever>))
(But nothing more complicated than an assignment should assume defined().)
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-04/msg00133.html
Nick Ing-Simmons asks for a clarification:
Thanks Larry - that is what the patch I posted does.
But it also does the same for C<readdir>, C<each> and C<glob> -
i.e. the same cases that solicit the warning in 5.004 is extending
the defined insertion to those cases desirable?
(glob and readdir seem to make sense, I am less sure about each).
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-04/msg00182.html
(it's clarified in a later message that Nick I-S hadn't realised that each
in *scalar* context returns the keys, so it's an analogous iterator which
can't return undef for any entry)
In turn, the "RULING" dates back to this thread/request:
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-03/msg01630.html
which also generates a massive thread, of which this message is probably
most insightful:
Pray tell how you can tell that the code is broken without comments in the
code explaining what they expected it to do. while ($x = <>) is legal
code and works as documented. It does not use the idiom of while (<>).
You can't just say "this code is broken" about while ($x = <>) *in all
cases* without more context to indicate the expected behaviour.
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-03/msg01966.html
in turn that seems to be a reaction to these warnings added in 5.004
$ ./perl -lwe 'while ($a = each %a) {}'
Value of each() operator can be "0"; test with defined() at -e line 1.
[strictly as far as git has it, most this commit:
commit a60067777be62ee91d1318f9ae26d9ed713245de
Author: Perl 5 Porters <perl5-porters@africa.nicoh.com>
Date: Wed Jan 1 08:59:00 1997 +1200
[inseparable changes from patch from perl5.003_17 to perl5.003_18]
CORE LANGUAGE CHANGES
...
Subject: Warn on '{if,while} ($x = X)' where X is glob, readdir, or <FH>
From: Chip Salzenberg <chip@atlantic.net>
Files: op.c pod/perldiag.pod
...
as determined by
.../bisect.pl --end perl-5.004 -lwe 'BEGIN {$SIG{__WARN__} = sub {++$b}}; while ($a = <>) {++$a}; exit $b' </dev/null
and the each warning in this commit:
commit 68dc074516a6859e3424b48d1647bcb08b1a1a7d
Author: Perl 5 Porters <perl5-porters@africa.nicoh.com>
Date: Sun Mar 9 11:57:19 1997 +1200
[inseparable changes from match from perl-5.003_93 to perl-5.003_94]
...
Subject: Warn on C<while ($x = each %y) {}>
From: Chip Salzenberg <chip@perl.com>
Files: op.c pod/perldiag.pod
as determined by
.../bisect.pl --end perl-5.004 -lwe 'BEGIN {$SIG{__WARN__} = sub {++$b}}; while ($a = each %a) {}; exit $b'
]
The rational for the former commit is detailed in the thread starting here:
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/9612/msg02125.html
the latter in these messages
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1997-03/msg01263.html
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1997-03/msg01302.html
The intent of the changes appears to be to retain the 5.003 and earlier
behaviour on what gets assigned for each construction, but change the loop
behaviour to terminate on undefined rather than simply falsehood for the
common simple cases:
while (OP ...)
and
while ($var = OP ...)
The mail archives from 1997 are fascinating. Lots of messages. Lots of
design discussions, not always helpful. And some of the same unanswered
questions as today.
Nicholas Clark
Thread Previous
|
Thread Next