Yes, BTW, your code:
<FILEHANDLE>;
just causes the first line to be discarded since it is not assigned to anything.
I guess as another point, with strict; and warnings; active, when reading from the predefined DATA file handle, you will get a warning if you don't have the while (defined (my $line =<DATA>)){} syntax, that is because a line with just "0" could evaluate to "false". On "real file handles" the action is a bit different - Perl is very good at "doing what I meant" and the defined() is implicit. Perl is a huge language and abounds with little fine differences. | [reply] [d/l] [select] |
you will get a warning if you don't have the while (defined (my $line =<DATA>)){} syntax, that is because a line with just "0" could evaluate to "false". On "real file handles" the action is a bit different
Sorry, but no, DATA is a real filehandle, there shouldn't be any differences. And in regards to this "Perl has changed how it deals with <DATA>", I can't verify that, the defined check seems to be implicit all the way back to Perl 5.6 (I haven't had a chance to look at older versions). And I haven't been able to produce a warning either, or get while (<DATA>) or while (my $line=<DATA>) to evaluate a line containing only 0 (no newline) as false, on 5.6 thru 5.28. Interestingly, the implicit defined check wasn't actually documented in detail until recently (5e979393c).
| [reply] [d/l] [select] |
| [reply] |
... with strict; and warnings; active, when reading from the predefined DATA file handle, you will get a warning if you don't have the while (defined (my $line =<DATA>)){} syntax ... a line with just "0" could evaluate to "false".
I'm confused about what you intended to say in the quoted text. A
while (<DATA>) { ... }
is always implicitly
while (defined($_ = <DATA>)) { ... }
or more fundamentally
while (defined($_ = readline(DATA))) { ... }
for DATA or any other filehandle. Even if a '0' with no newline is the last character in a file or __DATA__ or __END__ block, no warning will be printed. (IIRC, this became true in a very early, but still non-zero, sub-version of Perl 5 — or was it true even for 5.0.0?) (Update: The following code was tested under ActiveState Perl version 5.8.9.)
Another way to see this is by deparsing:
c:\@Work\Perl\monks\Marshall>perl -MO=Deparse,-p t_read_DATA_1.pl
use warnings;
use strict 'refs';
while (defined(($_ = <DATA>))) {
print($_);
}
__DATA__
line the first
second line
penultimate line is line 3
t_read_DATA_1.pl syntax OK
0
See O and B::Deparse. (I think the final "0" appears in console output before the "syntax OK" message because the "0" is not terminated by a newline and doesn't get flushed until the process ends. The "syntax" message may also be going to STDERR. Or something like that...)
Update: I really should have mentioned that the preceding code was run under ActiveState Perl version 5.8.9, the ancientest Perl I hold in captivity.
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
I tested this with my Perl
C:\Users\mmtho\Documents\PerlProjects>perl -v
This is perl 5, version 24, subversion 3 (v5.24.3) built for MSWin32-x
+64-multi-thread
(with 1 registered patch, see perl -V for more detail)
...
And I found out that Perl has changed how it deals with <DATA>.
This works with Perl 5.24:
#!/usr/bin/perl
use strict;
use warnings;
while (my $line = <DATA>)
{
print $line;
}
__DATA__
aBC
XYAS
| [reply] [d/l] [select] |