Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^3: How to process multiple input files?

by jwkrahn (Monsignor)
on May 23, 2011 at 06:44 UTC ( #906247=note: print w/replies, xml ) Need Help??

in reply to Re^2: How to process multiple input files?
in thread How to process multiple input files?

I think it would be prudent to test for defined.

The code I posted:

while ( my $line = <> ) {

does test for defined.

Replies are listed 'Best First'.
Re^4: How to process multiple input files?
by John M. Dlugosz (Monsignor) on May 23, 2011 at 17:22 UTC
    No, it tests for normal Truth. An undef, an empty string, a "0" etc. will all test as False after being assigned to $line.
      Check your answers, its easy :)
      $ perl -le " while ( my $line = <> ) { 1 } print $. " 0 0 0 0 ^Z 4 $ perl -MO=Deparse -le " while ( my $line = <> ) { 1 } print $. " BEGIN { $/ = "\n"; $\ = "\n"; } while (defined(my $line = <ARGV>)) { do { '???' }; } print $.; -e syntax OK
        It's not a problem with normal line-oriented use because the \n character is present on the end of every line with the possible exception of the last which then won't be empty. You got "0\n" in the string, not an empty string, not "0" alone.

        Try something like: while ( my $line = <> ) { say !!($line) } print $. and you'll see that each read is True as well as being defined.

        Recall as I explained originally, I'm concerned with totally empty files causing an empty string and testing false, or (less likely because it doesn't look like an xml file) a file containing a 0 only and no terminating end-of-line.

        However, I do stand corrected, after checking the perl IO reference: I was thinking that the special meaning of Truth applied only to implicit assignment to $_ in the condition. However, the 5.14 docs does indeed say "the assigned value (whether assignment is automatic or explicit) is then tested to see whether it is defined...".

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://906247]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2022-09-24 22:59 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (116 votes). Check out past polls.