Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: File Reading and Closing confusion

by repellent (Priest)
on Aug 08, 2010 at 23:31 UTC ( [id://853685]=note: print w/replies, xml ) Need Help??


in reply to Re: File Reading and Closing confusion
in thread File Reading and Closing confusion

    That said, no, those are not the same: while(<FH>){...} assigns to $_, but $line=<FH> assigns to $line. Other than that, they are the same.

Actually, a crucial difference is that while(<FH>){...} gets interpreted by Perl as:
while (defined($_ = <FH>)) { ... }

which effectively ends the while loop once there are no more lines to be read from the filehandle. With $line=<FH>, we need to check for defined-ness.

    my @lines = do { open my $fh, '<', $file or die "$file: $!\n"; <$fh> };

Another way:
my @lines = do { local @ARGV = $file; <> };

Replies are listed 'Best First'.
Re^3: File Reading and Closing confusion
by shmem (Chancellor) on Aug 09, 2010 at 00:23 UTC
    With $line=<FH>, we need to check for defined-ness.

    No. While there are lines read, they are defined since they have $/ attached. If $/ is undef, no point for while, since that's slurp mode. An empty string on the last line with no $/ is - no line.

      I was referring to being wary when $line=<FH> is used on its own. To illustrate:
      $ perl -e 'print "with_ending\nno_ending"' > a.txt $ perl -MData::Dumper open(FH, "<", "a.txt") or die $!; my $line1 = <FH>; my $line2 = <FH>; my $line3 = <FH>; close FH; $Data::Dumper::Useqq = 1; print Dumper [$line1, $line2, $line3]; __END__ $VAR1 = [ "with_ending\n", "no_end", undef ];

      We see that there is a possibility that a line read ($line3 in this case) may return undef. That's what I meant: we need to be wary that a line read will not always return defined.
        I was referring to being wary when $line=<FH> is used on its own.

        Yes, but while takes care of that - and we/you were talking about a while loop, weren't you?

        1.    while (<>) {
        ...
        
        which is Perl short-hand for the more explicitly written version:
        1.    LINE: while (defined($line = <ARGV>)) {
        ...
        

        But of course,

        We see that there is a possibility that a line read ($line3 in this case) may return undef.

        - if you read past EOF, for instance. Then readline returns undef:

        open(FH, "<", "a.txt") or die $!; my $line1 = <FH>; my $line2 = <FH>; print "eof!\n" if eof FH; my $line3 = <FH>; close FH; __END__ eof!
        That's what I meant: we need to be wary that a line read will not always return defined.

        That's exactly the condition when both the while($line = <FH>) { } and while(defined($line = <FH>)) { } loops terminate, so there's no difference in a while loop.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-25 10:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found