Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Catching errors in closing lexical filehandles

by davido (Cardinal)
on Sep 27, 2004 at 06:10 UTC ( [id://394081]=note: print w/replies, xml ) Need Help??

in reply to Catching errors in closing lexical filehandles

Lexical filehandles have advantages beyond the fact that they close automagically when they fall out of scope. The truth of the matter is that in most cases it's fine to let input filehandles auto-close as they fall from scope, but probably a very good idea to explicity close filehandles that relate to output filehandles.

Other reasons for using lexical filehandles include many of the same reasons that you would use lexical variables instead of package globals, plus it just looks cleaner passing lexical filehandlefs into function parameter lists than when typeglobs are used, IMHO. And it's nice to let filehandles fall under the watchful eye of strictures just like any other lexical variable; that's there to help write less buggy code.

So use lexicals when appropriate not just because they auto-close, but for all the other benefits that lexical variables offer too.


  • Comment on Re: Catching errors in closing lexical filehandles

Replies are listed 'Best First'.
Re^2: Catching errors in closing lexical filehandles
by gaal (Parson) on Sep 27, 2004 at 06:18 UTC
    I used to use IO::File for the cleanliness back when "open my $fh" didn't work. Or when I didn't feel like the overhead sometimes I used gensym (remember that?). I'm aware of all those advantages and mentioned most of them in the original post; and I still like the idea of automatic close :)

    Can you explain why explicitly closing output filehandles is especially important? Modulo close failures, in our case the close *will* happen, together with associated flushing.

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-06-25 00:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.