Warn about mixing signals and threads;
[p5sagit/p5-mst-13.2.git] / pod / perlopentut.pod
index 9cb9f67..6f7f77c 100644 (file)
@@ -73,8 +73,8 @@ from a different file, and forget to trim it before opening:
 This is not a bug, but a feature.  Because C<open> mimics the shell in
 its style of using redirection arrows to specify how to open the file, it
 also does so with respect to extra white space around the filename itself
-as well.  For accessing files with naughty names, see L<"Dispelling
-the Dweomer">.
+as well.  For accessing files with naughty names, see 
+L<"Dispelling the Dweomer">.
 
 =head2 Pipe Opens
 
@@ -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,22 +98,24 @@ 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 (<NET>) { }               # 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<open> will not return an error. That's
-because in the traditional C<fork>/C<exec> model, running the other
-program happens only in the forked child process, which means that
-the failed C<exec> can't be reflected in the return value of C<open>.
-Only a failed C<fork> shows up there.  See L<perlfaq8/"Why doesn't open()
-return an error when a pipe open fails?"> to see how to cope with this.
-There's also an explanation in L<perlipc>.
+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<E<gt>> 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<open> call will only indicate
+failure if Perl can't even run the shell.  See L<perlfaq8/"How can I
+capture STDERR from an external command?"> to see how to cope with
+this.  There's also an explanation in L<perlipc>.
 
 If you would like to open a bidirectional pipe, the IPC::Open2
-library will handle this for you.  Check out L<perlipc/"Bidirectional
-Communication with Another Process">
+library will handle this for you.  Check out 
+L<perlipc/"Bidirectional Communication with Another Process">
 
 =head2 The Minus File
 
@@ -126,8 +128,8 @@ access the standard output.
 If minus can be used as the default input or default output, what happens
 if you open a pipe into or out of minus?  What's the default command it
 would run?  The same script as you're currently running!  This is actually
-a stealth C<fork> hidden inside an C<open> call.  See L<perlipc/"Safe Pipe
-Opens"> for details.
+a stealth C<fork> hidden inside an C<open> call.  See 
+L<perlipc/"Safe Pipe Opens"> for details.
 
 =head2 Mixing Reads and Writes
 
@@ -309,7 +311,7 @@ C<O_DEFER>, C<O_SYNC>, C<O_ASYNC>, C<O_DSYNC>, C<O_RSYNC>,
 C<O_NOCTTY>, C<O_NDELAY> and C<O_LARGEFILE>.  Consult your open(2)
 manpage or its local equivalent for details.  (Note: starting from
 Perl release 5.6 the O_LARGEFILE flag, if available, is automatically
-added to the sysopen() flags because large files are the the default.)
+added to the sysopen() flags because large files are the default.)
 
 Here's how to use C<sysopen> to emulate the simple C<open> calls we had
 before.  We'll omit the C<|| die $!> checks for clarity, but make sure
@@ -684,9 +686,9 @@ also some high-level modules on CPAN that can help you with these games.
 Check out Term::ReadKey and Term::ReadLine.
 
 What else can you open?  To open a connection using sockets, you won't use
-one of Perl's two open functions.  See L<perlipc/"Sockets: Client/Server
-Communication"> for that.  Here's an example.  Once you have it,
-you can use FH as a bidirectional filehandle.
+one of Perl's two open functions.  See 
+L<perlipc/"Sockets: Client/Server Communication"> for that.  Here's an 
+example.  Once you have it, you can use FH as a bidirectional filehandle.
 
     use IO::Socket;
     local *FH = IO::Socket::INET->new("www.perl.com:80");
@@ -738,7 +740,7 @@ sneaky data mutilation behind your back.
 
 Depending on the vicissitudes of your runtime system, even these calls
 may need C<binmode> or C<O_BINARY> 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<flock> 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<open()> 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<binmode<open()> is being used, for example
+
+    binmode($fh, ":encoding(utf16)");
+
+=back
+
+For more detailed discussion about PerlIO see L<perlio>;
+for more detailed discussion about Unicode and I/O see L<perluniintro>.
+
 =head1 SEE ALSO 
 
 The C<open> and C<sysopen> 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