wageslave has asked for the wisdom of the Perl Monks concerning the following question:

The book Im learning perl from has very little information on the special variable $_ all it basically says is that if <STDIN> isnt assigned to a variable its assigned to $_ and it can be used with a foreach loop I've search some on the web but most of the tutorials I've run across have relativley little information on $_, although i've learned a bit more on it. one of the things I found out was that it works with a while loop, so I tried a few different ways of using it. but i cant figure out why it doesnt work for the second one and why the third one seems to be off by one every time it prints
print "first\n"; while (<STDIN>) { print; print "***\n"; last if(eof); } print "second\n"; while (<STDIN>!=eof) { print; print "***\n"; } print "\nthird\n"; while ((chomp($tst=<STDIN>))!=eof) {print $tst ; print "***\n"; }
I'm runing this on a windoze system. the first while statement works as I expect it to,basically you type something hit return it prints what you typed when you hit ctrl-z it exits.
the second one doesnt work, if you hit enter twice it exits out of the loop, from what I can guess is that $_ isn't being assigned, but no idea why it wont do the comparison .
The third one works buts its a lil off, after the second entry it prints stuff. from what I understand all 3 should produce the same output if someone could help me out and set me straight as to why the outputs are different I would greatly appreciate it

Replies are listed 'Best First'.
Re: question about $_ and eof
by wmono (Friar) on Mar 13, 2002 at 08:05 UTC

    Ah, welcome aboard, wageslave!

    To answer your question about $_, you can read that variable as "it". When you write something like print $_, you are saying "print it". You can, as you've discovered, leave the "it" out, and Perl will know what you mean.

    As for the problems you're having in your three code snippets, here's a quote from perldoc -f eof to get us started:

    eof FILEHANDLE eof () eof Returns 1 if the next read on FILEHANDLE will return end of file, or if FILEHANDLE is not open. FILEHANDLE may be an expression whose value gives the real filehandle. (Note that this function actually reads a character and then "ungetc"s it, so isn't very useful in an interactive context.) Do not read from a terminal file (or call "eof(FILEHANDLE)" on it) after end-of-file is reached. File types such as terminals may lose the end-of-file condition if you do.

    What I'd like to point out from there is that eof is not a value, but rather a function. So when you do <STDIN>!=eof, you compare <STDIN> with either true or false. That's not something that's very printable, or something that makes a lot of sense. This is also why your second loop doesn't work.

    Also, again from perldoc -f eof, I present the following snippet:

    Practical hint: you almost never need to use "eof" in Perl, because the input operators typically return "undef" when they run out of data, or if there was an error.

    Perl has a very practical view on what is true or false. When you do while (<STDIN>), you will stop when <STDIN> returns undef, which is considered false.

    Keeping all of this in mind, here's how I might re-write your loop:

    while (<STDIN>) { print; print "***\n"; }

    Alternatively, I might write:

    while (<STDIN>) { print $_, "***\n"; }

    Good luck in your adventures!

      Just to clarify one point about
      while (<STDIN>)
      Q: Why does this not stop on a blank line?

      A: <STDIN> is returning a newline character '\n', which is true. In practice, regardless of O/S, you never get the empty string or a bare 0, hence the only false value you encounter is undef.

        > A: <STDIN> is returning a newline character '\n', which
        > is true. In practice, regardless of O/S, you never get
        > the empty string or a bare 0, hence the only false value
        > you encounter is undef.

        I think, the construct while (<STDIN>) { is just another way to write:  while (defined ($_ = <STDIN>)){, not only for while ($_ = <STDIN>) {.

        The difference is not big; only if there is a 0 (=zero) on the last line of a file without \n, it can become essential. But with pure STDIN this case might never happen, only with files or the like...

        Best regards,
        perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"