I don't see anything wrong with doing it this way:
{
open my $fh, $filename or die "open: $!";
# do somthing with $fh
close $fh or die "close: $!"
}
Do you?
(update: BTW, according to perldoc, close returns true "only if IO buffers are successfully flushed". The relevance for closing an output file handle is obvious -- disks do fill up on occasion, and depending on your OS, there may be other things that get in the way of finishing output. But it's hard for me to imagine a case where this is important for an input file handle. Maybe if you're doing tricky stuff with input from sockets or processes, yeah, but in the typical case of a disk file or STDIN, the return from close shouldn't matter.)
(Another update, in response to your update: in the case of having multiple ways to exit the block, I usually make a habit of doing an explicit close on any sort of "premature return" condition involving an output file handle. Maybe that's an unnecessary compulsion on my part, but it just seems clean and coherent, like maintaining proper indentation.)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|