Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: how do I "initialize" $_

by AnomalousMonk (Archbishop)
on Oct 11, 2018 at 04:51 UTC ( [id://1223850]=note: print w/replies, xml ) Need Help??


in reply to how do I "initialize" $_

The default scalar  $_ (see perlvar) is assigned to in some cases, but not all; you've chosen one of the "not all" cases. A while-loop like

while (<FILEHANDLE>) { print; }
is really
while (defined($_ = <FILEHANDLE>)) { print $_; }
and likewise
for (<FILEHANDLE>) { print; }
is
for $_ (<FILEHANDLE>) { print $_; }
(Note that the while-loop will read and print the filehandle line-by-line, but the for-loop will read all lines of the file at once (could be a lotta lines!) and then print the lines one-by-one.) See perlsyn.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: how do I "initialize" $_
by Anonymous Monk on Oct 11, 2018 at 06:46 UTC
    Thanks. I knew other ways of reading the data (assigning it a variable or using a loop), but I didn't know why the "automatic" assignment didn't work in this case, and you explained that the best.
      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.

        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).

        ... 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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1223850]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-24 11:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found