in reply to How to handle the error when we try to read an empty text file in perl
use strict; use warnings; open my $fh, '<', 'C:\scripts\vacuum.txt', or die "Could not open file + $!"; while (my $line = <$fh>) { print "line $. reading '$line'\n"; } #update: removed nonsense part as pointed by BrowserUk below
Also see sysopen to have a more granular approach opening files-e File exists. -z File has zero size (is empty). -s File has nonzero size (returns size in bytes). >perl -we "use strict; my $file = 'c:\scripts\vacuum.txt'; if (-e -s $ +file){print qq(it exists and has not zero lenght\n)} else {print qq(e +xists but zero lenght\n)}" exists but zero lenght >perl -we "use strict; my $file = 'c:\scripts\002.txt'; if (-e -s $fil +e){print qq(it exists and has not z ero lenght\n)} else {print qq(exists but zero lenght\n)}" it exists and has not zero lenght
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to handle the error when we try to read an empty text file in perl
by ppp (Acolyte) on Jan 28, 2015 at 09:08 UTC | |
by soonix (Chancellor) on Jan 28, 2015 at 10:05 UTC |