http://qs321.pair.com?node_id=25729


in reply to File Input and Output

I wanted to add a bit that I have learned to help you when you start to program and have errors.

It is always good to make sure the file you are opening opens. Because if it doesn't the program will just keep running with no errors and you will not get your desired results. The easiest way to do this is.

open (FILE, "file") || die "file could not be opened";
Then as you program more you will probably do something more like this in all of your programs.

$file = "some_file.txt"; open(FILE,"$file") or &Error($!);
And then at the end of your code where you put your subs put this.

sub Error { $error = shift or $error = "unknown"; print "Sorry, but there was an error. Could not open $file"; print "Error: $error"; &End; }
Another thing that is good to do is to make sure that your close is successful.
close FILE or die "Could not close $file";
And as before you can write special subroutines to help specify your error for closing. This is very helpful when writing CGI scripts and making them look good when they do not open or close files.

LeGo