Plan 9 from Bell Labs’s /usr/web/sources/contrib/gabidiaz/root/sys/man/1perl/perlopentut

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sh \" Subsection heading
.br
.if t .Sp
.ne 5
.PP
\fB\\$1\fR
.PP
..
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  | will give a
.\" real vertical bar.  \*(C+ will give a nicer C++.  Capital omega is used to
.\" do unbreakable dashes and therefore won't be available.  \*(C` and \*(C'
.\" expand to `' in nroff, nothing in troff, for use with C<>.
.tr \(*W-|\(bv\*(Tr
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
'br\}
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.if \nF \{\
.    de IX
.    tm Index:\\$1\t\\n%\t"\\$2"
..
.    nr % 0
.    rr F
.\}
.\"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.hy 0
.if n .na
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear.  Run.  Save yourself.  No user-serviceable parts.
.    \" fudge factors for nroff and troff
.if n \{\
.    ds #H 0
.    ds #V .8m
.    ds #F .3m
.    ds #[ \f1
.    ds #] \fP
.\}
.if t \{\
.    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
.    ds #V .6m
.    ds #F 0
.    ds #[ \&
.    ds #] \&
.\}
.    \" simple accents for nroff and troff
.if n \{\
.    ds ' \&
.    ds ` \&
.    ds ^ \&
.    ds , \&
.    ds ~ ~
.    ds /
.\}
.if t \{\
.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
.    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
.    \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
.    \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
.    \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
.    ds : e
.    ds 8 ss
.    ds o a
.    ds d- d\h'-1'\(ga
.    ds D- D\h'-1'\(hy
.    ds th \o'bp'
.    ds Th \o'LP'
.    ds ae ae
.    ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "PERLOPENTUT 1"
.TH PERLOPENTUT 1 "2002-11-24" "perl v5.8.0" "Perl Programmers Reference Guide"
.SH "NAME"
perlopentut \- tutorial on opening things in Perl
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
Perl has two simple, built-in ways to open files: the shell way for
convenience, and the C way for precision.  The choice is yours.
.SH "Open A\*` la shell"
.IX Header "Open A` la shell"
Perl's \f(CW\*(C`open\*(C'\fR function was designed to mimic the way command-line
redirection in the shell works.  Here are some basic examples
from the shell:
.PP
.Vb 6
\&    $ myprogram file1 file2 file3
\&    $ myprogram    <  inputfile
\&    $ myprogram    >  outputfile
\&    $ myprogram    >> outputfile
\&    $ myprogram    |  otherprogram 
\&    $ otherprogram |  myprogram
.Ve
.PP
And here are some more advanced examples:
.PP
.Vb 4
\&    $ otherprogram      | myprogram f1 - f2
\&    $ otherprogram 2>&1 | myprogram -
\&    $ myprogram     <&3
\&    $ myprogram     >&4
.Ve
.PP
Programmers accustomed to constructs like those above can take comfort
in learning that Perl directly supports these familiar constructs using
virtually the same syntax as the shell.
.Sh "Simple Opens"
.IX Subsection "Simple Opens"
The \f(CW\*(C`open\*(C'\fR function takes two arguments: the first is a filehandle,
and the second is a single string comprising both what to open and how
to open it.  \f(CW\*(C`open\*(C'\fR returns true when it works, and when it fails,
returns a false value and sets the special variable $! to reflect
the system error.  If the filehandle was previously opened, it will
be implicitly closed first.
.PP
For example:
.PP
.Vb 4
\&    open(INFO,      "datafile") || die("can't open datafile: $!");
\&    open(INFO,   "<  datafile") || die("can't open datafile: $!");
\&    open(RESULTS,">  runstats") || die("can't open runstats: $!");
\&    open(LOG,    ">> logfile ") || die("can't open logfile:  $!");
.Ve
.PP
If you prefer the low-punctuation version, you could write that this way:
.PP
.Vb 3
\&    open INFO,   "<  datafile"  or die "can't open datafile: $!";
\&    open RESULTS,">  runstats"  or die "can't open runstats: $!";
\&    open LOG,    ">> logfile "  or die "can't open logfile:  $!";
.Ve
.PP
A few things to notice.  First, the leading less-than is optional.
If omitted, Perl assumes that you want to open the file for reading.
.PP
The other important thing to notice is that, just as in the shell,
any white space before or after the filename is ignored.  This is good,
because you wouldn't want these to do different things:
.PP
.Vb 3
\&    open INFO,   "<datafile"   
\&    open INFO,   "< datafile" 
\&    open INFO,   "<  datafile"
.Ve
.PP
Ignoring surround whitespace also helps for when you read a filename in
from a different file, and forget to trim it before opening:
.PP
.Vb 2
\&    $filename = <INFO>;         # oops, \en still there
\&    open(EXTRA, "< $filename") || die "can't open $filename: $!";
.Ve
.PP
This is not a bug, but a feature.  Because \f(CW\*(C`open\*(C'\fR 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\*(R".
.Sh "Pipe Opens"
.IX Subsection "Pipe Opens"
In C, when you want to open a file using the standard I/O library,
you use the \f(CW\*(C`fopen\*(C'\fR function, but when opening a pipe, you use the
\&\f(CW\*(C`popen\*(C'\fR function.  But in the shell, you just use a different redirection
character.  That's also the case for Perl.  The \f(CW\*(C`open\*(C'\fR call 
remains the same\*(--just its argument differs.  
.PP
If the leading character is a pipe symbol, \f(CW\*(C`open\*(C'\fR starts up a new
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:
.PP
.Vb 3
\&    open(PRINTER, "| lpr -Plp1")    || die "can't run lpr: $!";
\&    print PRINTER "stuff\en";
\&    close(PRINTER)                  || die "can't close lpr: $!";
.Ve
.PP
If the trailing character is a pipe, you start up a new command and open a
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:
.PP
.Vb 3
\&    open(NET, "netstat -i -n |")    || die "can't fun netstat: $!";
\&    while (<NET>) { }               # do something with input
\&    close(NET)                      || die "can't close netstat: $!";
.Ve
.PP
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 \f(CW$!\fR as
usual.  But if the command contains special shell characters, such as
\&\f(CW\*(C`>\*(C'\fR or \f(CW\*(C`*\*(C'\fR, 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 \f(CW\*(C`open\*(C'\fR call will only indicate
failure if Perl can't even run the shell.  See \*(L"How can I capture \s-1STDERR\s0 from an external command?\*(R" in perlfaq8 to see how to cope with
this.  There's also an explanation in perlipc.
.PP
If you would like to open a bidirectional pipe, the IPC::Open2
library will handle this for you.  Check out 
\&\*(L"Bidirectional Communication with Another Process\*(R" in perlipc
.Sh "The Minus File"
.IX Subsection "The Minus File"
Again following the lead of the standard shell utilities, Perl's
\&\f(CW\*(C`open\*(C'\fR function treats a file whose name is a single minus, \*(L"\-\*(R", in a
special way.  If you open minus for reading, it really means to access
the standard input.  If you open minus for writing, it really means to
access the standard output.
.PP
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 \f(CW\*(C`fork\*(C'\fR hidden inside an \f(CW\*(C`open\*(C'\fR call.  See 
\&\*(L"Safe Pipe Opens\*(R" in perlipc for details.
.Sh "Mixing Reads and Writes"
.IX Subsection "Mixing Reads and Writes"
It is possible to specify both read and write access.  All you do is
add a \*(L"+\*(R" symbol in front of the redirection.  But as in the shell,
using a less-than on a file never creates a new file; it only opens an
existing one.  On the other hand, using a greater-than always clobbers
(truncates to zero length) an existing file, or creates a brand-new one
if there isn't an old one.  Adding a \*(L"+\*(R" for read-write doesn't affect
whether it only works on existing files or always clobbers existing ones.
.PP
.Vb 2
\&    open(WTMP, "+< /usr/adm/wtmp") 
\&        || die "can't open /usr/adm/wtmp: $!";
.Ve
.PP
.Vb 2
\&    open(SCREEN, "+> /tmp/lkscreen")
\&        || die "can't open /tmp/lkscreen: $!";
.Ve
.PP
.Vb 2
\&    open(LOGFILE, "+>> /tmp/applog"
\&        || die "can't open /tmp/applog: $!";
.Ve
.PP
The first one won't create a new file, and the second one will always
clobber an old one.  The third one will create a new file if necessary
and not clobber an old one, and it will allow you to read at any point
in the file, but all writes will always go to the end.  In short,
the first case is substantially more common than the second and third
cases, which are almost always wrong.  (If you know C, the plus in
Perl's \f(CW\*(C`open\*(C'\fR is historically derived from the one in C's fopen(3S),
which it ultimately calls.)
.PP
In fact, when it comes to updating a file, unless you're working on
a binary file as in the \s-1WTMP\s0 case above, you probably don't want to
use this approach for updating.  Instead, Perl's \fB\-i\fR flag comes to
the rescue.  The following command takes all the C, \*(C+, or yacc source
or header files and changes all their foo's to bar's, leaving
the old version in the original file name with a \*(L".orig\*(R" tacked
on the end:
.PP
.Vb 1
\&    $ perl -i.orig -pe 's/\ebfoo\eb/bar/g' *.[Cchy]
.Ve
.PP
This is a short cut for some renaming games that are really
the best way to update textfiles.  See the second question in 
perlfaq5 for more details.
.Sh "Filters"
.IX Subsection "Filters"
One of the most common uses for \f(CW\*(C`open\*(C'\fR is one you never
even notice.  When you process the \s-1ARGV\s0 filehandle using
\&\f(CW\*(C`<ARGV>\*(C'\fR, Perl actually does an implicit open 
on each file in \f(CW@ARGV\fR.  Thus a program called like this:
.PP
.Vb 1
\&    $ myprogram file1 file2 file3
.Ve
.PP
Can have all its files opened and processed one at a time
using a construct no more complex than:
.PP
.Vb 3
\&    while (<>) {
\&        # do something with $_
\&    }
.Ve
.PP
If \f(CW@ARGV\fR is empty when the loop first begins, Perl pretends you've opened
up minus, that is, the standard input.  In fact, \f(CW$ARGV\fR, the currently
open file during \f(CW\*(C`<ARGV>\*(C'\fR processing, is even set to \*(L"\-\*(R"
in these circumstances.
.PP
You are welcome to pre-process your \f(CW@ARGV\fR before starting the loop to
make sure it's to your liking.  One reason to do this might be to remove
command options beginning with a minus.  While you can always roll the
simple ones by hand, the Getopts modules are good for this.
.PP
.Vb 1
\&    use Getopt::Std;
.Ve
.PP
.Vb 2
\&    # -v, -D, -o ARG, sets $opt_v, $opt_D, $opt_o
\&    getopts("vDo:");
.Ve
.PP
.Vb 2
\&    # -v, -D, -o ARG, sets $args{v}, $args{D}, $args{o}
\&    getopts("vDo:", \e%args);
.Ve
.PP
Or the standard Getopt::Long module to permit named arguments:
.PP
.Vb 5
\&    use Getopt::Long;
\&    GetOptions( "verbose"  => \e$verbose,        # --verbose
\&                "Debug"    => \e$debug,          # --Debug
\&                "output=s" => \e$output );       
\&            # --output=somestring or --output somestring
.Ve
.PP
Another reason for preprocessing arguments is to make an empty
argument list default to all files:
.PP
.Vb 1
\&    @ARGV = glob("*") unless @ARGV;
.Ve
.PP
You could even filter out all but plain, text files.  This is a bit
silent, of course, and you might prefer to mention them on the way.
.PP
.Vb 1
\&    @ARGV = grep { -f && -T } @ARGV;
.Ve
.PP
If you're using the \fB\-n\fR or \fB\-p\fR command-line options, you
should put changes to \f(CW@ARGV\fR in a \f(CW\*(C`BEGIN{}\*(C'\fR block.
.PP
Remember that a normal \f(CW\*(C`open\*(C'\fR has special properties, in that it might
call fopen(3S) or it might called popen(3S), depending on what its
argument looks like; that's why it's sometimes called \*(L"magic open\*(R".
Here's an example:
.PP
.Vb 3
\&    $pwdinfo = `domainname` =~ /^(\e(none\e))?$/
\&                    ? '< /etc/passwd'
\&                    : 'ypcat passwd |';
.Ve
.PP
.Vb 2
\&    open(PWD, $pwdinfo)                 
\&                or die "can't open $pwdinfo: $!";
.Ve
.PP
This sort of thing also comes into play in filter processing.  Because
\&\f(CW\*(C`<ARGV>\*(C'\fR processing employs the normal, shell-style Perl \f(CW\*(C`open\*(C'\fR,
it respects all the special things we've already seen:
.PP
.Vb 1
\&    $ myprogram f1 "cmd1|" - f2 "cmd2|" f3 < tmpfile
.Ve
.PP
That program will read from the file \fIf1\fR, the process \fIcmd1\fR, standard
input (\fItmpfile\fR in this case), the \fIf2\fR file, the \fIcmd2\fR command,
and finally the \fIf3\fR file.
.PP
Yes, this also means that if you have a file named \*(L"\-\*(R" (and so on) in
your directory, that they won't be processed as literal files by \f(CW\*(C`open\*(C'\fR.
You'll need to pass them as \*(L"./\-\*(R" much as you would for the \fIrm\fR program.
Or you could use \f(CW\*(C`sysopen\*(C'\fR as described below.
.PP
One of the more interesting applications is to change files of a certain
name into pipes.  For example, to autoprocess gzipped or compressed
files by decompressing them with \fIgzip\fR:
.PP
.Vb 1
\&    @ARGV = map { /^\e.(gz|Z)$/ ? "gzip -dc $_ |" : $_  } @ARGV;
.Ve
.PP
Or, if you have the \fI\s-1GET\s0\fR program installed from \s-1LWP\s0,
you can fetch URLs before processing them:
.PP
.Vb 1
\&    @ARGV = map { m#^\ew+://# ? "GET $_ |" : $_ } @ARGV;
.Ve
.PP
It's not for nothing that this is called magic \f(CW\*(C`<ARGV>\*(C'\fR.
Pretty nifty, eh?
.SH "Open A\*` la C"
.IX Header "Open A` la C"
If you want the convenience of the shell, then Perl's \f(CW\*(C`open\*(C'\fR is
definitely the way to go.  On the other hand, if you want finer precision
than C's simplistic fopen(3S) provides, then you should look to Perl's
\&\f(CW\*(C`sysopen\*(C'\fR, which is a direct hook into the \fIopen\fR\|(2) system call.
That does mean it's a bit more involved, but that's the price of 
precision.
.PP
\&\f(CW\*(C`sysopen\*(C'\fR takes 3 (or 4) arguments.
.PP
.Vb 1
\&    sysopen HANDLE, PATH, FLAGS, [MASK]
.Ve
.PP
The \s-1HANDLE\s0 argument is a filehandle just as with \f(CW\*(C`open\*(C'\fR.  The \s-1PATH\s0 is
a literal path, one that doesn't pay attention to any greater-thans or
less-thans or pipes or minuses, nor ignore white space.  If it's there,
it's part of the path.  The \s-1FLAGS\s0 argument contains one or more values
derived from the Fcntl module that have been or'd together using the
bitwise \*(L"|\*(R" operator.  The final argument, the \s-1MASK\s0, is optional; if
present, it is combined with the user's current umask for the creation
mode of the file.  You should usually omit this.
.PP
Although the traditional values of read\-only, write\-only, and read-write
are 0, 1, and 2 respectively, this is known not to hold true on some
systems.  Instead, it's best to load in the appropriate constants first
from the Fcntl module, which supplies the following standard flags:
.PP
.Vb 8
\&    O_RDONLY            Read only
\&    O_WRONLY            Write only
\&    O_RDWR              Read and write
\&    O_CREAT             Create the file if it doesn't exist
\&    O_EXCL              Fail if the file already exists
\&    O_APPEND            Append to the file
\&    O_TRUNC             Truncate the file
\&    O_NONBLOCK          Non-blocking access
.Ve
.PP
Less common flags that are sometimes available on some operating
systems include \f(CW\*(C`O_BINARY\*(C'\fR, \f(CW\*(C`O_TEXT\*(C'\fR, \f(CW\*(C`O_SHLOCK\*(C'\fR, \f(CW\*(C`O_EXLOCK\*(C'\fR,
\&\f(CW\*(C`O_DEFER\*(C'\fR, \f(CW\*(C`O_SYNC\*(C'\fR, \f(CW\*(C`O_ASYNC\*(C'\fR, \f(CW\*(C`O_DSYNC\*(C'\fR, \f(CW\*(C`O_RSYNC\*(C'\fR,
\&\f(CW\*(C`O_NOCTTY\*(C'\fR, \f(CW\*(C`O_NDELAY\*(C'\fR and \f(CW\*(C`O_LARGEFILE\*(C'\fR.  Consult your \fIopen\fR\|(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 \fIsysopen()\fR flags because large files are the default.)
.PP
Here's how to use \f(CW\*(C`sysopen\*(C'\fR to emulate the simple \f(CW\*(C`open\*(C'\fR calls we had
before.  We'll omit the \f(CW\*(C`|| die $!\*(C'\fR checks for clarity, but make sure
you always check the return values in real code.  These aren't quite
the same, since \f(CW\*(C`open\*(C'\fR will trim leading and trailing white space,
but you'll get the idea:
.PP
To open a file for reading:
.PP
.Vb 2
\&    open(FH, "< $path");
\&    sysopen(FH, $path, O_RDONLY);
.Ve
.PP
To open a file for writing, creating a new file if needed or else truncating
an old file:
.PP
.Vb 2
\&    open(FH, "> $path");
\&    sysopen(FH, $path, O_WRONLY | O_TRUNC | O_CREAT);
.Ve
.PP
To open a file for appending, creating one if necessary:
.PP
.Vb 2
\&    open(FH, ">> $path");
\&    sysopen(FH, $path, O_WRONLY | O_APPEND | O_CREAT);
.Ve
.PP
To open a file for update, where the file must already exist:
.PP
.Vb 2
\&    open(FH, "+< $path");
\&    sysopen(FH, $path, O_RDWR);
.Ve
.PP
And here are things you can do with \f(CW\*(C`sysopen\*(C'\fR that you cannot do with
a regular \f(CW\*(C`open\*(C'\fR.  As you see, it's just a matter of controlling the
flags in the third argument.
.PP
To open a file for writing, creating a new file which must not previously
exist:
.PP
.Vb 1
\&    sysopen(FH, $path, O_WRONLY | O_EXCL | O_CREAT);
.Ve
.PP
To open a file for appending, where that file must already exist:
.PP
.Vb 1
\&    sysopen(FH, $path, O_WRONLY | O_APPEND);
.Ve
.PP
To open a file for update, creating a new file if necessary:
.PP
.Vb 1
\&    sysopen(FH, $path, O_RDWR | O_CREAT);
.Ve
.PP
To open a file for update, where that file must not already exist:
.PP
.Vb 1
\&    sysopen(FH, $path, O_RDWR | O_EXCL | O_CREAT);
.Ve
.PP
To open a file without blocking, creating one if necessary:
.PP
.Vb 1
\&    sysopen(FH, $path, O_WRONLY | O_NONBLOCK | O_CREAT);
.Ve
.Sh "Permissions A\*` la mode"
.IX Subsection "Permissions A` la mode"
If you omit the \s-1MASK\s0 argument to \f(CW\*(C`sysopen\*(C'\fR, Perl uses the octal value
0666.  The normal \s-1MASK\s0 to use for executables and directories should
be 0777, and for anything else, 0666.
.PP
Why so permissive?  Well, it isn't really.  The \s-1MASK\s0 will be modified
by your process's current \f(CW\*(C`umask\*(C'\fR.  A umask is a number representing
\&\fIdisabled\fR permissions bits; that is, bits that will not be turned on
in the created files' permissions field.
.PP
For example, if your \f(CW\*(C`umask\*(C'\fR were 027, then the 020 part would
disable the group from writing, and the 007 part would disable others
from reading, writing, or executing.  Under these conditions, passing
\&\f(CW\*(C`sysopen\*(C'\fR 0666 would create a file with mode 0640, since \f(CW\*(C`0666 &~ 027\*(C'\fR
is 0640.
.PP
You should seldom use the \s-1MASK\s0 argument to \f(CW\*(C`sysopen()\*(C'\fR.  That takes
away the user's freedom to choose what permission new files will have.
Denying choice is almost always a bad thing.  One exception would be for
cases where sensitive or private data is being stored, such as with mail
folders, cookie files, and internal temporary files.
.SH "Obscure Open Tricks"
.IX Header "Obscure Open Tricks"
.Sh "Re-Opening Files (dups)"
.IX Subsection "Re-Opening Files (dups)"
Sometimes you already have a filehandle open, and want to make another
handle that's a duplicate of the first one.  In the shell, we place an
ampersand in front of a file descriptor number when doing redirections.
For example, \f(CW\*(C`2>&1\*(C'\fR makes descriptor 2 (that's \s-1STDERR\s0 in Perl)
be redirected into descriptor 1 (which is usually Perl's \s-1STDOUT\s0).
The same is essentially true in Perl: a filename that begins with an
ampersand is treated instead as a file descriptor if a number, or as a
filehandle if a string.
.PP
.Vb 2
\&    open(SAVEOUT, ">&SAVEERR") || die "couldn't dup SAVEERR: $!";
\&    open(MHCONTEXT, "<&4")     || die "couldn't dup fd4: $!";
.Ve
.PP
That means that if a function is expecting a filename, but you don't
want to give it a filename because you already have the file open, you
can just pass the filehandle with a leading ampersand.  It's best to
use a fully qualified handle though, just in case the function happens
to be in a different package:
.PP
.Vb 1
\&    somefunction("&main::LOGFILE");
.Ve
.PP
This way if \fIsomefunction()\fR is planning on opening its argument, it can
just use the already opened handle.  This differs from passing a handle,
because with a handle, you don't open the file.  Here you have something
you can pass to open.
.PP
If you have one of those tricky, newfangled I/O objects that the \*(C+
folks are raving about, then this doesn't work because those aren't a
proper filehandle in the native Perl sense.  You'll have to use \fIfileno()\fR
to pull out the proper descriptor number, assuming you can:
.PP
.Vb 4
\&    use IO::Socket;
\&    $handle = IO::Socket::INET->new("www.perl.com:80");
\&    $fd = $handle->fileno;
\&    somefunction("&$fd");  # not an indirect function call
.Ve
.PP
It can be easier (and certainly will be faster) just to use real
filehandles though:
.PP
.Vb 4
\&    use IO::Socket;
\&    local *REMOTE = IO::Socket::INET->new("www.perl.com:80");
\&    die "can't connect" unless defined(fileno(REMOTE));
\&    somefunction("&main::REMOTE");
.Ve
.PP
If the filehandle or descriptor number is preceded not just with a simple
\&\*(L"&\*(R" but rather with a \*(L"&=\*(R" combination, then Perl will not create a
completely new descriptor opened to the same place using the \fIdup\fR\|(2)
system call.  Instead, it will just make something of an alias to the
existing one using the fdopen(3S) library call  This is slightly more
parsimonious of systems resources, although this is less a concern
these days.  Here's an example of that:
.PP
.Vb 2
\&    $fd = $ENV{"MHCONTEXTFD"};
\&    open(MHCONTEXT, "<&=$fd")   or die "couldn't fdopen $fd: $!";
.Ve
.PP
If you're using magic \f(CW\*(C`<ARGV>\*(C'\fR, you could even pass in as a
command line argument in \f(CW@ARGV\fR something like \f(CW"<&=$MHCONTEXTFD"\fR,
but we've never seen anyone actually do this.
.Sh "Dispelling the Dweomer"
.IX Subsection "Dispelling the Dweomer"
Perl is more of a DWIMmer language than something like Java\*(--where \s-1DWIM\s0
is an acronym for \*(L"do what I mean\*(R".  But this principle sometimes leads
to more hidden magic than one knows what to do with.  In this way, Perl
is also filled with \fIdweomer\fR, an obscure word meaning an enchantment.
Sometimes, Perl's DWIMmer is just too much like dweomer for comfort.
.PP
If magic \f(CW\*(C`open\*(C'\fR is a bit too magical for you, you don't have to turn
to \f(CW\*(C`sysopen\*(C'\fR.  To open a file with arbitrary weird characters in
it, it's necessary to protect any leading and trailing whitespace.
Leading whitespace is protected by inserting a \f(CW"./"\fR in front of a
filename that starts with whitespace.  Trailing whitespace is protected
by appending an \s-1ASCII\s0 \s-1NUL\s0 byte (\f(CW"\e0"\fR) at the end off the string.
.PP
.Vb 2
\&    $file =~ s#^(\es)#./$1#;
\&    open(FH, "< $file\e0")   || die "can't open $file: $!";
.Ve
.PP
This assumes, of course, that your system considers dot the current
working directory, slash the directory separator, and disallows \s-1ASCII\s0
NULs within a valid filename.  Most systems follow these conventions,
including all \s-1POSIX\s0 systems as well as proprietary Microsoft systems.
The only vaguely popular system that doesn't work this way is the
proprietary Macintosh system, which uses a colon where the rest of us
use a slash.  Maybe \f(CW\*(C`sysopen\*(C'\fR isn't such a bad idea after all.
.PP
If you want to use \f(CW\*(C`<ARGV>\*(C'\fR processing in a totally boring
and non-magical way, you could do this first:
.PP
.Vb 10
\&    #   "Sam sat on the ground and put his head in his hands.  
\&    #   'I wish I had never come here, and I don't want to see 
\&    #   no more magic,' he said, and fell silent."
\&    for (@ARGV) { 
\&        s#^([^./])#./$1#;
\&        $_ .= "\e0";
\&    } 
\&    while (<>) {  
\&        # now process $_
\&    }
.Ve
.PP
But be warned that users will not appreciate being unable to use \*(L"\-\*(R"
to mean standard input, per the standard convention.
.Sh "Paths as Opens"
.IX Subsection "Paths as Opens"
You've probably noticed how Perl's \f(CW\*(C`warn\*(C'\fR and \f(CW\*(C`die\*(C'\fR functions can
produce messages like:
.PP
.Vb 1
\&    Some warning at scriptname line 29, <FH> line 7.
.Ve
.PP
That's because you opened a filehandle \s-1FH\s0, and had read in seven records
from it.  But what was the name of the file, not the handle?
.PP
If you aren't running with \f(CW\*(C`strict refs\*(C'\fR, or if you've turn them off
temporarily, then all you have to do is this:
.PP
.Vb 4
\&    open($path, "< $path") || die "can't open $path: $!";
\&    while (<$path>) {
\&        # whatever
\&    }
.Ve
.PP
Since you're using the pathname of the file as its handle,
you'll get warnings more like
.PP
.Vb 1
\&    Some warning at scriptname line 29, </etc/motd> line 7.
.Ve
.Sh "Single Argument Open"
.IX Subsection "Single Argument Open"
Remember how we said that Perl's open took two arguments?  That was a
passive prevarication.  You see, it can also take just one argument.
If and only if the variable is a global variable, not a lexical, you
can pass \f(CW\*(C`open\*(C'\fR just one argument, the filehandle, and it will 
get the path from the global scalar variable of the same name.
.PP
.Vb 5
\&    $FILE = "/etc/motd";
\&    open FILE or die "can't open $FILE: $!";
\&    while (<FILE>) {
\&        # whatever
\&    }
.Ve
.PP
Why is this here?  Someone has to cater to the hysterical porpoises.
It's something that's been in Perl since the very beginning, if not
before.
.Sh "Playing with \s-1STDIN\s0 and \s-1STDOUT\s0"
.IX Subsection "Playing with STDIN and STDOUT"
One clever move with \s-1STDOUT\s0 is to explicitly close it when you're done
with the program.
.PP
.Vb 1
\&    END { close(STDOUT) || die "can't close stdout: $!" }
.Ve
.PP
If you don't do this, and your program fills up the disk partition due
to a command line redirection, it won't report the error exit with a
failure status.
.PP
You don't have to accept the \s-1STDIN\s0 and \s-1STDOUT\s0 you were given.  You are
welcome to reopen them if you'd like.
.PP
.Vb 2
\&    open(STDIN, "< datafile")
\&        || die "can't open datafile: $!";
.Ve
.PP
.Vb 2
\&    open(STDOUT, "> output")
\&        || die "can't open output: $!";
.Ve
.PP
And then these can be read directly or passed on to subprocesses.
This makes it look as though the program were initially invoked
with those redirections from the command line.
.PP
It's probably more interesting to connect these to pipes.  For example:
.PP
.Vb 3
\&    $pager = $ENV{PAGER} || "(less || more)";
\&    open(STDOUT, "| $pager")
\&        || die "can't fork a pager: $!";
.Ve
.PP
This makes it appear as though your program were called with its stdout
already piped into your pager.  You can also use this kind of thing
in conjunction with an implicit fork to yourself.  You might do this
if you would rather handle the post processing in your own program,
just in a different process:
.PP
.Vb 4
\&    head(100);
\&    while (<>) {
\&        print;
\&    }
.Ve
.PP
.Vb 10
\&    sub head {
\&        my $lines = shift || 20;
\&        return unless $pid = open(STDOUT, "|-");
\&        die "cannot fork: $!" unless defined $pid;
\&        while (<STDIN>) {
\&            print;
\&            last if --$lines < 0;
\&        } 
\&        exit;
\&    }
.Ve
.PP
This technique can be applied to repeatedly push as many filters on your
output stream as you wish.
.SH "Other I/O Issues"
.IX Header "Other I/O Issues"
These topics aren't really arguments related to \f(CW\*(C`open\*(C'\fR or \f(CW\*(C`sysopen\*(C'\fR,
but they do affect what you do with your open files.
.Sh "Opening Non-File Files"
.IX Subsection "Opening Non-File Files"
When is a file not a file?  Well, you could say when it exists but
isn't a plain file.   We'll check whether it's a symbolic link first,
just in case.
.PP
.Vb 3
\&    if (-l $file || ! -f _) {
\&        print "$file is not a plain file\en";
\&    }
.Ve
.PP
What other kinds of files are there than, well, files?  Directories,
symbolic links, named pipes, Unix-domain sockets, and block and character
devices.  Those are all files, too\*(--just not \fIplain\fR files.  This isn't
the same issue as being a text file. Not all text files are plain files.
Not all plain files are textfiles.  That's why there are separate \f(CW\*(C`\-f\*(C'\fR
and \f(CW\*(C`\-T\*(C'\fR file tests.
.PP
To open a directory, you should use the \f(CW\*(C`opendir\*(C'\fR function, then
process it with \f(CW\*(C`readdir\*(C'\fR, carefully restoring the directory 
name if necessary:
.PP
.Vb 5
\&    opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
\&    while (defined($file = readdir(DIR))) {
\&        # do something with "$dirname/$file"
\&    }
\&    closedir(DIR);
.Ve
.PP
If you want to process directories recursively, it's better to use the
File::Find module.  For example, this prints out all files recursively,
add adds a slash to their names if the file is a directory.
.PP
.Vb 3
\&    @ARGV = qw(.) unless @ARGV;
\&    use File::Find;
\&    find sub { print $File::Find::name, -d && '/', "\en" }, @ARGV;
.Ve
.PP
This finds all bogus symbolic links beneath a particular directory:
.PP
.Vb 1
\&    find sub { print "$File::Find::name\en" if -l && !-e }, $dir;
.Ve
.PP
As you see, with symbolic links, you can just pretend that it is
what it points to.  Or, if you want to know \fIwhat\fR it points to, then
\&\f(CW\*(C`readlink\*(C'\fR is called for:
.PP
.Vb 7
\&    if (-l $file) {
\&        if (defined($whither = readlink($file))) {
\&            print "$file points to $whither\en";
\&        } else {
\&            print "$file points nowhere: $!\en";
\&        } 
\&    }
.Ve
.PP
Named pipes are a different matter.  You pretend they're regular files,
but their opens will normally block until there is both a reader and
a writer.  You can read more about them in \*(L"Named Pipes\*(R" in perlipc.
Unix-domain sockets are rather different beasts as well; they're
described in \*(L"Unix\-Domain \s-1TCP\s0 Clients and Servers\*(R" in perlipc.
.PP
When it comes to opening devices, it can be easy and it can tricky.
We'll assume that if you're opening up a block device, you know what
you're doing.  The character devices are more interesting.  These are
typically used for modems, mice, and some kinds of printers.  This is
described in \*(L"How do I read and write the serial port?\*(R" in perlfaq8
It's often enough to open them carefully:
.PP
.Vb 5
\&    sysopen(TTYIN, "/dev/ttyS1", O_RDWR | O_NDELAY | O_NOCTTY)
\&                # (O_NOCTTY no longer needed on POSIX systems)
\&        or die "can't open /dev/ttyS1: $!";
\&    open(TTYOUT, "+>&TTYIN")
\&        or die "can't dup TTYIN: $!";
.Ve
.PP
.Vb 1
\&    $ofh = select(TTYOUT); $| = 1; select($ofh);
.Ve
.PP
.Vb 2
\&    print TTYOUT "+++at\e015";
\&    $answer = <TTYIN>;
.Ve
.PP
With descriptors that you haven't opened using \f(CW\*(C`sysopen\*(C'\fR, such as a
socket, you can set them to be non-blocking using \f(CW\*(C`fcntl\*(C'\fR:
.PP
.Vb 3
\&    use Fcntl;
\&    fcntl(Connection, F_SETFL, O_NONBLOCK) 
\&        or die "can't set non blocking: $!";
.Ve
.PP
Rather than losing yourself in a morass of twisting, turning \f(CW\*(C`ioctl\*(C'\fRs,
all dissimilar, if you're going to manipulate ttys, it's best to
make calls out to the \fIstty\fR\|(1) program if you have it, or else use the
portable \s-1POSIX\s0 interface.  To figure this all out, you'll need to read the
\&\fItermios\fR\|(3) manpage, which describes the \s-1POSIX\s0 interface to tty devices,
and then \s-1POSIX\s0, which describes Perl's interface to \s-1POSIX\s0.  There are
also some high-level modules on \s-1CPAN\s0 that can help you with these games.
Check out Term::ReadKey and Term::ReadLine.
.PP
What else can you open?  To open a connection using sockets, you won't use
one of Perl's two open functions.  See 
\&\*(L"Sockets: Client/Server Communication\*(R" in perlipc for that.  Here's an 
example.  Once you have it, you can use \s-1FH\s0 as a bidirectional filehandle.
.PP
.Vb 2
\&    use IO::Socket;
\&    local *FH = IO::Socket::INET->new("www.perl.com:80");
.Ve
.PP
For opening up a \s-1URL\s0, the \s-1LWP\s0 modules from \s-1CPAN\s0 are just what
the doctor ordered.  There's no filehandle interface, but
it's still easy to get the contents of a document:
.PP
.Vb 2
\&    use LWP::Simple;
\&    $doc = get('http://www.linpro.no/lwp/');
.Ve
.Sh "Binary Files"
.IX Subsection "Binary Files"
On certain legacy systems with what could charitably be called terminally
convoluted (some would say broken) I/O models, a file isn't a file\*(--at
least, not with respect to the C standard I/O library.  On these old
systems whose libraries (but not kernels) distinguish between text and
binary streams, to get files to behave properly you'll have to bend over
backwards to avoid nasty problems.  On such infelicitous systems, sockets
and pipes are already opened in binary mode, and there is currently no
way to turn that off.  With files, you have more options.
.PP
Another option is to use the \f(CW\*(C`binmode\*(C'\fR function on the appropriate
handles before doing regular I/O on them:
.PP
.Vb 3
\&    binmode(STDIN);
\&    binmode(STDOUT);
\&    while (<STDIN>) { print }
.Ve
.PP
Passing \f(CW\*(C`sysopen\*(C'\fR a non-standard flag option will also open the file in
binary mode on those systems that support it.  This is the equivalent of
opening the file normally, then calling \f(CW\*(C`binmode\*(C'\fRing on the handle.
.PP
.Vb 2
\&    sysopen(BINDAT, "records.data", O_RDWR | O_BINARY)
\&        || die "can't open records.data: $!";
.Ve
.PP
Now you can use \f(CW\*(C`read\*(C'\fR and \f(CW\*(C`print\*(C'\fR on that handle without worrying
about the system non-standard I/O library breaking your data.  It's not
a pretty picture, but then, legacy systems seldom are.  \s-1CP/M\s0 will be
with us until the end of days, and after.
.PP
On systems with exotic I/O systems, it turns out that, astonishingly
enough, even unbuffered I/O using \f(CW\*(C`sysread\*(C'\fR and \f(CW\*(C`syswrite\*(C'\fR might do
sneaky data mutilation behind your back.
.PP
.Vb 3
\&    while (sysread(WHENCE, $buf, 1024)) {
\&        syswrite(WHITHER, $buf, length($buf));
\&    }
.Ve
.PP
Depending on the vicissitudes of your runtime system, even these calls
may need \f(CW\*(C`binmode\*(C'\fR or \f(CW\*(C`O_BINARY\*(C'\fR first.  Systems known to be free of
such difficulties include Unix, the Mac \s-1OS\s0, Plan 9, and Inferno.
.Sh "File Locking"
.IX Subsection "File Locking"
In a multitasking environment, you may need to be careful not to collide
with other processes who want to do I/O on the same files as others
are working on.  You'll often need shared or exclusive locks
on files for reading and writing respectively.  You might just
pretend that only exclusive locks exist.
.PP
Never use the existence of a file \f(CW\*(C`\-e $file\*(C'\fR as a locking indication,
because there is a race condition between the test for the existence of
the file and its creation.  Atomicity is critical.
.PP
Perl's most portable locking interface is via the \f(CW\*(C`flock\*(C'\fR function,
whose simplicity is emulated on systems that don't directly support it,
such as SysV or WindowsNT.  The underlying semantics may affect how
it all works, so you should learn how \f(CW\*(C`flock\*(C'\fR is implemented on your
system's port of Perl.
.PP
File locking \fIdoes not\fR lock out another process that would like to
do I/O.  A file lock only locks out others trying to get a lock, not
processes trying to do I/O.  Because locks are advisory, if one process
uses locking and another doesn't, all bets are off.
.PP
By default, the \f(CW\*(C`flock\*(C'\fR 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 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.
.PP
Here's how to get a blocking shared lock on a file, typically used
for reading:
.PP
.Vb 5
\&    use 5.004;
\&    use Fcntl qw(:DEFAULT :flock);
\&    open(FH, "< filename")  or die "can't open filename: $!";
\&    flock(FH, LOCK_SH)      or die "can't lock filename: $!";
\&    # now read from FH
.Ve
.PP
You can get a non-blocking lock by using \f(CW\*(C`LOCK_NB\*(C'\fR.
.PP
.Vb 2
\&    flock(FH, LOCK_SH | LOCK_NB)
\&        or die "can't lock filename: $!";
.Ve
.PP
This can be useful for producing more user-friendly behaviour by warning
if you're going to be blocking:
.PP
.Vb 10
\&    use 5.004;
\&    use Fcntl qw(:DEFAULT :flock);
\&    open(FH, "< filename")  or die "can't open filename: $!";
\&    unless (flock(FH, LOCK_SH | LOCK_NB)) {
\&        $| = 1;
\&        print "Waiting for lock...";
\&        flock(FH, LOCK_SH)  or die "can't lock filename: $!";
\&        print "got it.\en"
\&    } 
\&    # now read from FH
.Ve
.PP
To get an exclusive lock, typically used for writing, you have to be
careful.  We \f(CW\*(C`sysopen\*(C'\fR the file so it can be locked before it gets
emptied.  You can get a nonblocking version using \f(CW\*(C`LOCK_EX | LOCK_NB\*(C'\fR.
.PP
.Vb 9
\&    use 5.004;
\&    use Fcntl qw(:DEFAULT :flock);
\&    sysopen(FH, "filename", O_WRONLY | O_CREAT)
\&        or die "can't open filename: $!";
\&    flock(FH, LOCK_EX)
\&        or die "can't lock filename: $!";
\&    truncate(FH, 0)
\&        or die "can't truncate filename: $!";
\&    # now write to FH
.Ve
.PP
Finally, due to the uncounted millions who cannot be dissuaded from
wasting cycles on useless vanity devices called hit counters, here's
how to increment a number in a file safely:
.PP
.Vb 1
\&    use Fcntl qw(:DEFAULT :flock);
.Ve
.PP
.Vb 6
\&    sysopen(FH, "numfile", O_RDWR | O_CREAT)
\&        or die "can't open numfile: $!";
\&    # autoflush FH
\&    $ofh = select(FH); $| = 1; select ($ofh);
\&    flock(FH, LOCK_EX)
\&        or die "can't write-lock numfile: $!";
.Ve
.PP
.Vb 5
\&    $num = <FH> || 0;
\&    seek(FH, 0, 0)
\&        or die "can't rewind numfile : $!";
\&    print FH $num+1, "\en"
\&        or die "can't write numfile: $!";
.Ve
.PP
.Vb 4
\&    truncate(FH, tell(FH))
\&        or die "can't truncate numfile: $!";
\&    close(FH)
\&        or die "can't close numfile: $!";
.Ve
.Sh "\s-1IO\s0 Layers"
.IX Subsection "IO Layers"
In Perl 5.8.0 a new I/O framework called \*(L"PerlIO\*(R" was introduced.
This is a new \*(L"plumbing\*(R" 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 \*(L"layers\*(R".
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.
.PP
Full discussion about the features of PerlIO is out of scope for this
tutorial, but here is how to recognize the layers being used:
.IP "\(bu" 4
The three\-(or more)\-argument form of \f(CW\*(C`open()\*(C'\fR is being used and the
second argument contains something else in addition to the usual
\&\f(CW'<'\fR, \f(CW'>'\fR, \f(CW'>>'\fR, \f(CW'|'\fR and their variants,
for example:
.Sp
.Vb 1
\&    open(my $fh, "<:utf8", $fn);
.Ve
.IP "\(bu" 4
The two-argument form of \f(CW\*(C`binmode<open()\*(C'\fR is being used, for example
.Sp
.Vb 1
\&    binmode($fh, ":encoding(utf16)");
.Ve
.PP
For more detailed discussion about PerlIO see perlio;
for more detailed discussion about Unicode and I/O see perluniintro.
.SH "SEE ALSO"
.IX Header "SEE ALSO"
The \f(CW\*(C`open\*(C'\fR and \f(CW\*(C`sysopen\*(C'\fR function in \fIperlfunc\fR\|(1);
the standard \fIopen\fR\|(2), \fIdup\fR\|(2), \fIfopen\fR\|(3), and \fIfdopen\fR\|(3) manpages;
the \s-1POSIX\s0 documentation.
.SH "AUTHOR and COPYRIGHT"
.IX Header "AUTHOR and COPYRIGHT"
Copyright 1998 Tom Christiansen.  
.PP
This documentation is free; you can redistribute it and/or modify it
under the same terms as Perl itself.
.PP
Irrespective of its distribution, all code examples in these files are
hereby placed into the public domain.  You are permitted and
encouraged to use this code in your own programs for fun or for profit
as you see fit.  A simple comment in the code giving credit would be
courteous but is not required.
.SH "HISTORY"
.IX Header "HISTORY"
First release: Sat Jan  9 08:09:11 \s-1MST\s0 1999

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.