Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am working with several flat file databases, pulling info out of them and generating reports in HTML. I have been count the number of entries in each file, and if there are no entries, want to display a page stating that there are no entries for that file. I've written the script to do this, but if the script opens a file with no entries, the script fails. Can anyone tell me what I'm doing wrong here? Here is a snippet of my code:
$lines = 0; open (DATABASE, "$filename" || &ErrorMessage); while (sysread DATABASE, $buffer, 4096) { $lines += ($buffer =~ tr/\n//); } close(DATABASE); if ($lines == 0){ &NoReport; } sub NoReport { print "Content-type: text/html\n\n"; print<<EOL; <html> <head> <title>No Report</title> </head> <body bgcolor="white"> <h2> No information has been submitted for the report you have requested. </h2> </body> </html> EOL exit; }

I probably just overlooked something simple. Thanks for your time.

Replies are listed 'Best First'.
Re: subroutine not working
by runrig (Abbot) on Sep 11, 2001 at 01:08 UTC
    This does not look right. You are not checking the status of open:
    open (DATABASE, "$filename" || &ErrorMessage); # Did you mean this? You should also probably pass '$!' to # and include '$!' in ErrorMessage open (DATABASE, $filename) || &ErrorMessage;
    But if you're just checking for any content, why not just use -s $filename??