On Tue, 3 Feb 2004, r huber wrote:
> Greeting:
>
> When I execute the following:
>
> <>;
> print;
>
> I get the error message "Use of uninitialized value in
> print"
>
> but when I exectue:
>
> while (<>) {
> print;
> }
>
> I get what I type in echoed back to my screen.
>
> Why does the first snippet of code get an error
> message and the second not?
> Thanks,
> rj
This behaviour is normal and is documented on the Web at:
http://www.perldoc.com/perl5.8.0/pod/perlop.html#I-O-Operators
It says (quoting)
<quote>
Ordinarily you must assign the returned value to a variable, but there is
one situation when an automatic assignment happens. If and only if the
input symbol is the only thing inside the conditional of a while
statement (even if disguised as a for(;;) loop), the value is
automatically assigned to the global variable $_, destroying whatever was
there previously. (This may seem like an odd thing to you, but you'll use
the construct in almost every Perls script your write.) The $_ variable is
not implicitly localized. You'll have to put a local $_; before the loop
if you want that to happen.
</quote>
The above site is very handly to have up in your browser while writing
Perl code. At least, it is for me!
--
Maranatha!
John McKown
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>