Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

how do I "initialize" $_

by Anonymous Monk
on Oct 11, 2018 at 03:12 UTC ( #1223846=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This is among the basics. I thought I am trying to read in the first line of a file, and then print it.
#!/usr/bin/perl -w use strict; open (FILEHANDLE, '<', 'some.file') or die "Can't open some.file for r +eading.\n"; <FILEHANDLE>; print; close FILEHANDLE;
That produces:
Use of uninitialized value $_ in print at ./ line 6, <FILEHANDL +E> line 1.
What am I not understanding?

Replies are listed 'Best First'.
Re: how do I "initialize" $_
by AnomalousMonk (Chancellor) on Oct 11, 2018 at 04:51 UTC

    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; }
    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:  <%-{-{-{-<

      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.

Re: how do I "initialize" $_
by tybalt89 (Vicar) on Oct 11, 2018 at 03:22 UTC
Re: how do I "initialize" $_
by Marshall (Abbot) on Oct 11, 2018 at 04:43 UTC
    open (FILEHANDLE, '<', 'some.file') or die "Can't open some.file for r +eading.\n"; while (<FILEHANDLE>) { print; }
    Update: In general, I almost never have a statement like $_ = $var. I'm sure that I've done that, but in very,very special situations.

    To read the first line:

    my $first = <FILEHANDLE>;
Re: how do I "initialize" $_
by holli (Monsignor) on Oct 11, 2018 at 17:31 UTC
    Whatever learning material you use - if it teaches you to still use bareword FILEHANDLES in 2018 - it is outdated. What are you learning from?


    You can lead your users to water, but alas, you cannot drown them.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1223846]
Approved by marto
Front-paged by haukex
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2018-12-10 13:39 GMT
Find Nodes?
    Voting Booth?
    How many stories does it take before you've heard them all?

    Results (49 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!