summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA. Sinan Unur <nanis@cpan.org>2010-10-03 21:30:50 -0400
committerJesse Vincent <jesse@bestpractical.com>2010-10-04 01:56:32 -0400
commitd7d7fefda1ec0b9784e8148011dbac52e088dd91 (patch)
treef2a0e52499180f91b7d5837cfd8a58ba41d98cc8
parentf8a8dd4929328c0c19bc1cfdf4c1b6e48abb0dcc (diff)
downloadperl-d7d7fefda1ec0b9784e8148011dbac52e088dd91.tar.gz
Further clarification on indirect filehandles
A recent discussion on Stackoverflow.com indicated to me that there is some potential for confusion in the "Indirect Filehandles" section in perlopentut.pod. See comments to http://stackoverflow.com/questions/3661161/writing-a-macro-in-perl/3661239#36612 39 The attached patch is my attempt at clarifying that indirect filehandles are closed when there are no more references to them rather than simply when the end of the current lexical scope is reached. I also added an example of returning such a filehandle from a subroutine. I am not sure if this is the best way to word it, so I would appreciate feedback. The patch is attached. -- Sinan
-rw-r--r--pod/perlopentut.pod19
1 files changed, 18 insertions, 1 deletions
diff --git a/pod/perlopentut.pod b/pod/perlopentut.pod
index ea4b307b45..2322907630 100644
--- a/pod/perlopentut.pod
+++ b/pod/perlopentut.pod
@@ -117,13 +117,30 @@ like C<my $infile>, there's no clash and no need to worry about future
conflicts.
Another convenient behavior is that an indirect filehandle automatically
-closes when it goes out of scope or when you undefine it:
+closes when there are no more references to it:
sub firstline {
open( my $in, shift ) && return scalar <$in>;
# no close() required
}
+Indirect filehandles also make it easy to pass filehandles to and return
+filehandles from subroutines:
+
+ for my $file ( qw(this.conf that.conf) ) {
+ my $fin = open_or_throw('<', $file);
+ process_conf( $fin );
+ # no close() needed
+ }
+
+ use Carp;
+ sub open_or_throw {
+ my ($mode, $filename) = @_;
+ open my $h, $mode, $filename
+ or croak "Could not open '$filename': $!";
+ return $h;
+ }
+
=head2 Pipe Opens
In C, when you want to open a file using the standard I/O library,