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