In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] (R Huber) writes: >Why does > ><>; >print; > >give the error "Use of uninitialized value in print" > >and > >while( <> ) { >print; >} > >not give that error
You've assumed that: <>; is short for: $_ = <>; whereas this is only true when the <> is the only thing inside the condition of a while loop. By itself (what we call "in void context"), it just reads a line and throws it away. So $_ has not been changed and if you never assigned anything to it before the <> line, is still undefined. Here's how it works: while (<>) gets turned into: while ($_ = <>) internally. But wait, there's more. An assignment to <> in the condition of a while loop is further modified internally, in this case to: while (defined($_ = <>)) Why? Because the result of <> could be a line that has a false value. If the line read in was "0" (not "0\n", which means it must be the last line of the input, but still, all lines count), then without the defined() test that line would be erroneously skipped. <> returns undef when there is no more input, hence that is what we must test for. And since we need to do that so often, Perl adds the shortcuts so we can type just: while (<>) to get what we want. -- Peter Scott http://www.perldebugged.com/ *** NEW *** http//www.perlmedic.com/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>