Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

RE: File Input and Output

by LeGo (Chaplain)
on Aug 02, 2000 at 18:02 UTC ( #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


Comment on RE: File Input and Output
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-08-23 17:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (175 votes), past polls