X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlopentut.pod;h=6f7f77c43232dd4df0e88fcfbbb982824da8dba0;hb=cf5baa4869b9f9ab8e2d559f474c8e805806c370;hp=b4003f4f2efbec0e78f0fba38879b7d0137a33cc;hpb=0e06870bf080a38cda51c06c6612359afc2334e1;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlopentut.pod b/pod/perlopentut.pod index b4003f4..6f7f77c 100644 --- a/pod/perlopentut.pod +++ b/pod/perlopentut.pod @@ -89,7 +89,7 @@ command and open a write-only filehandle leading into that command. This lets you write into that handle and have what you write show up on that command's standard input. For example: - open(PRINTER, "| lpr -Plp1") || die "cannot fork: $!"; + open(PRINTER, "| lpr -Plp1") || die "can't run lpr: $!"; print PRINTER "stuff\n"; close(PRINTER) || die "can't close lpr: $!"; @@ -98,18 +98,20 @@ read-only filehandle leading out of that command. This lets whatever that command writes to its standard output show up on your handle for reading. For example: - open(NET, "netstat -i -n |") || die "cannot fork: $!"; + open(NET, "netstat -i -n |") || die "can't fun netstat: $!"; while () { } # do something with input close(NET) || die "can't close netstat: $!"; -What happens if you try to open a pipe to or from a non-existent command? -In most systems, such an C will not return an error. That's -because in the traditional C/C model, running the other -program happens only in the forked child process, which means that -the failed C can't be reflected in the return value of C. -Only a failed C shows up there. See -L -to see how to cope with this. There's also an explanation in L. +What happens if you try to open a pipe to or from a non-existent +command? If possible, Perl will detect the failure and set C<$!> as +usual. But if the command contains special shell characters, such as +C> or C<*>, called 'metacharacters', Perl does not execute the +command directly. Instead, Perl runs the shell, which then tries to +run the command. This means that it's the shell that gets the error +indication. In such a case, the C call will only indicate +failure if Perl can't even run the shell. See L to see how to cope with +this. There's also an explanation in L. If you would like to open a bidirectional pipe, the IPC::Open2 library will handle this for you. Check out @@ -738,7 +740,7 @@ sneaky data mutilation behind your back. Depending on the vicissitudes of your runtime system, even these calls may need C or C first. Systems known to be free of -such difficulties include Unix, the Mac OS, Plan9, and Inferno. +such difficulties include Unix, the Mac OS, Plan 9, and Inferno. =head2 File Locking @@ -765,7 +767,7 @@ uses locking and another doesn't, all bets are off. By default, the C call will block until a lock is granted. A request for a shared lock will be granted as soon as there is no -exclusive locker. A request for a exclusive lock will be granted as +exclusive locker. A request for an exclusive lock will be granted as soon as there is no locker of any kind. Locks are on file descriptors, not file names. You can't lock a file until you open it, and you can't hold on to a lock once the file has been closed. @@ -836,6 +838,42 @@ how to increment a number in a file safely: close(FH) or die "can't close numfile: $!"; +=head2 IO Layers + +In Perl 5.8.0 a new I/O framework called "PerlIO" was introduced. +This is a new "plumbing" for all the I/O happening in Perl; for the +most part everything will work just as it did, but PerlIO brought in +also some new features, like the capability of think of I/O as "layers". +One I/O layer may in addition to just moving the data also do +transformations on the data. Such transformations may include +compression and decompression, encryption and decryption, and transforming +between various character encodings. + +Full discussion about the features of PerlIO is out of scope for this +tutorial, but here is how to recognize the layers being used: + +=over 4 + +=item * + +The three-(or more)-argument form of C is being used and the +second argument contains something else in addition to the usual +C<< '<' >>, C<< '>' >>, C<< '>>' >>, C<< '|' >> and their variants, +for example: + + open(my $fh, "<:utf8", $fn); + +=item * + +The two-argument form of C is being used, for example + + binmode($fh, ":encoding(utf16)"); + +=back + +For more detailed discussion about PerlIO see L; +for more detailed discussion about Unicode and I/O see L. + =head1 SEE ALSO The C and C function in perlfunc(1); @@ -846,12 +884,8 @@ the POSIX documentation. Copyright 1998 Tom Christiansen. -When included as part of the Standard Version of Perl, or as part of -its complete documentation whether printed or otherwise, this work may -be distributed only under the terms of Perl's Artistic License. Any -distribution of this file or derivatives thereof outside of that -package require that special arrangements be made with copyright -holder. +This documentation is free; you can redistribute it and/or modify it +under the same terms as Perl itself. Irrespective of its distribution, all code examples in these files are hereby placed into the public domain. You are permitted and