Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: How to handle the error when we try to read an empty text file in perl

by BrowserUk (Patriarch)
on Jan 28, 2015 at 08:02 UTC ( [id://1114717]=note: print w/replies, xml ) Need Help??


in reply to How to handle the error when we try to read an empty text file in perl

The problem is when i try to read a file which is empty then it crashes (because the code try to read a file and it find nothing in the first line and code breaks with an error that couldnt open this file).

That makes no sense. If the file is empty the while loop will never be entered:

E:\test>dir Volume in drive E is New Volume Volume Serial Number is 1821-B83A Directory of E:\test 28/01/2015 08:10 <DIR> . 28/01/2015 08:10 <DIR> .. 28/01/2015 08:10 0 fred 1 File(s) 0 bytes 2 Dir(s) 1,624,181,088,256 bytes free E:\test>perl -E"while( <> ){ print 'got here'; }" fred

BTW, this is also nonsense:

open my $fh, '<', 'C:\ppp.txt', or die "Could not open file $!"; while (my $line = <$fh>) { foreach my $str ($line)

Iterating a foreach over a single scalar variable, means it will only ever iterate once. Ie. it is completely redundant.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-03-29 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found