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

Hii thansk for answer plese see the updated question.
  • Comment on Re^2: How to handle the error when we try to read an empty text file in perl

Replies are listed 'Best First'.
Re^3: How to handle the error when we try to read an empty text file in perl
by soonix (Chancellor) on Jan 28, 2015 at 10:05 UTC
    I mean if file is empty then it should add the $address in ppp.txt's first line otherwise go to the while loop
    in this case, rewording and expanding Discipulus' example:
    my $file = 'C:\ppp.txt'; print "file '$file' "; if (-e $file) { print 'exists '; if (-z $file) { print 'but is empty'; } else { print 'and is not empty'; } } else { print q!doesn't exist!; } print "\n";
    you would have to move your code in like this (untested):
    my $file = 'C:/ppp.txt'; print "file '$file' "; if (-e $file) { print 'exists '; if (-z $file) { # print 'but is empty'; my $fh, '>>', $file, or die "Could not open file '$file' for +appending: $!"; print$fh $address . ' or whatever you want'; close $fh; } else { # print 'and is not empty'; ... (your "old" open and while loop) } } else { # print q!doesn't exist!; ... (either create it or print error message) } print "\n";