diff options
Diffstat (limited to 'Master/tlpkg/tlperl/lib/pods/perlopentut.pod')
-rw-r--r-- | Master/tlpkg/tlperl/lib/pods/perlopentut.pod | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/Master/tlpkg/tlperl/lib/pods/perlopentut.pod b/Master/tlpkg/tlperl/lib/pods/perlopentut.pod index ea4b307b459..4bb43bffd76 100644 --- a/Master/tlpkg/tlperl/lib/pods/perlopentut.pod +++ b/Master/tlpkg/tlperl/lib/pods/perlopentut.pod @@ -55,7 +55,7 @@ If you prefer the low-punctuation version, you could write that this way: open RESULTS,"> runstats" or die "can't open runstats: $!"; open LOG, ">> logfile " or die "can't open logfile: $!"; -A few things to notice. First, the leading less-than is optional. +A few things to notice. First, the leading C<< < >> is optional. If omitted, Perl assumes that you want to open the file for reading. Note also that the first example uses the C<||> logical operator, and the @@ -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, |