Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re^2: How to process multiple input files?

by John M. Dlugosz (Monsignor)
on May 23, 2011 at 00:22 UTC ( #906218=note: print w/replies, xml ) Need Help??

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

I worry that an empty file will stop it prematurely. Or a file might contain just "0" or somesuch, but that's less likely. Since he's slurping whole files rather than reading lines, I think it would be prudent to test for defined. (Hmm, what does the normal line-oriented read do if an empty file is in the list? Maybe it's always an issue.)

update: never mind. In production code I would have simply written defined to be sure, but looking through the docs I see that this construct is special even in the case of explicit assignment. I know that the quick while(<>) tests for defined, or started to at some specific version of Perl (I remember the classic Camel book explaining how lines are never False because they end in "\n"), but wasn't sure that applied when assignment was being made.

In general, I rely less on special cases and magical meanings in well-written production code than in a quick one-liner. Declaring variables, and not using $_ much falls into the same category, so I somehow was thinking the magic was not in effect.

Replies are listed 'Best First'.
Re^3: How to process multiple input files?
by jwkrahn (Monsignor) on May 23, 2011 at 06:44 UTC
    I think it would be prudent to test for defined.

    The code I posted:

    while ( my $line = <> ) {

    does test for defined.

      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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://906218]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2018-07-19 20:52 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (420 votes). Check out past polls.