While reading a book on advanced Perl programming(1), I came across this code:
while (defined($s = <>)) {
...
Is there any special reason for using defined
here? The documentation for
perlop says:
In these loop constructs, the assigned value (whether assignment is automatic or explicit) is then tested to see whether it is defined. The defined test avoids problems where line has a string value that would be treated as false by Perl, for example a
""
or a"0"
with no trailing newline. If you really mean for such values to terminate the loop, they should be tested for explicitly: [...]
So, would there be a corner case or that's simply because the book is too old
and the automatic defined
test was added in a recent Perl version?
(1) Advanced Perl Programming, First Edition, Sriram Srinivasan. O'Reilly (1997)