[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] <> warning



"William B. Birkett" <wbirkett@pcolor.com> writes:
>When I run a program containing the following lines,
>
>    while ($line = <IN_FILE>) {
>        print $line;
>    }
>
>I get the following warning,
>
># Value of <HANDLE> construct can be "0"; test with defined().
>File 'Untitled'; Line 10
>
>The reason I am using $line is that I have another file opened and don't
>want to alter the $_ variable. This construct is in the MacPerl P&E book,
>so I assume it is commonly used.

Yes it is, but those who use it don't use -w :-)

>The program executes properly, but I am bothered by the warning. What does
>the warning mean?

It means what it says, sort of: If a file ends with a "0" as the last character
(not followed by a newline character, a rather exotic occurrence), the while
loop will terminate.

>Is there a better way to write this code?

while (defined($line = <IN_FILE>)) {
}

BTW, the form

while (<IN_FILE>) {
}

already does this implicitly, and by the next release of perl, the form

while ($line = <IN_FILE>) {
}

will do this implictly as well; the warning above just represents an
intermediate stage in understanding this problem.

Matthias

-- 
Matthias Neeracher   <neeri@iis.ee.ethz.ch>   http://www.iis.ee.ethz.ch/~neeri
   "I'm set free to find a new illusion" -- Velvet Underground




***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch