Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

RE: File Input and Output

by LeGo (Chaplain)
on Aug 02, 2000 at 18:02 UTC ( [id://25729]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://25729]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 09:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found