Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Design Question - OPEN/CLOSE FILE

Status
Not open for further replies.

shlomyb

Programmer
Nov 14, 2006
32
IL
Hi

I have a script that open a file , do some actionsa and close the file.

In the "do some actions" section I check some conditions and can exit the script with error(1) WITHOUT close the file ( cause in the script , I did not get there yet ...)

Does Perl close the file automitacally ?

OR should I create an EXIT sub that will close the file and then will exit(1) ?

Thanks( as usual :))
 
perl closes any open files when it exits.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
As steve says Perl closes any files it opens, but you may lose some data due to buffering.

I'd say it's good practice to close open files, before exiting, regardless. Also, if you meet an error condition, you can use the last keyword to exit file read loops gracefully in the event of an error, and exit the program normally.

Just my €0.02
--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top