# Format:
# function func-name
# arg-1-name|
# arg-2-name|
# ...
# desc-lines
# -----

function abs
value|
Returns the absolute value of its argument.
If VALUE is omitted, uses <code class="inline"><span class="i">$_</span></code>
.
-----

function abs
Returns the absolute value of its argument.
If VALUE is omitted, uses <code class="inline"><span class="i">$_</span></code>
.
-----

function accept
newsocket|
genericsocket|
Accepts an incoming socket connect, just as the accept(2) system call
does.  Returns the packed address if it succeeded, false otherwise.
See the example in <a href="../perlipc.html#Sockets%3a-Client%2fServer-Communication">"Sockets: Client/Server Communication" in perlipc</a>.</p>
<p>On systems that support a close-on-exec flag on files, the flag will
be set for the newly opened file descriptor, as determined by the
value of $^F.  See <a href="../perlvar.html#%24%5eF">"$^F" in perlvar</a>.
-----

function alarm
seconds|
Arranges to have a SIGALRM delivered to this process after the
specified number of wallclock seconds has elapsed.  If SECONDS is not
specified, the value stored in <code class="inline"><span class="i">$_</span></code>
 is used. (On some machines,
unfortunately, the elapsed time may be up to one second less or more
than you specified because of how seconds are counted, and process
scheduling may delay the delivery of the signal even further.)</p>
<p>Only one timer may be counting at once.  Each call disables the
previous timer, and an argument of <code class="inline"><span class="n">0</span></code>
 may be supplied to cancel the
previous timer without starting a new one.  The returned value is the
amount of time remaining on the previous timer.</p>
<p>For delays of finer granularity than one second, the Time::HiRes module
(from CPAN, and starting from Perl 5.8 part of the standard
distribution) provides ualarm().  You may also use Perl&#39;s four-argument
version of select() leaving the first three arguments undefined, or you
might be able to use the <code class="inline"><a class="l_k" href="../functions/syscall.html">syscall</a></code> interface to access setitimer(2) if
your system supports it. See <a href="../perlfaq8.html">perlfaq8</a> for details.</p>
<p>It is usually a mistake to intermix <code class="inline"><a class="l_k" href="../functions/alarm.html">alarm</a></code> and <code class="inline"><a class="l_k" href="../functions/sleep.html">sleep</a></code> calls.
(<code class="inline"><a class="l_k" href="../functions/sleep.html">sleep</a></code> may be internally implemented in your system with <code class="inline"><a class="l_k" href="../functions/alarm.html">alarm</a></code>)</p>
<p>If you want to use <code class="inline"><a class="l_k" href="../functions/alarm.html">alarm</a></code> to time out a system call you need to use an
<code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code>/<code class="inline"><a class="l_k" href="../functions/die.html">die</a></code> pair.  You can&#39;t rely on the alarm causing the system call to
fail with <code class="inline"><span class="i">$!</span></code>
 set to <code class="inline"><span class="w">EINTR</span></code>
 because Perl sets up signal handlers to
restart system calls on some systems.  Using <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code>/<code class="inline"><a class="l_k" href="../functions/die.html">die</a></code> always works,
modulo the caveats given in <a href="../perlipc.html#Signals">"Signals" in perlipc</a>.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/eval.html">eval</a> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/local.html">local</a> <span class="i">$SIG</span>{<span class="w">ALRM</span>} = <a class="l_k" href="../functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;alarm\n&quot;</span> <span class="s">}</span><span class="sc">;</span> <span class="c"># NB: \n required</span></li><li>	<a class="l_k" href="../functions/alarm.html">alarm</a> <span class="i">$timeout</span><span class="sc">;</span></li><li>	<span class="i">$nread</span> = <a class="l_k" href="../functions/sysread.html">sysread</a> <span class="w">SOCKET</span><span class="cm">,</span> <span class="i">$buffer</span><span class="cm">,</span> <span class="i">$size</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/alarm.html">alarm</a> <span class="n">0</span><span class="sc">;</span></li><li>    <span class="s">}</span><span class="sc">;</span></li><li>    if <span class="s">(</span><span class="i">$@</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/die.html">die</a> unless <span class="i">$@</span> eq <span class="q">&quot;alarm\n&quot;</span><span class="sc">;</span>   <span class="c"># propagate unexpected errors</span></li><li>    	<span class="c"># timed out</span></li><li>    <span class="s">}</span></li><li>    else <span class="s">{</span></li><li>    	<span class="c"># didn&#39;t</span></li><li>    <span class="s">}</span></li></ol></pre><p>For more information see <a href="../perlipc.html">perlipc</a>.
-----

function alarm
Arranges to have a SIGALRM delivered to this process after the
specified number of wallclock seconds has elapsed.  If SECONDS is not
specified, the value stored in <code class="inline"><span class="i">$_</span></code>
 is used. (On some machines,
unfortunately, the elapsed time may be up to one second less or more
than you specified because of how seconds are counted, and process
scheduling may delay the delivery of the signal even further.)</p>
<p>Only one timer may be counting at once.  Each call disables the
previous timer, and an argument of <code class="inline"><span class="n">0</span></code>
 may be supplied to cancel the
previous timer without starting a new one.  The returned value is the
amount of time remaining on the previous timer.</p>
<p>For delays of finer granularity than one second, the Time::HiRes module
(from CPAN, and starting from Perl 5.8 part of the standard
distribution) provides ualarm().  You may also use Perl&#39;s four-argument
version of select() leaving the first three arguments undefined, or you
might be able to use the <code class="inline"><a class="l_k" href="../functions/syscall.html">syscall</a></code> interface to access setitimer(2) if
your system supports it. See <a href="../perlfaq8.html">perlfaq8</a> for details.</p>
<p>It is usually a mistake to intermix <code class="inline"><a class="l_k" href="../functions/alarm.html">alarm</a></code> and <code class="inline"><a class="l_k" href="../functions/sleep.html">sleep</a></code> calls.
(<code class="inline"><a class="l_k" href="../functions/sleep.html">sleep</a></code> may be internally implemented in your system with <code class="inline"><a class="l_k" href="../functions/alarm.html">alarm</a></code>)</p>
<p>If you want to use <code class="inline"><a class="l_k" href="../functions/alarm.html">alarm</a></code> to time out a system call you need to use an
<code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code>/<code class="inline"><a class="l_k" href="../functions/die.html">die</a></code> pair.  You can&#39;t rely on the alarm causing the system call to
fail with <code class="inline"><span class="i">$!</span></code>
 set to <code class="inline"><span class="w">EINTR</span></code>
 because Perl sets up signal handlers to
restart system calls on some systems.  Using <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code>/<code class="inline"><a class="l_k" href="../functions/die.html">die</a></code> always works,
modulo the caveats given in <a href="../perlipc.html#Signals">"Signals" in perlipc</a>.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/eval.html">eval</a> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/local.html">local</a> <span class="i">$SIG</span>{<span class="w">ALRM</span>} = <a class="l_k" href="../functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;alarm\n&quot;</span> <span class="s">}</span><span class="sc">;</span> <span class="c"># NB: \n required</span></li><li>	<a class="l_k" href="../functions/alarm.html">alarm</a> <span class="i">$timeout</span><span class="sc">;</span></li><li>	<span class="i">$nread</span> = <a class="l_k" href="../functions/sysread.html">sysread</a> <span class="w">SOCKET</span><span class="cm">,</span> <span class="i">$buffer</span><span class="cm">,</span> <span class="i">$size</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/alarm.html">alarm</a> <span class="n">0</span><span class="sc">;</span></li><li>    <span class="s">}</span><span class="sc">;</span></li><li>    if <span class="s">(</span><span class="i">$@</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/die.html">die</a> unless <span class="i">$@</span> eq <span class="q">&quot;alarm\n&quot;</span><span class="sc">;</span>   <span class="c"># propagate unexpected errors</span></li><li>    	<span class="c"># timed out</span></li><li>    <span class="s">}</span></li><li>    else <span class="s">{</span></li><li>    	<span class="c"># didn&#39;t</span></li><li>    <span class="s">}</span></li></ol></pre><p>For more information see <a href="../perlipc.html">perlipc</a>.
-----

function atan2
y|
x|
Returns the arctangent of Y/X in the range -PI to PI.</p>
<p>For the tangent operation, you may use the <code class="inline"><span class="w">Math::Trig::tan</span></code>

function, or use the familiar relation:</p>
<pre class="verbatim"><ol><li><a name="tan"></a>    sub <span class="m">tan</span> <span class="s">{</span> <a class="l_k" href="../functions/sin.html">sin</a><span class="s">(</span><span class="i">$_</span>[<span class="n">0</span>]<span class="s">)</span> / <a class="l_k" href="../functions/cos.html">cos</a><span class="s">(</span><span class="i">$_</span>[<span class="n">0</span>]<span class="s">)</span>  <span class="s">}</span></li></ol></pre><p>The return value for <code class="inline"><a class="l_k" href="../functions/atan2.html">atan2(0,0)</a></code> is implementation-defined; consult
your atan2(3) manpage for more information.
-----

function bind
socket|
name|
Binds a network address to a socket, just as the bind system call
does.  Returns true if it succeeded, false otherwise.  NAME should be a
packed address of the appropriate type for the socket.  See the examples in
<a href="../perlipc.html#Sockets%3a-Client%2fServer-Communication">"Sockets: Client/Server Communication" in perlipc</a>.
-----

function binmode
filehandle|
layer|
Arranges for FILEHANDLE to be read or written in &quot;binary&quot; or &quot;text&quot;
mode on systems where the run-time libraries distinguish between
binary and text files.  If FILEHANDLE is an expression, the value is
taken as the name of the filehandle.  Returns true on success,
otherwise it returns <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> and sets <code class="inline"><span class="i">$!</span></code>
 (errno).</p>
<p>On some systems (in general, DOS and Windows-based systems) binmode()
is necessary when you&#39;re not working with a text file.  For the sake
of portability it is a good idea to always use it when appropriate,
and to never use it when it isn&#39;t appropriate.  Also, people can
set their I/O to be by default UTF-8 encoded Unicode, not bytes.</p>
<p>In other words: regardless of platform, use binmode() on binary data,
like for example images.</p>
<p>If LAYER is present it is a single string, but may contain multiple
directives. The directives alter the behaviour of the file handle.
When LAYER is present using binmode on a text file makes sense.</p>
<p>If LAYER is omitted or specified as <code class="inline"><span class="j">:</span><span class="w">raw</span></code>
 the filehandle is made
suitable for passing binary data. This includes turning off possible CRLF
translation and marking it as bytes (as opposed to Unicode characters).
Note that, despite what may be implied in <i>&quot;Programming Perl&quot;</i> (the
Camel) or elsewhere, <code class="inline"><span class="j">:</span><span class="w">raw</span></code>
 is <i>not</i> simply the inverse of <code class="inline"><span class="j">:</span><span class="w">crlf</span></code>

-- other layers which would affect the binary nature of the stream are
<i>also</i> disabled. See <a href="../PerlIO.html">PerlIO</a>, <a href="../perlrun.html">perlrun</a> and the discussion about the
PERLIO environment variable.</p>
<p>The <code class="inline"><span class="j">:</span><span class="w">bytes</span></code>
, <code class="inline"><span class="j">:</span><span class="w">crlf</span></code>
, and <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
, and any other directives of the
form <code class="inline"><span class="j">:</span>...</code>
, are called I/O <i>layers</i>.  The <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> pragma can be used to
establish default I/O layers.  See <a href="../open.html">open</a>.</p>
<p><i>The LAYER parameter of the binmode() function is described as &quot;DISCIPLINE&quot;
in &quot;Programming Perl, 3rd Edition&quot;.  However, since the publishing of this
book, by many known as &quot;Camel III&quot;, the consensus of the naming of this
functionality has moved from &quot;discipline&quot; to &quot;layer&quot;.  All documentation
of this version of Perl therefore refers to &quot;layers&quot; rather than to
&quot;disciplines&quot;.  Now back to the regularly scheduled documentation...</i></p>
<p>To mark FILEHANDLE as UTF-8, use <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
 or <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span><span class="w">utf8</span><span class="s">)</span></code>
.
<code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
 just marks the data as UTF-8 without further checking,
while <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span><span class="w">utf8</span><span class="s">)</span></code>
 checks the data for actually being valid
UTF-8. More details can be found in <a href="../PerlIO/encoding.html">PerlIO::encoding</a>.</p>
<p>In general, binmode() should be called after open() but before any I/O
is done on the filehandle.  Calling binmode() will normally flush any
pending buffered output data (and perhaps pending input data) on the
handle.  An exception to this is the <code class="inline"><span class="j">:</span><span class="w">encoding</span></code>
 layer that
changes the default character encoding of the handle, see <a href="../open.html">open</a>.
The <code class="inline"><span class="j">:</span><span class="w">encoding</span></code>
 layer sometimes needs to be called in
mid-stream, and it doesn&#39;t flush the stream.  The <code class="inline"><span class="j">:</span><span class="w">encoding</span></code>

also implicitly pushes on top of itself the <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
 layer because
internally Perl will operate on UTF-8 encoded Unicode characters.</p>
<p>The operating system, device drivers, C libraries, and Perl run-time
system all work together to let the programmer treat a single
character (<code class="inline">\<span class="w">n</span></code>
) as the line terminator, irrespective of the external
representation.  On many operating systems, the native text file
representation matches the internal representation, but on some
platforms the external representation of <code class="inline">\<span class="w">n</span></code>
 is made up of more than
one character.</p>
<p>Mac OS, all variants of Unix, and Stream_LF files on VMS use a single
character to end each line in the external representation of text (even
though that single character is CARRIAGE RETURN on Mac OS and LINE FEED
on Unix and most VMS files). In other systems like OS/2, DOS and the
various flavors of MS-Windows your program sees a <code class="inline">\<span class="w">n</span></code>
 as a simple <code class="inline">\<span class="w">cJ</span></code>
,
but what&#39;s stored in text files are the two characters <code class="inline">\<span class="w">cM</span>\<span class="w">cJ</span></code>
.  That
means that, if you don&#39;t use binmode() on these systems, <code class="inline">\<span class="w">cM</span>\<span class="w">cJ</span></code>

sequences on disk will be converted to <code class="inline">\<span class="w">n</span></code>
 on input, and any <code class="inline">\<span class="w">n</span></code>
 in
your program will be converted back to <code class="inline">\<span class="w">cM</span>\<span class="w">cJ</span></code>
 on output.  This is what
you want for text files, but it can be disastrous for binary files.</p>
<p>Another consequence of using binmode() (on some systems) is that
special end-of-file markers will be seen as part of the data stream.
For systems from the Microsoft family this means that if your binary
data contains <code class="inline">\<span class="w">cZ</span></code>
, the I/O subsystem will regard it as the end of
the file, unless you use binmode().</p>
<p>binmode() is not only important for readline() and print() operations,
but also when using read(), seek(), sysread(), syswrite() and tell()
(see <a href="../perlport.html">perlport</a> for more details).  See the <code class="inline"><span class="i">$/</span></code>
 and <code class="inline"><span class="i">$\</span></code>
 variables
in <a href="../perlvar.html">perlvar</a> for how to manually set your input and output
line-termination sequences.
-----

function binmode
filehandle|
Arranges for FILEHANDLE to be read or written in &quot;binary&quot; or &quot;text&quot;
mode on systems where the run-time libraries distinguish between
binary and text files.  If FILEHANDLE is an expression, the value is
taken as the name of the filehandle.  Returns true on success,
otherwise it returns <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> and sets <code class="inline"><span class="i">$!</span></code>
 (errno).</p>
<p>On some systems (in general, DOS and Windows-based systems) binmode()
is necessary when you&#39;re not working with a text file.  For the sake
of portability it is a good idea to always use it when appropriate,
and to never use it when it isn&#39;t appropriate.  Also, people can
set their I/O to be by default UTF-8 encoded Unicode, not bytes.</p>
<p>In other words: regardless of platform, use binmode() on binary data,
like for example images.</p>
<p>If LAYER is present it is a single string, but may contain multiple
directives. The directives alter the behaviour of the file handle.
When LAYER is present using binmode on a text file makes sense.</p>
<p>If LAYER is omitted or specified as <code class="inline"><span class="j">:</span><span class="w">raw</span></code>
 the filehandle is made
suitable for passing binary data. This includes turning off possible CRLF
translation and marking it as bytes (as opposed to Unicode characters).
Note that, despite what may be implied in <i>&quot;Programming Perl&quot;</i> (the
Camel) or elsewhere, <code class="inline"><span class="j">:</span><span class="w">raw</span></code>
 is <i>not</i> simply the inverse of <code class="inline"><span class="j">:</span><span class="w">crlf</span></code>

-- other layers which would affect the binary nature of the stream are
<i>also</i> disabled. See <a href="../PerlIO.html">PerlIO</a>, <a href="../perlrun.html">perlrun</a> and the discussion about the
PERLIO environment variable.</p>
<p>The <code class="inline"><span class="j">:</span><span class="w">bytes</span></code>
, <code class="inline"><span class="j">:</span><span class="w">crlf</span></code>
, and <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
, and any other directives of the
form <code class="inline"><span class="j">:</span>...</code>
, are called I/O <i>layers</i>.  The <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> pragma can be used to
establish default I/O layers.  See <a href="../open.html">open</a>.</p>
<p><i>The LAYER parameter of the binmode() function is described as &quot;DISCIPLINE&quot;
in &quot;Programming Perl, 3rd Edition&quot;.  However, since the publishing of this
book, by many known as &quot;Camel III&quot;, the consensus of the naming of this
functionality has moved from &quot;discipline&quot; to &quot;layer&quot;.  All documentation
of this version of Perl therefore refers to &quot;layers&quot; rather than to
&quot;disciplines&quot;.  Now back to the regularly scheduled documentation...</i></p>
<p>To mark FILEHANDLE as UTF-8, use <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
 or <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span><span class="w">utf8</span><span class="s">)</span></code>
.
<code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
 just marks the data as UTF-8 without further checking,
while <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span><span class="w">utf8</span><span class="s">)</span></code>
 checks the data for actually being valid
UTF-8. More details can be found in <a href="../PerlIO/encoding.html">PerlIO::encoding</a>.</p>
<p>In general, binmode() should be called after open() but before any I/O
is done on the filehandle.  Calling binmode() will normally flush any
pending buffered output data (and perhaps pending input data) on the
handle.  An exception to this is the <code class="inline"><span class="j">:</span><span class="w">encoding</span></code>
 layer that
changes the default character encoding of the handle, see <a href="../open.html">open</a>.
The <code class="inline"><span class="j">:</span><span class="w">encoding</span></code>
 layer sometimes needs to be called in
mid-stream, and it doesn&#39;t flush the stream.  The <code class="inline"><span class="j">:</span><span class="w">encoding</span></code>

also implicitly pushes on top of itself the <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
 layer because
internally Perl will operate on UTF-8 encoded Unicode characters.</p>
<p>The operating system, device drivers, C libraries, and Perl run-time
system all work together to let the programmer treat a single
character (<code class="inline">\<span class="w">n</span></code>
) as the line terminator, irrespective of the external
representation.  On many operating systems, the native text file
representation matches the internal representation, but on some
platforms the external representation of <code class="inline">\<span class="w">n</span></code>
 is made up of more than
one character.</p>
<p>Mac OS, all variants of Unix, and Stream_LF files on VMS use a single
character to end each line in the external representation of text (even
though that single character is CARRIAGE RETURN on Mac OS and LINE FEED
on Unix and most VMS files). In other systems like OS/2, DOS and the
various flavors of MS-Windows your program sees a <code class="inline">\<span class="w">n</span></code>
 as a simple <code class="inline">\<span class="w">cJ</span></code>
,
but what&#39;s stored in text files are the two characters <code class="inline">\<span class="w">cM</span>\<span class="w">cJ</span></code>
.  That
means that, if you don&#39;t use binmode() on these systems, <code class="inline">\<span class="w">cM</span>\<span class="w">cJ</span></code>

sequences on disk will be converted to <code class="inline">\<span class="w">n</span></code>
 on input, and any <code class="inline">\<span class="w">n</span></code>
 in
your program will be converted back to <code class="inline">\<span class="w">cM</span>\<span class="w">cJ</span></code>
 on output.  This is what
you want for text files, but it can be disastrous for binary files.</p>
<p>Another consequence of using binmode() (on some systems) is that
special end-of-file markers will be seen as part of the data stream.
For systems from the Microsoft family this means that if your binary
data contains <code class="inline">\<span class="w">cZ</span></code>
, the I/O subsystem will regard it as the end of
the file, unless you use binmode().</p>
<p>binmode() is not only important for readline() and print() operations,
but also when using read(), seek(), sysread(), syswrite() and tell()
(see <a href="../perlport.html">perlport</a> for more details).  See the <code class="inline"><span class="i">$/</span></code>
 and <code class="inline"><span class="i">$\</span></code>
 variables
in <a href="../perlvar.html">perlvar</a> for how to manually set your input and output
line-termination sequences.
-----

function bless
ref|
classname|
This function tells the thingy referenced by REF that it is now an object
in the CLASSNAME package.  If CLASSNAME is omitted, the current package
is used.  Because a <code class="inline"><a class="l_k" href="../functions/bless.html">bless</a></code> is often the last thing in a constructor,
it returns the reference for convenience.  Always use the two-argument
version if a derived class might inherit the function doing the blessing.
See <a href="../perltoot.html">perltoot</a> and <a href="../perlobj.html">perlobj</a> for more about the blessing (and blessings)
of objects.</p>
<p>Consider always blessing objects in CLASSNAMEs that are mixed case.
Namespaces with all lowercase names are considered reserved for
Perl pragmata.  Builtin types have all uppercase names. To prevent
confusion, you may wish to avoid such package names as well.  Make sure
that CLASSNAME is a true value.</p>
<p>See <a href="../perlmod.html#Perl-Modules">"Perl Modules" in perlmod</a>.
-----

function bless
ref|
This function tells the thingy referenced by REF that it is now an object
in the CLASSNAME package.  If CLASSNAME is omitted, the current package
is used.  Because a <code class="inline"><a class="l_k" href="../functions/bless.html">bless</a></code> is often the last thing in a constructor,
it returns the reference for convenience.  Always use the two-argument
version if a derived class might inherit the function doing the blessing.
See <a href="../perltoot.html">perltoot</a> and <a href="../perlobj.html">perlobj</a> for more about the blessing (and blessings)
of objects.</p>
<p>Consider always blessing objects in CLASSNAMEs that are mixed case.
Namespaces with all lowercase names are considered reserved for
Perl pragmata.  Builtin types have all uppercase names. To prevent
confusion, you may wish to avoid such package names as well.  Make sure
that CLASSNAME is a true value.</p>
<p>See <a href="../perlmod.html#Perl-Modules">"Perl Modules" in perlmod</a>.
-----

function break
Break out of a <code class="inline">given<span class="s">(</span><span class="s">)</span></code>
 block.</p>
<p>This keyword is enabled by the &quot;switch&quot; feature: see <a href="../feature.html">feature</a>
for more information.
-----

function caller
expr|
Returns the context of the current subroutine call.  In scalar context,
returns the caller&#39;s package name if there is a caller, that is, if
we&#39;re in a subroutine or <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code> or <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code>, and the undefined value
otherwise.  In list context, returns</p>
<pre class="verbatim"><ol><li>    <span class="c"># 0         1          2</span></li><li>    <span class="s">(</span><span class="i">$package</span><span class="cm">,</span> <span class="i">$filename</span><span class="cm">,</span> <span class="i">$line</span><span class="s">)</span> = <a class="l_k" href="../functions/caller.html">caller</a><span class="sc">;</span></li></ol></pre><p>With EXPR, it returns some extra information that the debugger uses to
print a stack trace.  The value of EXPR indicates how many call frames
to go back before the current one.</p>
<pre class="verbatim"><ol><li>    <span class="c">#  0         1          2      3            4</span></li><li>    <span class="s">(</span><span class="i">$package</span><span class="cm">,</span> <span class="i">$filename</span><span class="cm">,</span> <span class="i">$line</span><span class="cm">,</span> <span class="i">$subroutine</span><span class="cm">,</span> <span class="i">$hasargs</span><span class="cm">,</span></li><li></li><li>    <span class="c">#  5          6          7            8       9         10</span></li><li>    <span class="i">$wantarray</span><span class="cm">,</span> <span class="i">$evaltext</span><span class="cm">,</span> <span class="i">$is_require</span><span class="cm">,</span> <span class="i">$hints</span><span class="cm">,</span> <span class="i">$bitmask</span><span class="cm">,</span> <span class="i">$hinthash</span><span class="s">)</span></li><li>     = <a class="l_k" href="../functions/caller.html">caller</a><span class="s">(</span><span class="i">$i</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Here $subroutine may be <code class="inline"><span class="s">(</span><a class="l_k" href="../functions/eval.html">eval</a><span class="s">)</span></code>
 if the frame is not a subroutine
call, but an <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code>.  In such a case additional elements $evaltext and
<code class="inline"><span class="i">$is_require</span></code>
 are set: <code class="inline"><span class="i">$is_require</span></code>
 is true if the frame is created by a
<code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> or <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code> statement, $evaltext contains the text of the
<code class="inline"><a class="l_k" href="../functions/eval.html">eval</a> <span class="w">EXPR</span></code>
 statement.  In particular, for an <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a> <span class="w">BLOCK</span></code>
 statement,
$subroutine is <code class="inline"><span class="s">(</span><a class="l_k" href="../functions/eval.html">eval</a><span class="s">)</span></code>
, but $evaltext is undefined.  (Note also that
each <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code> statement creates a <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> frame inside an <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a> <span class="w">EXPR</span></code>

frame.)  $subroutine may also be <code class="inline"><span class="s">(</span><span class="w">unknown</span><span class="s">)</span></code>
 if this particular
subroutine happens to have been deleted from the symbol table.
<code class="inline"><span class="i">$hasargs</span></code>
 is true if a new instance of <code class="inline"><span class="i">@_</span></code>
 was set up for the frame.
<code class="inline"><span class="i">$hints</span></code>
 and <code class="inline"><span class="i">$bitmask</span></code>
 contain pragmatic hints that the caller was
compiled with.  The <code class="inline"><span class="i">$hints</span></code>
 and <code class="inline"><span class="i">$bitmask</span></code>
 values are subject to change
between versions of Perl, and are not meant for external use.</p>
<p><code class="inline"><span class="i">$hinthash</span></code>
 is a reference to a hash containing the value of <code class="inline"><span class="i">%^H</span></code>
 when the
caller was compiled, or <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> if <code class="inline"><span class="i">%^H</span></code>
 was empty. Do not modify the values
of this hash, as they are the actual values stored in the optree.</p>
<p>Furthermore, when called from within the DB package, caller returns more
detailed information: it sets the list variable <code class="inline"><span class="i">@DB::args</span></code>
 to be the
arguments with which the subroutine was invoked.</p>
<p>Be aware that the optimizer might have optimized call frames away before
<code class="inline"><a class="l_k" href="../functions/caller.html">caller</a></code> had a chance to get the information.  That means that <code class="inline"><a class="l_k" href="../functions/caller.html">caller(N)</a></code>
might not return information about the call frame you expect it do, for
<code class="inline"><span class="w">N</span> &gt; <span class="n">1</span></code>
.  In particular, <code class="inline"><span class="i">@DB::args</span></code>
 might have information from the
previous time <code class="inline"><a class="l_k" href="../functions/caller.html">caller</a></code> was called.
-----

function caller
Returns the context of the current subroutine call.  In scalar context,
returns the caller&#39;s package name if there is a caller, that is, if
we&#39;re in a subroutine or <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code> or <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code>, and the undefined value
otherwise.  In list context, returns</p>
<pre class="verbatim"><ol><li>    <span class="c"># 0         1          2</span></li><li>    <span class="s">(</span><span class="i">$package</span><span class="cm">,</span> <span class="i">$filename</span><span class="cm">,</span> <span class="i">$line</span><span class="s">)</span> = <a class="l_k" href="../functions/caller.html">caller</a><span class="sc">;</span></li></ol></pre><p>With EXPR, it returns some extra information that the debugger uses to
print a stack trace.  The value of EXPR indicates how many call frames
to go back before the current one.</p>
<pre class="verbatim"><ol><li>    <span class="c">#  0         1          2      3            4</span></li><li>    <span class="s">(</span><span class="i">$package</span><span class="cm">,</span> <span class="i">$filename</span><span class="cm">,</span> <span class="i">$line</span><span class="cm">,</span> <span class="i">$subroutine</span><span class="cm">,</span> <span class="i">$hasargs</span><span class="cm">,</span></li><li></li><li>    <span class="c">#  5          6          7            8       9         10</span></li><li>    <span class="i">$wantarray</span><span class="cm">,</span> <span class="i">$evaltext</span><span class="cm">,</span> <span class="i">$is_require</span><span class="cm">,</span> <span class="i">$hints</span><span class="cm">,</span> <span class="i">$bitmask</span><span class="cm">,</span> <span class="i">$hinthash</span><span class="s">)</span></li><li>     = <a class="l_k" href="../functions/caller.html">caller</a><span class="s">(</span><span class="i">$i</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Here $subroutine may be <code class="inline"><span class="s">(</span><a class="l_k" href="../functions/eval.html">eval</a><span class="s">)</span></code>
 if the frame is not a subroutine
call, but an <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code>.  In such a case additional elements $evaltext and
<code class="inline"><span class="i">$is_require</span></code>
 are set: <code class="inline"><span class="i">$is_require</span></code>
 is true if the frame is created by a
<code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> or <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code> statement, $evaltext contains the text of the
<code class="inline"><a class="l_k" href="../functions/eval.html">eval</a> <span class="w">EXPR</span></code>
 statement.  In particular, for an <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a> <span class="w">BLOCK</span></code>
 statement,
$subroutine is <code class="inline"><span class="s">(</span><a class="l_k" href="../functions/eval.html">eval</a><span class="s">)</span></code>
, but $evaltext is undefined.  (Note also that
each <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code> statement creates a <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> frame inside an <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a> <span class="w">EXPR</span></code>

frame.)  $subroutine may also be <code class="inline"><span class="s">(</span><span class="w">unknown</span><span class="s">)</span></code>
 if this particular
subroutine happens to have been deleted from the symbol table.
<code class="inline"><span class="i">$hasargs</span></code>
 is true if a new instance of <code class="inline"><span class="i">@_</span></code>
 was set up for the frame.
<code class="inline"><span class="i">$hints</span></code>
 and <code class="inline"><span class="i">$bitmask</span></code>
 contain pragmatic hints that the caller was
compiled with.  The <code class="inline"><span class="i">$hints</span></code>
 and <code class="inline"><span class="i">$bitmask</span></code>
 values are subject to change
between versions of Perl, and are not meant for external use.</p>
<p><code class="inline"><span class="i">$hinthash</span></code>
 is a reference to a hash containing the value of <code class="inline"><span class="i">%^H</span></code>
 when the
caller was compiled, or <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> if <code class="inline"><span class="i">%^H</span></code>
 was empty. Do not modify the values
of this hash, as they are the actual values stored in the optree.</p>
<p>Furthermore, when called from within the DB package, caller returns more
detailed information: it sets the list variable <code class="inline"><span class="i">@DB::args</span></code>
 to be the
arguments with which the subroutine was invoked.</p>
<p>Be aware that the optimizer might have optimized call frames away before
<code class="inline"><a class="l_k" href="../functions/caller.html">caller</a></code> had a chance to get the information.  That means that <code class="inline"><a class="l_k" href="../functions/caller.html">caller(N)</a></code>
might not return information about the call frame you expect it do, for
<code class="inline"><span class="w">N</span> &gt; <span class="n">1</span></code>
.  In particular, <code class="inline"><span class="i">@DB::args</span></code>
 might have information from the
previous time <code class="inline"><a class="l_k" href="../functions/caller.html">caller</a></code> was called.
-----

function chdir
expr|
Changes the working directory to EXPR, if possible. If EXPR is omitted,
changes to the directory specified by <code class="inline"><span class="i">$ENV</span>{<span class="w">HOME</span>}</code>
, if set; if not,
changes to the directory specified by <code class="inline"><span class="i">$ENV</span>{<span class="w">LOGDIR</span>}</code>
. (Under VMS, the
variable <code class="inline"><span class="i">$ENV</span>{<span class="w">SYS</span><span class="i">$LOGIN</span>}</code>
 is also checked, and used if it is set.) If
neither is set, <code class="inline"><a class="l_k" href="../functions/chdir.html">chdir</a></code> does nothing. It returns true upon success,
false otherwise. See the example under <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>.</p>
<p>On systems that support fchdir, you might pass a file handle or
directory handle as argument.  On systems that don&#39;t support fchdir,
passing handles produces a fatal error at run time.
-----

function chdir
filehandle|
Changes the working directory to EXPR, if possible. If EXPR is omitted,
changes to the directory specified by <code class="inline"><span class="i">$ENV</span>{<span class="w">HOME</span>}</code>
, if set; if not,
changes to the directory specified by <code class="inline"><span class="i">$ENV</span>{<span class="w">LOGDIR</span>}</code>
. (Under VMS, the
variable <code class="inline"><span class="i">$ENV</span>{<span class="w">SYS</span><span class="i">$LOGIN</span>}</code>
 is also checked, and used if it is set.) If
neither is set, <code class="inline"><a class="l_k" href="../functions/chdir.html">chdir</a></code> does nothing. It returns true upon success,
false otherwise. See the example under <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>.</p>
<p>On systems that support fchdir, you might pass a file handle or
directory handle as argument.  On systems that don&#39;t support fchdir,
passing handles produces a fatal error at run time.
-----

function chdir
dirhandle|
Changes the working directory to EXPR, if possible. If EXPR is omitted,
changes to the directory specified by <code class="inline"><span class="i">$ENV</span>{<span class="w">HOME</span>}</code>
, if set; if not,
changes to the directory specified by <code class="inline"><span class="i">$ENV</span>{<span class="w">LOGDIR</span>}</code>
. (Under VMS, the
variable <code class="inline"><span class="i">$ENV</span>{<span class="w">SYS</span><span class="i">$LOGIN</span>}</code>
 is also checked, and used if it is set.) If
neither is set, <code class="inline"><a class="l_k" href="../functions/chdir.html">chdir</a></code> does nothing. It returns true upon success,
false otherwise. See the example under <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>.</p>
<p>On systems that support fchdir, you might pass a file handle or
directory handle as argument.  On systems that don&#39;t support fchdir,
passing handles produces a fatal error at run time.
-----

function chdir
Changes the working directory to EXPR, if possible. If EXPR is omitted,
changes to the directory specified by <code class="inline"><span class="i">$ENV</span>{<span class="w">HOME</span>}</code>
, if set; if not,
changes to the directory specified by <code class="inline"><span class="i">$ENV</span>{<span class="w">LOGDIR</span>}</code>
. (Under VMS, the
variable <code class="inline"><span class="i">$ENV</span>{<span class="w">SYS</span><span class="i">$LOGIN</span>}</code>
 is also checked, and used if it is set.) If
neither is set, <code class="inline"><a class="l_k" href="../functions/chdir.html">chdir</a></code> does nothing. It returns true upon success,
false otherwise. See the example under <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>.</p>
<p>On systems that support fchdir, you might pass a file handle or
directory handle as argument.  On systems that don&#39;t support fchdir,
passing handles produces a fatal error at run time.
-----

function chmod
list|
Changes the permissions of a list of files.  The first element of the
list must be the numerical mode, which should probably be an octal
number, and which definitely should <i>not</i> be a string of octal digits:
<code class="inline"><span class="n">0644</span></code>
 is okay, <code class="inline"><span class="q">&#39;0644&#39;</span></code>
 is not.  Returns the number of files
successfully changed.  See also <a href="../functions/oct.html">"oct"</a>, if all you have is a string.</p>
<pre class="verbatim"><ol><li>    <span class="i">$cnt</span> = <a class="l_k" href="../functions/chmod.html">chmod</a> <span class="n">0755</span><span class="cm">,</span> <span class="q">&#39;foo&#39;</span><span class="cm">,</span> <span class="q">&#39;bar&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/chmod.html">chmod</a> <span class="n">0755</span><span class="cm">,</span> <span class="i">@executables</span><span class="sc">;</span></li><li>    <span class="i">$mode</span> = <span class="q">&#39;0644&#39;</span><span class="sc">;</span> <a class="l_k" href="../functions/chmod.html">chmod</a> <span class="i">$mode</span><span class="cm">,</span> <span class="q">&#39;foo&#39;</span><span class="sc">;</span>      <span class="c"># !!! sets mode to</span></li><li>                                             <span class="c"># --w----r-T</span></li><li>    <span class="i">$mode</span> = <span class="q">&#39;0644&#39;</span><span class="sc">;</span> <a class="l_k" href="../functions/chmod.html">chmod</a> <a class="l_k" href="../functions/oct.html">oct</a><span class="s">(</span><span class="i">$mode</span><span class="s">)</span><span class="cm">,</span> <span class="q">&#39;foo&#39;</span><span class="sc">;</span> <span class="c"># this is better</span></li><li>    <span class="i">$mode</span> = <span class="n">0644</span><span class="sc">;</span>   <a class="l_k" href="../functions/chmod.html">chmod</a> <span class="i">$mode</span><span class="cm">,</span> <span class="q">&#39;foo&#39;</span><span class="sc">;</span>      <span class="c"># this is best</span></li></ol></pre><p>On systems that support fchmod, you might pass file handles among the
files.  On systems that don&#39;t support fchmod, passing file handles
produces a fatal error at run time.   The file handles must be passed
as globs or references to be recognized.  Barewords are considered
file names.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">&quot;&lt;&quot;</span><span class="cm">,</span> <span class="q">&quot;foo&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$perm</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a> <span class="i">$fh</span><span class="s">)</span>[<span class="n">2</span>] &amp; <span class="n">07777</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/chmod.html">chmod</a><span class="s">(</span><span class="i">$perm</span> | <span class="n">0600</span><span class="cm">,</span> <span class="i">$fh</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>You can also import the symbolic <code class="inline"><span class="w">S_I</span>*</code>
 constants from the Fcntl
module:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Fcntl</span> <span class="q">&#39;:mode&#39;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/chmod.html">chmod</a> <span class="w">S_IRWXU</span>|<span class="w">S_IRGRP</span>|<span class="w">S_IXGRP</span>|<span class="w">S_IROTH</span>|<span class="w">S_IXOTH</span><span class="cm">,</span> <span class="i">@executables</span><span class="sc">;</span></li><li>    <span class="c"># This is identical to the chmod 0755 of the above example.</span></li></ol><
-----

function chomp
variable|
This safer version of <a href="../functions/chop.html">"chop"</a> removes any trailing string
that corresponds to the current value of <code class="inline"><span class="i">$/</span></code>
 (also known as
$INPUT_RECORD_SEPARATOR in the <code class="inline"><span class="w">English</span></code>
 module).  It returns the total
number of characters removed from all its arguments.  It&#39;s often used to
remove the newline from the end of an input record when you&#39;re worried
that the final record may be missing its newline.  When in paragraph
mode (<code class="inline"><span class="i">$/</span> = <span class="q">&quot;&quot;</span></code>
), it removes all trailing newlines from the string.
When in slurp mode (<code class="inline"><span class="i">$/</span> = <a class="l_k" href="../functions/undef.html">undef</a></code>
) or fixed-length record mode (<code class="inline"><span class="i">$/</span></code>
 is
a reference to an integer or the like, see <a href="../perlvar.html">perlvar</a>) chomp() won&#39;t
remove anything.
If VARIABLE is omitted, it chomps <code class="inline"><span class="i">$_</span></code>
.  Example:</p>
<pre class="verbatim"><ol><li>    while <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/chomp.html">chomp</a><span class="sc">;</span>	<span class="c"># avoid \n on last field</span></li><li>	<span class="i">@array</span> = <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/:/</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="c"># ...</span></li><li>    <span class="s">}</span></li></ol></pre><p>If VARIABLE is a hash, it chomps the hash&#39;s values, but not its keys.</p>
<p>You can actually chomp anything that&#39;s an lvalue, including an assignment:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/chomp.html">chomp</a><span class="s">(</span><span class="i">$cwd</span> = <span class="q">`pwd`</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/chomp.html">chomp</a><span class="s">(</span><span class="i">$answer</span> = <span class="q">&lt;STDIN&gt;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If you chomp a list, each element is chomped, and the total number of
characters removed is returned.</p>
<p>Note that parentheses are necessary when you&#39;re chomping anything
that is not a simple variable.  This is because <code class="inline"><a class="l_k" href="../functions/chomp.html">chomp</a> <span class="i">$cwd</span> = <span class="q">`pwd`</span><span class="sc">;</span></code>

is interpreted as <code class="inline"><span class="s">(</span><a class="l_k" href="../functions/chomp.html">chomp</a> <span class="i">$cwd</span><span class="s">)</span> = <span class="q">`pwd`</span><span class="sc">;</span></code>
, rather than as
<code class="inline"><a class="l_k" href="../functions/chomp.html">chomp</a><span class="s">(</span> <span class="i">$cwd</span> = <span class="q">`pwd`</span> <span class="s">)</span></code>
 which you might expect.  Similarly,
<code class="inline"><a class="l_k" href="../functions/chomp.html">chomp</a> <span class="i">$a</span><span class="cm">,</span> <span class="i">$b</span></code>
 is interpreted as <code class="inline"><a class="l_k" href="../functions/chomp.html">chomp</a><span class="s">(</span><span class="i">$a</span><span class="s">)</span><span class="cm">,</span> <span class="i">$b</span></code>
 rather than
as <code class="inline"><a class="l_k" href="../functions/chomp.html">chomp</a><span class="s">(</span><span class="i">$a</span><span class="cm">,</span> <span class="i">$b</span><span class="s">)</span></code>
.
-----

function chomp
( list )|
This safer version of <a href="../functions/chop.html">"chop"</a> removes any trailing string
that corresponds to the current value of <code class="inline"><span class="i">$/</span></code>
 (also known as
$INPUT_RECORD_SEPARATOR in the <code class="inline"><span class="w">English</span></code>
 module).  It returns the total
number of characters removed from all its arguments.  It&#39;s often used to
remove the newline from the end of an input record when you&#39;re worried
that the final record may be missing its newline.  When in paragraph
mode (<code class="inline"><span class="i">$/</span> = <span class="q">&quot;&quot;</span></code>
), it removes all trailing newlines from the string.
When in slurp mode (<code class="inline"><span class="i">$/</span> = <a class="l_k" href="../functions/undef.html">undef</a></code>
) or fixed-length record mode (<code class="inline"><span class="i">$/</span></code>
 is
a reference to an integer or the like, see <a href="../perlvar.html">perlvar</a>) chomp() won&#39;t
remove anything.
If VARIABLE is omitted, it chomps <code class="inline"><span class="i">$_</span></code>
.  Example:</p>
<pre class="verbatim"><ol><li>    while <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/chomp.html">chomp</a><span class="sc">;</span>	<span class="c"># avoid \n on last field</span></li><li>	<span class="i">@array</span> = <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/:/</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="c"># ...</span></li><li>    <span class="s">}</span></li></ol></pre><p>If VARIABLE is a hash, it chomps the hash&#39;s values, but not its keys.</p>
<p>You can actually chomp anything that&#39;s an lvalue, including an assignment:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/chomp.html">chomp</a><span class="s">(</span><span class="i">$cwd</span> = <span class="q">`pwd`</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/chomp.html">chomp</a><span class="s">(</span><span class="i">$answer</span> = <span class="q">&lt;STDIN&gt;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If you chomp a list, each element is chomped, and the total number of
characters removed is returned.</p>
<p>Note that parentheses are necessary when you&#39;re chomping anything
that is not a simple variable.  This is because <code class="inline"><a class="l_k" href="../functions/chomp.html">chomp</a> <span class="i">$cwd</span> = <span class="q">`pwd`</span><span class="sc">;</span></code>

is interpreted as <code class="inline"><span class="s">(</span><a class="l_k" href="../functions/chomp.html">chomp</a> <span class="i">$cwd</span><span class="s">)</span> = <span class="q">`pwd`</span><span class="sc">;</span></code>
, rather than as
<code class="inline"><a class="l_k" href="../functions/chomp.html">chomp</a><span class="s">(</span> <span class="i">$cwd</span> = <span class="q">`pwd`</span> <span class="s">)</span></code>
 which you might expect.  Similarly,
<code class="inline"><a class="l_k" href="../functions/chomp.html">chomp</a> <span class="i">$a</span><span class="cm">,</span> <span class="i">$b</span></code>
 is interpreted as <code class="inline"><a class="l_k" href="../functions/chomp.html">chomp</a><span class="s">(</span><span class="i">$a</span><span class="s">)</span><span class="cm">,</span> <span class="i">$b</span></code>
 rather than
as <code class="inline"><a class="l_k" href="../functions/chomp.html">chomp</a><span class="s">(</span><span class="i">$a</span><span class="cm">,</span> <span class="i">$b</span><span class="s">)</span></code>
.
-----

function chomp
This safer version of <a href="../functions/chop.html">"chop"</a> removes any trailing string
that corresponds to the current value of <code class="inline"><span class="i">$/</span></code>
 (also known as
$INPUT_RECORD_SEPARATOR in the <code class="inline"><span class="w">English</span></code>
 module).  It returns the total
number of characters removed from all its arguments.  It&#39;s often used to
remove the newline from the end of an input record when you&#39;re worried
that the final record may be missing its newline.  When in paragraph
mode (<code class="inline"><span class="i">$/</span> = <span class="q">&quot;&quot;</span></code>
), it removes all trailing newlines from the string.
When in slurp mode (<code class="inline"><span class="i">$/</span> = <a class="l_k" href="../functions/undef.html">undef</a></code>
) or fixed-length record mode (<code class="inline"><span class="i">$/</span></code>
 is
a reference to an integer or the like, see <a href="../perlvar.html">perlvar</a>) chomp() won&#39;t
remove anything.
If VARIABLE is omitted, it chomps <code class="inline"><span class="i">$_</span></code>
.  Example:</p>
<pre class="verbatim"><ol><li>    while <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/chomp.html">chomp</a><span class="sc">;</span>	<span class="c"># avoid \n on last field</span></li><li>	<span class="i">@array</span> = <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/:/</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="c"># ...</span></li><li>    <span class="s">}</span></li></ol></pre><p>If VARIABLE is a hash, it chomps the hash&#39;s values, but not its keys.</p>
<p>You can actually chomp anything that&#39;s an lvalue, including an assignment:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/chomp.html">chomp</a><span class="s">(</span><span class="i">$cwd</span> = <span class="q">`pwd`</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/chomp.html">chomp</a><span class="s">(</span><span class="i">$answer</span> = <span class="q">&lt;STDIN&gt;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If you chomp a list, each element is chomped, and the total number of
characters removed is returned.</p>
<p>Note that parentheses are necessary when you&#39;re chomping anything
that is not a simple variable.  This is because <code class="inline"><a class="l_k" href="../functions/chomp.html">chomp</a> <span class="i">$cwd</span> = <span class="q">`pwd`</span><span class="sc">;</span></code>

is interpreted as <code class="inline"><span class="s">(</span><a class="l_k" href="../functions/chomp.html">chomp</a> <span class="i">$cwd</span><span class="s">)</span> = <span class="q">`pwd`</span><span class="sc">;</span></code>
, rather than as
<code class="inline"><a class="l_k" href="../functions/chomp.html">chomp</a><span class="s">(</span> <span class="i">$cwd</span> = <span class="q">`pwd`</span> <span class="s">)</span></code>
 which you might expect.  Similarly,
<code class="inline"><a class="l_k" href="../functions/chomp.html">chomp</a> <span class="i">$a</span><span class="cm">,</span> <span class="i">$b</span></code>
 is interpreted as <code class="inline"><a class="l_k" href="../functions/chomp.html">chomp</a><span class="s">(</span><span class="i">$a</span><span class="s">)</span><span class="cm">,</span> <span class="i">$b</span></code>
 rather than
as <code class="inline"><a class="l_k" href="../functions/chomp.html">chomp</a><span class="s">(</span><span class="i">$a</span><span class="cm">,</span> <span class="i">$b</span><span class="s">)</span></code>
.
-----

function chop
variable|
Chops off the last character of a string and returns the character
chopped.  It is much more efficient than <code class="inline"><a class="l_k" href="../functions/s.html">s/.$//s</a></code> because it neither
scans nor copies the string.  If VARIABLE is omitted, chops <code class="inline"><span class="i">$_</span></code>
.
If VARIABLE is a hash, it chops the hash&#39;s values, but not its keys.</p>
<p>You can actually chop anything that&#39;s an lvalue, including an assignment.</p>
<p>If you chop a list, each element is chopped.  Only the value of the
last <code class="inline"><a class="l_k" href="../functions/chop.html">chop</a></code> is returned.</p>
<p>Note that <code class="inline"><a class="l_k" href="../functions/chop.html">chop</a></code> returns the last character.  To return all but the last
character, use <code class="inline"><a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$string</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">-1</span><span class="s">)</span></code>
.</p>
<p>See also <a href="../functions/chomp.html">"chomp"</a>.
-----

function chop
( list )|
Chops off the last character of a string and returns the character
chopped.  It is much more efficient than <code class="inline"><a class="l_k" href="../functions/s.html">s/.$//s</a></code> because it neither
scans nor copies the string.  If VARIABLE is omitted, chops <code class="inline"><span class="i">$_</span></code>
.
If VARIABLE is a hash, it chops the hash&#39;s values, but not its keys.</p>
<p>You can actually chop anything that&#39;s an lvalue, including an assignment.</p>
<p>If you chop a list, each element is chopped.  Only the value of the
last <code class="inline"><a class="l_k" href="../functions/chop.html">chop</a></code> is returned.</p>
<p>Note that <code class="inline"><a class="l_k" href="../functions/chop.html">chop</a></code> returns the last character.  To return all but the last
character, use <code class="inline"><a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$string</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">-1</span><span class="s">)</span></code>
.</p>
<p>See also <a href="../functions/chomp.html">"chomp"</a>.
-----

function chop
Chops off the last character of a string and returns the character
chopped.  It is much more efficient than <code class="inline"><a class="l_k" href="../functions/s.html">s/.$//s</a></code> because it neither
scans nor copies the string.  If VARIABLE is omitted, chops <code class="inline"><span class="i">$_</span></code>
.
If VARIABLE is a hash, it chops the hash&#39;s values, but not its keys.</p>
<p>You can actually chop anything that&#39;s an lvalue, including an assignment.</p>
<p>If you chop a list, each element is chopped.  Only the value of the
last <code class="inline"><a class="l_k" href="../functions/chop.html">chop</a></code> is returned.</p>
<p>Note that <code class="inline"><a class="l_k" href="../functions/chop.html">chop</a></code> returns the last character.  To return all but the last
character, use <code class="inline"><a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$string</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">-1</span><span class="s">)</span></code>
.</p>
<p>See also <a href="../functions/chomp.html">"chomp"</a>.
-----

function chown
list|
Changes the owner (and group) of a list of files.  The first two
elements of the list must be the <i>numeric</i> uid and gid, in that
order.  A value of -1 in either position is interpreted by most
systems to leave that value unchanged.  Returns the number of files
successfully changed.</p>
<pre class="verbatim"><ol><li>    <span class="i">$cnt</span> = <a class="l_k" href="../functions/chown.html">chown</a> <span class="i">$uid</span><span class="cm">,</span> <span class="i">$gid</span><span class="cm">,</span> <span class="q">&#39;foo&#39;</span><span class="cm">,</span> <span class="q">&#39;bar&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/chown.html">chown</a> <span class="i">$uid</span><span class="cm">,</span> <span class="i">$gid</span><span class="cm">,</span> <span class="i">@filenames</span><span class="sc">;</span></li></ol></pre><p>On systems that support fchown, you might pass file handles among the
files.  On systems that don&#39;t support fchown, passing file handles
produces a fatal error at run time.  The file handles must be passed
as globs or references to be recognized.  Barewords are considered
file names.</p>
<p>Here&#39;s an example that looks up nonnumeric uids in the passwd file:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;User: &quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/chomp.html">chomp</a><span class="s">(</span><span class="i">$user</span> = <span class="q">&lt;STDIN&gt;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Files: &quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/chomp.html">chomp</a><span class="s">(</span><span class="i">$pattern</span> = <span class="q">&lt;STDIN&gt;</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="s">(</span><span class="i">$login</span><span class="cm">,</span><span class="i">$pass</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="s">)</span> = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$user</span><span class="s">)</span></li><li>	or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;$user not in passwd file&quot;</span><span class="sc">;</span></li><li></li><li>    <span class="i">@ary</span> = <a class="l_k" href="../functions/glob.html">glob</a><span class="s">(</span><span class="i">$pattern</span><span class="s">)</span><span class="sc">;</span>	<span class="c"># expand filenames</span></li><li>    <a class="l_k" href="../functions/chown.html">chown</a> <span class="i">$uid</span><span class="cm">,</span> <span class="i">$gid</span><span class="cm">,</span> <span class="i">@ary</span><span class="sc">;</span></li></ol></pre><p>On most systems, you are not allowed to change the ownership of the
file unless you&#39;re the superuser, although you should be able to change
the group to any of your secondary groups.  On insecure systems, these
restrictions may be relaxed, but this is not a portable assumption.
On POSIX systems, you can detect this condition this way:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">POSIX</span> <span class="q">qw(sysconf _PC_CHOWN_RESTRICTED)</span><span class="sc">;</span></li><li>    <span class="i">$can_chown_giveaway</span> = not <span class="i">sysconf</span><span class="s">(</span><span class="w">_PC_CHOWN_RESTRICTED</span><span class="s">)</span><span class="sc">;</span></li></ol><
-----

function chr
number|
Returns the character represented by that NUMBER in the character set.
For example, <code class="inline"><a class="l_k" href="../functions/chr.html">chr(65)</a></code> is <code class="inline"><span class="q">&quot;A&quot;</span></code>
 in either ASCII or Unicode, and
chr(0x263a) is a Unicode smiley face.</p>
<p>Negative values give the Unicode replacement character (chr(0xfffd)),
except under the <a href="../bytes.html">bytes</a> pragma, where low eight bits of the value
(truncated to an integer) are used.</p>
<p>If NUMBER is omitted, uses <code class="inline"><span class="i">$_</span></code>
.</p>
<p>For the reverse, use <a href="../functions/ord.html">"ord"</a>.</p>
<p>Note that characters from 128 to 255 (inclusive) are by default
internally not encoded as UTF-8 for backward compatibility reasons.</p>
<p>See <a href="../perlunicode.html">perlunicode</a> for more about Unicode.
-----

function chr
Returns the character represented by that NUMBER in the character set.
For example, <code class="inline"><a class="l_k" href="../functions/chr.html">chr(65)</a></code> is <code class="inline"><span class="q">&quot;A&quot;</span></code>
 in either ASCII or Unicode, and
chr(0x263a) is a Unicode smiley face.</p>
<p>Negative values give the Unicode replacement character (chr(0xfffd)),
except under the <a href="../bytes.html">bytes</a> pragma, where low eight bits of the value
(truncated to an integer) are used.</p>
<p>If NUMBER is omitted, uses <code class="inline"><span class="i">$_</span></code>
.</p>
<p>For the reverse, use <a href="../functions/ord.html">"ord"</a>.</p>
<p>Note that characters from 128 to 255 (inclusive) are by default
internally not encoded as UTF-8 for backward compatibility reasons.</p>
<p>See <a href="../perlunicode.html">perlunicode</a> for more about Unicode.
-----

function chroot
filename|
This function works like the system call by the same name: it makes the
named directory the new root directory for all further pathnames that
begin with a <code class="inline">/</code> by your process and all its children.  (It doesn&#39;t
change your current working directory, which is unaffected.)  For security
reasons, this call is restricted to the superuser.  If FILENAME is
omitted, does a <code class="inline"><a class="l_k" href="../functions/chroot.html">chroot</a></code> to <code class="inline"><span class="i">$_</span></code>
.
-----

function chroot
This function works like the system call by the same name: it makes the
named directory the new root directory for all further pathnames that
begin with a <code class="inline">/</code> by your process and all its children.  (It doesn&#39;t
change your current working directory, which is unaffected.)  For security
reasons, this call is restricted to the superuser.  If FILENAME is
omitted, does a <code class="inline"><a class="l_k" href="../functions/chroot.html">chroot</a></code> to <code class="inline"><span class="i">$_</span></code>
.
-----

function close
filehandle|
Closes the file or pipe associated with the file handle, flushes the IO
buffers, and closes the system file descriptor.  Returns true if those
operations have succeeded and if no error was reported by any PerlIO
layer.  Closes the currently selected filehandle if the argument is
omitted.</p>
<p>You don&#39;t have to close FILEHANDLE if you are immediately going to do
another <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> on it, because <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> will close it for you.  (See
<code class="inline"><a class="l_k" href="../functions/open.html">open</a></code>.)  However, an explicit <code class="inline"><a class="l_k" href="../functions/close.html">close</a></code> on an input file resets the line
counter (<code class="inline"><span class="i">$.</span></code>
), while the implicit close done by <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> does not.</p>
<p>If the file handle came from a piped open, <code class="inline"><a class="l_k" href="../functions/close.html">close</a></code> will additionally
return false if one of the other system calls involved fails, or if the
program exits with non-zero status.  (If the only problem was that the
program exited non-zero, <code class="inline"><span class="i">$!</span></code>
 will be set to <code class="inline"><span class="n">0</span></code>
.)  Closing a pipe
also waits for the process executing on the pipe to complete, in case you
want to look at the output of the pipe afterwards, and
implicitly puts the exit status value of that command into <code class="inline"><span class="i">$?</span></code>
 and
<code class="inline"><span class="i">$</span>{<span class="w">^CHILD_ERROR_NATIVE</span>}</code>
.</p>
<p>Prematurely closing the read end of a pipe (i.e. before the process
writing to it at the other end has closed it) will result in a
SIGPIPE being delivered to the writer.  If the other end can&#39;t
handle that, be sure to read all the data before closing the pipe.</p>
<p>Example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">OUTPUT</span><span class="cm">,</span> <span class="q">&#39;|sort &gt;foo&#39;</span><span class="s">)</span>  <span class="c"># pipe to sort</span></li><li>        or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t start sort: $!&quot;</span><span class="sc">;</span></li><li>    <span class="c">#...			# print stuff to output</span></li><li>    <a class="l_k" href="../functions/close.html">close</a> <span class="w">OUTPUT</span>		<span class="c"># wait for sort to finish</span></li><li>        or <a class="l_k" href="../functions/warn.html">warn</a> <span class="i">$!</span> ? <span class="q">&quot;Error closing sort pipe: $!&quot;</span></li><li>                   <span class="co">:</span> <span class="q">&quot;Exit status $? from sort&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">INPUT</span><span class="cm">,</span> <span class="q">&#39;foo&#39;</span><span class="s">)</span>		<span class="c"># get sort&#39;s results</span></li><li>        or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t open &#39;foo&#39; for input: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>FILEHANDLE may be an expression whose value can be used as an indirect
filehandle, usually the real filehandle name.
-----

function close
Closes the file or pipe associated with the file handle, flushes the IO
buffers, and closes the system file descriptor.  Returns true if those
operations have succeeded and if no error was reported by any PerlIO
layer.  Closes the currently selected filehandle if the argument is
omitted.</p>
<p>You don&#39;t have to close FILEHANDLE if you are immediately going to do
another <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> on it, because <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> will close it for you.  (See
<code class="inline"><a class="l_k" href="../functions/open.html">open</a></code>.)  However, an explicit <code class="inline"><a class="l_k" href="../functions/close.html">close</a></code> on an input file resets the line
counter (<code class="inline"><span class="i">$.</span></code>
), while the implicit close done by <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> does not.</p>
<p>If the file handle came from a piped open, <code class="inline"><a class="l_k" href="../functions/close.html">close</a></code> will additionally
return false if one of the other system calls involved fails, or if the
program exits with non-zero status.  (If the only problem was that the
program exited non-zero, <code class="inline"><span class="i">$!</span></code>
 will be set to <code class="inline"><span class="n">0</span></code>
.)  Closing a pipe
also waits for the process executing on the pipe to complete, in case you
want to look at the output of the pipe afterwards, and
implicitly puts the exit status value of that command into <code class="inline"><span class="i">$?</span></code>
 and
<code class="inline"><span class="i">$</span>{<span class="w">^CHILD_ERROR_NATIVE</span>}</code>
.</p>
<p>Prematurely closing the read end of a pipe (i.e. before the process
writing to it at the other end has closed it) will result in a
SIGPIPE being delivered to the writer.  If the other end can&#39;t
handle that, be sure to read all the data before closing the pipe.</p>
<p>Example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">OUTPUT</span><span class="cm">,</span> <span class="q">&#39;|sort &gt;foo&#39;</span><span class="s">)</span>  <span class="c"># pipe to sort</span></li><li>        or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t start sort: $!&quot;</span><span class="sc">;</span></li><li>    <span class="c">#...			# print stuff to output</span></li><li>    <a class="l_k" href="../functions/close.html">close</a> <span class="w">OUTPUT</span>		<span class="c"># wait for sort to finish</span></li><li>        or <a class="l_k" href="../functions/warn.html">warn</a> <span class="i">$!</span> ? <span class="q">&quot;Error closing sort pipe: $!&quot;</span></li><li>                   <span class="co">:</span> <span class="q">&quot;Exit status $? from sort&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">INPUT</span><span class="cm">,</span> <span class="q">&#39;foo&#39;</span><span class="s">)</span>		<span class="c"># get sort&#39;s results</span></li><li>        or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t open &#39;foo&#39; for input: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>FILEHANDLE may be an expression whose value can be used as an indirect
filehandle, usually the real filehandle name.
-----

function closedir
dirhandle|
Closes a directory opened by <code class="inline"><a class="l_k" href="../functions/opendir.html">opendir</a></code> and returns the success of that
system call.
-----

function connect
socket|
name|
Attempts to connect to a remote socket, just as the connect system call
does.  Returns true if it succeeded, false otherwise.  NAME should be a
packed address of the appropriate type for the socket.  See the examples in
<a href="../perlipc.html#Sockets%3a-Client%2fServer-Communication">"Sockets: Client/Server Communication" in perlipc</a>.
-----

function continue
block|
<code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code> is actually a flow control statement rather than a function.  If
there is a <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code> BLOCK attached to a BLOCK (typically in a <code class="inline">while</code>
 or
<code class="inline">foreach</code>
), it is always executed just before the conditional is about to
be evaluated again, just like the third part of a <code class="inline">for</code>
 loop in C.  Thus
it can be used to increment a loop variable, even when the loop has been
continued via the <code class="inline"><a class="l_k" href="../functions/next.html">next</a></code> statement (which is similar to the C <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code>
statement).</p>
<p><code class="inline"><a class="l_k" href="../functions/last.html">last</a></code>, <code class="inline"><a class="l_k" href="../functions/next.html">next</a></code>, or <code class="inline"><a class="l_k" href="../functions/redo.html">redo</a></code> may appear within a <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code>
block.  <code class="inline"><a class="l_k" href="../functions/last.html">last</a></code> and <code class="inline"><a class="l_k" href="../functions/redo.html">redo</a></code> will behave as if they had been executed within
the main block.  So will <code class="inline"><a class="l_k" href="../functions/next.html">next</a></code>, but since it will execute a <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code>
block, it may be more entertaining.</p>
<pre class="verbatim"><ol><li>    while <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="s">{</span></li><li>	<span class="c">### redo always comes here</span></li><li>	<span class="w">do_something</span><span class="sc">;</span></li><li>    <span class="s">}</span> <a class="l_k" href="../functions/continue.html">continue</a> <span class="s">{</span></li><li>	<span class="c">### next always comes here</span></li><li>	<span class="w">do_something_else</span><span class="sc">;</span></li><li>	<span class="c"># then back the top to re-check EXPR</span></li><li>    <span class="s">}</span></li><li>    <span class="c">### last always comes here</span></li></ol></pre><p>Omitting the <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code> section is semantically equivalent to using an
empty one, logically enough.  In that case, <code class="inline"><a class="l_k" href="../functions/next.html">next</a></code> goes directly back
to check the condition at the top of the loop.</p>
<p>If the &quot;switch&quot; feature is enabled, <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code> is also a
function that will break out of the current <code class="inline">when</code>
 or <code class="inline"><span class="w">default</span></code>

block, and fall through to the next case. See <a href="../feature.html">feature</a> and
<a href="../perlsyn.html#Switch-statements">"Switch statements" in perlsyn</a> for more information.
-----

function continue
<code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code> is actually a flow control statement rather than a function.  If
there is a <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code> BLOCK attached to a BLOCK (typically in a <code class="inline">while</code>
 or
<code class="inline">foreach</code>
), it is always executed just before the conditional is about to
be evaluated again, just like the third part of a <code class="inline">for</code>
 loop in C.  Thus
it can be used to increment a loop variable, even when the loop has been
continued via the <code class="inline"><a class="l_k" href="../functions/next.html">next</a></code> statement (which is similar to the C <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code>
statement).</p>
<p><code class="inline"><a class="l_k" href="../functions/last.html">last</a></code>, <code class="inline"><a class="l_k" href="../functions/next.html">next</a></code>, or <code class="inline"><a class="l_k" href="../functions/redo.html">redo</a></code> may appear within a <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code>
block.  <code class="inline"><a class="l_k" href="../functions/last.html">last</a></code> and <code class="inline"><a class="l_k" href="../functions/redo.html">redo</a></code> will behave as if they had been executed within
the main block.  So will <code class="inline"><a class="l_k" href="../functions/next.html">next</a></code>, but since it will execute a <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code>
block, it may be more entertaining.</p>
<pre class="verbatim"><ol><li>    while <span class="s">(</span><span class="w">EXPR</span><span class="s">)</span> <span class="s">{</span></li><li>	<span class="c">### redo always comes here</span></li><li>	<span class="w">do_something</span><span class="sc">;</span></li><li>    <span class="s">}</span> <a class="l_k" href="../functions/continue.html">continue</a> <span class="s">{</span></li><li>	<span class="c">### next always comes here</span></li><li>	<span class="w">do_something_else</span><span class="sc">;</span></li><li>	<span class="c"># then back the top to re-check EXPR</span></li><li>    <span class="s">}</span></li><li>    <span class="c">### last always comes here</span></li></ol></pre><p>Omitting the <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code> section is semantically equivalent to using an
empty one, logically enough.  In that case, <code class="inline"><a class="l_k" href="../functions/next.html">next</a></code> goes directly back
to check the condition at the top of the loop.</p>
<p>If the &quot;switch&quot; feature is enabled, <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code> is also a
function that will break out of the current <code class="inline">when</code>
 or <code class="inline"><span class="w">default</span></code>

block, and fall through to the next case. See <a href="../feature.html">feature</a> and
<a href="../perlsyn.html#Switch-statements">"Switch statements" in perlsyn</a> for more information.
-----

function cos
expr|
Returns the cosine of EXPR (expressed in radians).  If EXPR is omitted,
takes cosine of <code class="inline"><span class="i">$_</span></code>
.</p>
<p>For the inverse cosine operation, you may use the <code class="inline"><span class="i">Math::Trig::acos</span><span class="s">(</span><span class="s">)</span></code>

function, or use this relation:</p>
<pre class="verbatim"><ol><li><a name="acos"></a>    sub <span class="m">acos</span> <span class="s">{</span> <a class="l_k" href="../functions/atan2.html">atan2</a><span class="s">(</span> <a class="l_k" href="../functions/sqrt.html">sqrt</a><span class="s">(</span><span class="n">1</span> - <span class="i">$_</span>[<span class="n">0</span>] * <span class="i">$_</span>[<span class="n">0</span>]<span class="s">)</span><span class="cm">,</span> <span class="i">$_</span>[<span class="n">0</span>] <span class="s">)</span> <span class="s">}</span></li></ol><
-----

function cos
Returns the cosine of EXPR (expressed in radians).  If EXPR is omitted,
takes cosine of <code class="inline"><span class="i">$_</span></code>
.</p>
<p>For the inverse cosine operation, you may use the <code class="inline"><span class="i">Math::Trig::acos</span><span class="s">(</span><span class="s">)</span></code>

function, or use this relation:</p>
<pre class="verbatim"><ol><li><a name="acos"></a>    sub <span class="m">acos</span> <span class="s">{</span> <a class="l_k" href="../functions/atan2.html">atan2</a><span class="s">(</span> <a class="l_k" href="../functions/sqrt.html">sqrt</a><span class="s">(</span><span class="n">1</span> - <span class="i">$_</span>[<span class="n">0</span>] * <span class="i">$_</span>[<span class="n">0</span>]<span class="s">)</span><span class="cm">,</span> <span class="i">$_</span>[<span class="n">0</span>] <span class="s">)</span> <span class="s">}</span></li></ol><
-----

function crypt
plaintext|
salt|
     
   </p>
<p>Creates a digest string exactly like the crypt(3) function in the C
library (assuming that you actually have a version there that has not
been extirpated as a potential munition).</p>
<p>crypt() is a one-way hash function.  The PLAINTEXT and SALT is turned
into a short string, called a digest, which is returned.  The same
PLAINTEXT and SALT will always return the same string, but there is no
(known) way to get the original PLAINTEXT from the hash.  Small
changes in the PLAINTEXT or SALT will result in large changes in the
digest.</p>
<p>There is no decrypt function.  This function isn&#39;t all that useful for
cryptography (for that, look for <i>Crypt</i> modules on your nearby CPAN
mirror) and the name &quot;crypt&quot; is a bit of a misnomer.  Instead it is
primarily used to check if two pieces of text are the same without
having to transmit or store the text itself.  An example is checking
if a correct password is given.  The digest of the password is stored,
not the password itself.  The user types in a password that is
crypt()&#39;d with the same salt as the stored digest.  If the two digests
match the password is correct.</p>
<p>When verifying an existing digest string you should use the digest as
the salt (like <code class="inline"><a class="l_k" href="../functions/crypt.html">crypt</a><span class="s">(</span><span class="i">$plain</span><span class="cm">,</span> <span class="i">$digest</span><span class="s">)</span> eq <span class="i">$digest</span></code>
).  The SALT used
to create the digest is visible as part of the digest.  This ensures
crypt() will hash the new string with the same salt as the digest.
This allows your code to work with the standard <a href="../functions/crypt.html">crypt</a> and
with more exotic implementations.  In other words, do not assume
anything about the returned string itself, or how many bytes in the
digest matter.</p>
<p>Traditionally the result is a string of 13 bytes: two first bytes of
the salt, followed by 11 bytes from the set <code class="inline">[./0-9A-Za-z]</code>, and only
the first eight bytes of PLAINTEXT mattered. But alternative
hashing schemes (like MD5), higher level security schemes (like C2),
and implementations on non-UNIX platforms may produce different
strings.</p>
<p>When choosing a new salt create a random two character string whose
characters come from the set <code class="inline">[./0-9A-Za-z]</code> (like <code class="inline"><a class="l_k" href="../functions/join.html">join</a> <span class="q">&#39;&#39;</span><span class="cm">,</span> <span class="s">(</span><span class="q">&#39;.&#39;</span><span class="cm">,</span>
<span class="q">&#39;/&#39;</span><span class="cm">,</span> <span class="n">0</span>..<span class="n">9</span><span class="cm">,</span> <span class="q">&#39;A&#39;</span>..<span class="q">&#39;Z&#39;</span><span class="cm">,</span> <span class="q">&#39;a&#39;</span>..<span class="q">&#39;z&#39;</span><span class="s">)</span>[<a class="l_k" href="../functions/rand.html">rand</a> <span class="n">64</span><span class="cm">,</span> <a class="l_k" href="../functions/rand.html">rand</a> <span class="n">64</span>]</code>
).  This set of
characters is just a recommendation; the characters allowed in
the salt depend solely on your system&#39;s crypt library, and Perl can&#39;t
restrict what salts <code class="inline"><a class="l_k" href="../functions/crypt.html">crypt()</a></code> accepts.</p>
<p>Here&#39;s an example that makes sure that whoever runs this program knows
their password:</p>
<pre class="verbatim"><ol><li>    <span class="i">$pwd</span> = <span class="s">(</span><a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$&lt;</span><span class="s">)</span><span class="s">)</span>[<span class="n">1</span>]<span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/system.html">system</a> <span class="q">&quot;stty -echo&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Password: &quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/chomp.html">chomp</a><span class="s">(</span><span class="i">$word</span> = <span class="q">&lt;STDIN&gt;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/system.html">system</a> <span class="q">&quot;stty echo&quot;</span><span class="sc">;</span></li><li></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/crypt.html">crypt</a><span class="s">(</span><span class="i">$word</span><span class="cm">,</span> <span class="i">$pwd</span><span class="s">)</span> ne <span class="i">$pwd</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Sorry...\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span> else <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;ok\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Of course, typing in your own password to whoever asks you
for it is unwise.</p>
<p>The <a href="../functions/crypt.html">crypt</a> function is unsuitable for hashing large quantities
of data, not least of all because you can&#39;t get the information
back.  Look at the <a href="../Digest.html">Digest</a> module for more robust algorithms.</p>
<p>If using crypt() on a Unicode string (which <i>potentially</i> has
characters with codepoints above 255), Perl tries to make sense
of the situation by trying to downgrade (a copy of the string)
the string back to an eight-bit byte string before calling crypt()
(on that copy).  If that works, good.  If not, crypt() dies with
<code class="inline"><span class="w">Wide</span> <span class="w">character</span> <span class="w">in</span> <a class="l_k" href="../functions/crypt.html">crypt</a></code>
.
-----

function dbmclose
hash|
[This function has been largely superseded by the <code class="inline"><a class="l_k" href="../functions/untie.html">untie</a></code> function.]</p>
<p>Breaks the binding between a DBM file and a hash.
-----

function dbmopen
hash|
dbname|
mask|
[This function has been largely superseded by the <code class="inline"><a class="l_k" href="../functions/tie.html">tie</a></code> function.]</p>
<p>This binds a dbm(3), ndbm(3), sdbm(3), gdbm(3), or Berkeley DB file to a
hash.  HASH is the name of the hash.  (Unlike normal <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code>, the first
argument is <i>not</i> a filehandle, even though it looks like one).  DBNAME
is the name of the database (without the <i>.dir</i> or <i>.pag</i> extension if
any).  If the database does not exist, it is created with protection
specified by MASK (as modified by the <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code>).  If your system supports
only the older DBM functions, you may perform only one <code class="inline"><a class="l_k" href="../functions/dbmopen.html">dbmopen</a></code> in your
program.  In older versions of Perl, if your system had neither DBM nor
ndbm, calling <code class="inline"><a class="l_k" href="../functions/dbmopen.html">dbmopen</a></code> produced a fatal error; it now falls back to
sdbm(3).</p>
<p>If you don&#39;t have write access to the DBM file, you can only read hash
variables, not set them.  If you want to test whether you can write,
either use file tests or try setting a dummy hash entry inside an <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code>,
which will trap the error.</p>
<p>Note that functions such as <code class="inline"><a class="l_k" href="../functions/keys.html">keys</a></code> and <code class="inline"><a class="l_k" href="../functions/values.html">values</a></code> may return huge lists
when used on large DBM files.  You may prefer to use the <code class="inline"><a class="l_k" href="../functions/each.html">each</a></code>
function to iterate over large DBM files.  Example:</p>
<pre class="verbatim"><ol><li>    <span class="c"># print out history file offsets</span></li><li>    <a class="l_k" href="../functions/dbmopen.html">dbmopen</a><span class="s">(</span><span class="i">%HIST</span><span class="cm">,</span><span class="q">&#39;/usr/lib/news/history&#39;</span><span class="cm">,</span><span class="n">0666</span><span class="s">)</span><span class="sc">;</span></li><li>    while <span class="s">(</span><span class="s">(</span><span class="i">$key</span><span class="cm">,</span><span class="i">$val</span><span class="s">)</span> = <a class="l_k" href="../functions/each.html">each</a> <span class="i">%HIST</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="i">$key</span><span class="cm">,</span> <span class="q">&#39; = &#39;</span><span class="cm">,</span> <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&#39;L&#39;</span><span class="cm">,</span><span class="i">$val</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    <a class="l_k" href="../functions/dbmclose.html">dbmclose</a><span class="s">(</span><span class="i">%HIST</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>See also <a href="../AnyDBM_File.html">AnyDBM_File</a> for a more general description of the pros and
cons of the various dbm approaches, as well as <a href="../DB_File.html">DB_File</a> for a particularly
rich implementation.</p>
<p>You can control which DBM library you use by loading that library
before you call dbmopen():</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">DB_File</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/dbmopen.html">dbmopen</a><span class="s">(</span><span class="i">%NS_Hist</span><span class="cm">,</span> <span class="q">&quot;$ENV{HOME}/.netscape/history.db&quot;</span><span class="s">)</span></li><li>	or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t open netscape history file: $!&quot;</span><span class="sc">;</span></li></ol><
-----

function defined
expr|
Returns a Boolean value telling whether EXPR has a value other than
the undefined value <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>.  If EXPR is not present, <code class="inline"><span class="i">$_</span></code>
 will be
checked.</p>
<p>Many operations return <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> to indicate failure, end of file,
system error, uninitialized variable, and other exceptional
conditions.  This function allows you to distinguish <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> from
other values.  (A simple Boolean test will not distinguish among
<code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>, zero, the empty string, and <code class="inline"><span class="q">&quot;0&quot;</span></code>
, which are all equally
false.)  Note that since <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> is a valid scalar, its presence
doesn&#39;t <i>necessarily</i> indicate an exceptional condition: <code class="inline"><a class="l_k" href="../functions/pop.html">pop</a></code>
returns <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> when its argument is an empty array, <i>or</i> when the
element to return happens to be <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>.</p>
<p>You may also use <code class="inline"><a class="l_k" href="../functions/defined.html">defined(&func)</a></code> to check whether subroutine <code class="inline"><span class="i">&amp;func</span></code>

has ever been defined.  The return value is unaffected by any forward
declarations of <code class="inline"><span class="i">&amp;func</span></code>
.  Note that a subroutine which is not defined
may still be callable: its package may have an <code class="inline">AUTOLOAD</code>
 method that
makes it spring into existence the first time that it is called -- see
<a href="../perlsub.html">perlsub</a>.</p>
<p>Use of <code class="inline"><a class="l_k" href="../functions/defined.html">defined</a></code> on aggregates (hashes and arrays) is deprecated.  It
used to report whether memory for that aggregate has ever been
allocated.  This behavior may disappear in future versions of Perl.
You should instead use a simple test for size:</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span><span class="i">@an_array</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;has array elements\n&quot;</span> <span class="s">}</span></li><li>    if <span class="s">(</span><span class="i">%a_hash</span><span class="s">)</span>   <span class="s">{</span> <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;has hash members\n&quot;</span>   <span class="s">}</span></li></ol></pre><p>When used on a hash element, it tells you whether the value is defined,
not whether the key exists in the hash.  Use <a href="../functions/exists.html">"exists"</a> for the latter
purpose.</p>
<p>Examples:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> if <a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$switch</span>{<span class="q">&#39;D&#39;</span>}<span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;$val\n&quot;</span> while <a class="l_k" href="../functions/defined.html">defined</a><span class="s">(</span><span class="i">$val</span> = <a class="l_k" href="../functions/pop.html">pop</a><span class="s">(</span><span class="i">@ary</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t readlink $sym: $!&quot;</span></li><li>	unless <a class="l_k" href="../functions/defined.html">defined</a><span class="s">(</span><span class="i">$value</span> = <a class="l_k" href="../functions/readlink.html">readlink</a> <span class="i">$sym</span><span class="s">)</span><span class="sc">;</span></li><li><a name="foo"></a>    sub <span class="m">foo</span> <span class="s">{</span> <a class="l_k" href="../functions/defined.html">defined</a> <span class="i">&amp;$bar</span> ? <span class="i">&amp;$bar</span><span class="s">(</span><span class="i">@_</span><span class="s">)</span> <span class="co">:</span> <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;No bar&quot;</span><span class="sc">;</span> <span class="s">}</span></li><li>    <span class="i">$debugging</span> = <span class="n">0</span> unless <a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$debugging</span><span class="sc">;</span></li></ol></pre><p>Note:  Many folks tend to overuse <code class="inline"><a class="l_k" href="../functions/defined.html">defined</a></code>, and then are surprised to
discover that the number <code class="inline"><span class="n">0</span></code>
 and <code class="inline"><span class="q">&quot;&quot;</span></code>
 (the zero-length string) are, in fact,
defined values.  For example, if you say</p>
<pre class="verbatim"><ol><li>    <span class="q">&quot;ab&quot;</span> =~ <span class="q">/a(.*)b/</span><span class="sc">;</span></li></ol></pre><p>The pattern match succeeds, and <code class="inline"><span class="i">$1</span></code>
 is defined, despite the fact that it
matched &quot;nothing&quot;.  It didn&#39;t really fail to match anything.  Rather, it
matched something that happened to be zero characters long.  This is all
very above-board and honest.  When a function returns an undefined value,
it&#39;s an admission that it couldn&#39;t give you an honest answer.  So you
should use <code class="inline"><a class="l_k" href="../functions/defined.html">defined</a></code> only when you&#39;re questioning the integrity of what
you&#39;re trying to do.  At other times, a simple comparison to <code class="inline"><span class="n">0</span></code>
 or <code class="inline"><span class="q">&quot;&quot;</span></code>
 is
what you want.</p>
<p>See also <a href="../functions/undef.html">"undef"</a>, <a href="../functions/exists.html">"exists"</a>, <a href="../functions/ref.html">"ref"</a>.
-----

function defined
Returns a Boolean value telling whether EXPR has a value other than
the undefined value <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>.  If EXPR is not present, <code class="inline"><span class="i">$_</span></code>
 will be
checked.</p>
<p>Many operations return <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> to indicate failure, end of file,
system error, uninitialized variable, and other exceptional
conditions.  This function allows you to distinguish <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> from
other values.  (A simple Boolean test will not distinguish among
<code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>, zero, the empty string, and <code class="inline"><span class="q">&quot;0&quot;</span></code>
, which are all equally
false.)  Note that since <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> is a valid scalar, its presence
doesn&#39;t <i>necessarily</i> indicate an exceptional condition: <code class="inline"><a class="l_k" href="../functions/pop.html">pop</a></code>
returns <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> when its argument is an empty array, <i>or</i> when the
element to return happens to be <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>.</p>
<p>You may also use <code class="inline"><a class="l_k" href="../functions/defined.html">defined(&func)</a></code> to check whether subroutine <code class="inline"><span class="i">&amp;func</span></code>

has ever been defined.  The return value is unaffected by any forward
declarations of <code class="inline"><span class="i">&amp;func</span></code>
.  Note that a subroutine which is not defined
may still be callable: its package may have an <code class="inline">AUTOLOAD</code>
 method that
makes it spring into existence the first time that it is called -- see
<a href="../perlsub.html">perlsub</a>.</p>
<p>Use of <code class="inline"><a class="l_k" href="../functions/defined.html">defined</a></code> on aggregates (hashes and arrays) is deprecated.  It
used to report whether memory for that aggregate has ever been
allocated.  This behavior may disappear in future versions of Perl.
You should instead use a simple test for size:</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span><span class="i">@an_array</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;has array elements\n&quot;</span> <span class="s">}</span></li><li>    if <span class="s">(</span><span class="i">%a_hash</span><span class="s">)</span>   <span class="s">{</span> <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;has hash members\n&quot;</span>   <span class="s">}</span></li></ol></pre><p>When used on a hash element, it tells you whether the value is defined,
not whether the key exists in the hash.  Use <a href="../functions/exists.html">"exists"</a> for the latter
purpose.</p>
<p>Examples:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> if <a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$switch</span>{<span class="q">&#39;D&#39;</span>}<span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;$val\n&quot;</span> while <a class="l_k" href="../functions/defined.html">defined</a><span class="s">(</span><span class="i">$val</span> = <a class="l_k" href="../functions/pop.html">pop</a><span class="s">(</span><span class="i">@ary</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t readlink $sym: $!&quot;</span></li><li>	unless <a class="l_k" href="../functions/defined.html">defined</a><span class="s">(</span><span class="i">$value</span> = <a class="l_k" href="../functions/readlink.html">readlink</a> <span class="i">$sym</span><span class="s">)</span><span class="sc">;</span></li><li><a name="foo"></a>    sub <span class="m">foo</span> <span class="s">{</span> <a class="l_k" href="../functions/defined.html">defined</a> <span class="i">&amp;$bar</span> ? <span class="i">&amp;$bar</span><span class="s">(</span><span class="i">@_</span><span class="s">)</span> <span class="co">:</span> <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;No bar&quot;</span><span class="sc">;</span> <span class="s">}</span></li><li>    <span class="i">$debugging</span> = <span class="n">0</span> unless <a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$debugging</span><span class="sc">;</span></li></ol></pre><p>Note:  Many folks tend to overuse <code class="inline"><a class="l_k" href="../functions/defined.html">defined</a></code>, and then are surprised to
discover that the number <code class="inline"><span class="n">0</span></code>
 and <code class="inline"><span class="q">&quot;&quot;</span></code>
 (the zero-length string) are, in fact,
defined values.  For example, if you say</p>
<pre class="verbatim"><ol><li>    <span class="q">&quot;ab&quot;</span> =~ <span class="q">/a(.*)b/</span><span class="sc">;</span></li></ol></pre><p>The pattern match succeeds, and <code class="inline"><span class="i">$1</span></code>
 is defined, despite the fact that it
matched &quot;nothing&quot;.  It didn&#39;t really fail to match anything.  Rather, it
matched something that happened to be zero characters long.  This is all
very above-board and honest.  When a function returns an undefined value,
it&#39;s an admission that it couldn&#39;t give you an honest answer.  So you
should use <code class="inline"><a class="l_k" href="../functions/defined.html">defined</a></code> only when you&#39;re questioning the integrity of what
you&#39;re trying to do.  At other times, a simple comparison to <code class="inline"><span class="n">0</span></code>
 or <code class="inline"><span class="q">&quot;&quot;</span></code>
 is
what you want.</p>
<p>See also <a href="../functions/undef.html">"undef"</a>, <a href="../functions/exists.html">"exists"</a>, <a href="../functions/ref.html">"ref"</a>.
-----

function delete
expr|
Given an expression that specifies a hash element, array element, hash slice,
or array slice, deletes the specified element(s) from the hash or array.
In the case of an array, if the array elements happen to be at the end,
the size of the array will shrink to the highest element that tests
true for exists() (or 0 if no such element exists).</p>
<p>Returns a list with the same number of elements as the number of elements
for which deletion was attempted.  Each element of that list consists of
either the value of the element deleted, or the undefined value.  In scalar
context, this means that you get the value of the last element deleted (or
the undefined value if that element did not exist).</p>
<pre class="verbatim"><ol><li>    <span class="i">%hash</span> = <span class="s">(</span><span class="w">foo</span> <span class="cm">=&gt;</span> <span class="n">11</span><span class="cm">,</span> <span class="w">bar</span> <span class="cm">=&gt;</span> <span class="n">22</span><span class="cm">,</span> <span class="w">baz</span> <span class="cm">=&gt;</span> <span class="n">33</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$scalar</span> = <a class="l_k" href="../functions/delete.html">delete</a> <span class="i">$hash</span>{<span class="w">foo</span>}<span class="sc">;</span>             <span class="c"># $scalar is 11</span></li><li>    <span class="i">$scalar</span> = <a class="l_k" href="../functions/delete.html">delete</a> <span class="i">@hash</span>{<span class="q">qw(foo bar)</span>}<span class="sc">;</span>     <span class="c"># $scalar is 22</span></li><li>    <span class="i">@array</span>  = <a class="l_k" href="../functions/delete.html">delete</a> <span class="i">@hash</span>{<span class="q">qw(foo bar baz)</span>}<span class="sc">;</span> <span class="c"># @array  is (undef,undef,33)</span></li></ol></pre><p>Deleting from <code class="inline"><span class="i">%ENV</span></code>
 modifies the environment.  Deleting from
a hash tied to a DBM file deletes the entry from the DBM file.  Deleting
from a <code class="inline"><a class="l_k" href="../functions/tie.html">tie</a></code>d hash or array may not necessarily return anything.</p>
<p>Deleting an array element effectively returns that position of the array
to its initial, uninitialized state.  Subsequently testing for the same
element with exists() will return false.  Also, deleting array elements
in the middle of an array will not shift the index of the elements
after them down.  Use splice() for that.  See <a href="../functions/exists.html">"exists"</a>.</p>
<p>The following (inefficiently) deletes all the values of %HASH and @ARRAY:</p>
<pre class="verbatim"><ol><li>    foreach <span class="i">$key</span> <span class="s">(</span><a class="l_k" href="../functions/keys.html">keys</a> <span class="i">%HASH</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/delete.html">delete</a> <span class="i">$HASH</span>{<span class="i">$key</span>}<span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    foreach <span class="i">$index</span> <span class="s">(</span><span class="n">0</span> .. <span class="i">$#ARRAY</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/delete.html">delete</a> <span class="i">$ARRAY</span>[<span class="i">$index</span>]<span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>And so do these:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/delete.html">delete</a> <span class="i">@HASH</span>{<a class="l_k" href="../functions/keys.html">keys</a> <span class="i">%HASH</span>}<span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/delete.html">delete</a> <span class="i">@ARRAY</span>[<span class="n">0</span> .. <span class="i">$#ARRAY</span>]<span class="sc">;</span></li></ol></pre><p>But both of these are slower than just assigning the empty list
or undefining %HASH or @ARRAY:</p>
<pre class="verbatim"><ol><li>    <span class="i">%HASH</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span>		<span class="c"># completely empty %HASH</span></li><li>    <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">%HASH</span><span class="sc">;</span>	<span class="c"># forget %HASH ever existed</span></li><li></li><li>    <span class="i">@ARRAY</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span>	<span class="c"># completely empty @ARRAY</span></li><li>    <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">@ARRAY</span><span class="sc">;</span>	<span class="c"># forget @ARRAY ever existed</span></li></ol></pre><p>Note that the EXPR can be arbitrarily complicated as long as the final
operation is a hash element, array element,  hash slice, or array slice
lookup:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/delete.html">delete</a> <span class="i">$ref</span>-&gt;[<span class="i">$x</span>][<span class="i">$y</span>]{<span class="i">$key</span>}<span class="sc">;</span></li><li>    <a class="l_k" href="../functions/delete.html">delete</a> <span class="i">@</span>{<span class="i">$ref</span>-&gt;[<span class="i">$x</span>][<span class="i">$y</span>]}{<span class="i">$key1</span><span class="cm">,</span> <span class="i">$key2</span><span class="cm">,</span> <span class="i">@morekeys</span>}<span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/delete.html">delete</a> <span class="i">$ref</span>-&gt;[<span class="i">$x</span>][<span class="i">$y</span>][<span class="i">$index</span>]<span class="sc">;</span></li><li>    <a class="l_k" href="../functions/delete.html">delete</a> <span class="i">@</span>{<span class="i">$ref</span>-&gt;[<span class="i">$x</span>][<span class="i">$y</span>]}[<span class="i">$index1</span><span class="cm">,</span> <span class="i">$index2</span><span class="cm">,</span> <span class="i">@moreindices</span>]<span class="sc">;</span></li></ol><
-----

function die
list|
Outside an <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code>, prints the value of LIST to <code class="inline"><span class="w">STDERR</span></code>
 and
exits with the current value of <code class="inline"><span class="i">$!</span></code>
 (errno).  If <code class="inline"><span class="i">$!</span></code>
 is <code class="inline"><span class="n">0</span></code>
,
exits with the value of <code class="inline"><span class="s">(</span><span class="i">$?</span>&gt;&gt; <span class="n">8</span><span class="s">)</span></code>
 (backtick `command`
status).  If <code class="inline"><span class="s">(</span><span class="i">$?</span>&gt;&gt; <span class="n">8</span><span class="s">)</span></code>
 is <code class="inline"><span class="n">0</span></code>
, exits with <code class="inline"><span class="n">255</span></code>
.  Inside
an <code class="inline"><a class="l_k" href="../functions/eval.html">eval(),</a></code> the error message is stuffed into <code class="inline"><span class="i">$@</span></code>
 and the
<code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code> is terminated with the undefined value.  This makes
<code class="inline"><a class="l_k" href="../functions/die.html">die</a></code> the way to raise an exception.</p>
<p>Equivalent examples:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t cd to spool: $!\n&quot;</span> unless <a class="l_k" href="../functions/chdir.html">chdir</a> <span class="q">&#39;/usr/spool/news&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/chdir.html">chdir</a> <span class="q">&#39;/usr/spool/news&#39;</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t cd to spool: $!\n&quot;</span></li></ol></pre><p>If the last element of LIST does not end in a newline, the current
script line number and input line number (if any) are also printed,
and a newline is supplied.  Note that the &quot;input line number&quot; (also
known as &quot;chunk&quot;) is subject to whatever notion of &quot;line&quot; happens to
be currently in effect, and is also available as the special variable
<code class="inline"><span class="i">$.</span></code>
.  See <a href="../perlvar.html#%24%2f">"$/" in perlvar</a> and <a href="../perlvar.html#%24.">"$." in perlvar</a>.</p>
<p>Hint: sometimes appending <code class="inline"><span class="q">&quot;, stopped&quot;</span></code>
 to your message will cause it
to make better sense when the string <code class="inline"><span class="q">&quot;at foo line 123&quot;</span></code>
 is appended.
Suppose you are running script &quot;canasta&quot;.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;/etc/games is no good&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;/etc/games is no good, stopped&quot;</span><span class="sc">;</span></li></ol></pre><p>produce, respectively</p>
<pre class="verbatim"><ol><li>    /etc/games is no good at canasta line 123.</li><li>    /etc/games is no good, stopped at canasta line 123.</li></ol></pre><p>See also exit(), warn(), and the Carp module.</p>
<p>If LIST is empty and <code class="inline"><span class="i">$@</span></code>
 already contains a value (typically from a
previous eval) that value is reused after appending <code class="inline"><span class="q">&quot;\t...propagated&quot;</span></code>
.
This is useful for propagating exceptions:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/eval.html">eval</a> <span class="s">{</span> ... <span class="s">}</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/die.html">die</a> unless <span class="i">$@</span> =~ <span class="q">/Expected exception/</span><span class="sc">;</span></li></ol></pre><p>If LIST is empty and <code class="inline"><span class="i">$@</span></code>
 contains an object reference that has a
<code class="inline"><span class="w">PROPAGATE</span></code>
 method, that method will be called with additional file
and line number parameters.  The return value replaces the value in
<code class="inline"><span class="i">$@</span></code>
.  i.e. as if <code class="inline"><span class="i">$@</span> = <a class="l_k" href="../functions/eval.html">eval</a> <span class="s">{</span> <span class="i">$@</span><span class="i">-&gt;PROPAGATE</span><span class="s">(</span><span class="w">__FILE__</span><span class="cm">,</span> <span class="w">__LINE__</span><span class="s">)</span> <span class="s">}</span><span class="sc">;</span></code>

were called.</p>
<p>If <code class="inline"><span class="i">$@</span></code>
 is empty then the string <code class="inline"><span class="q">&quot;Died&quot;</span></code>
 is used.</p>
<p>die() can also be called with a reference argument.  If this happens to be
trapped within an eval(), $@ contains the reference.  This behavior permits
a more elaborate exception handling implementation using objects that
maintain arbitrary state about the nature of the exception.  Such a scheme
is sometimes preferable to matching particular string values of $@ using
regular expressions.  Because $@ is a global variable, and eval() may be
used within object implementations, care must be taken that analyzing the
error object doesn&#39;t replace the reference in the global variable.  The
easiest solution is to make a local copy of the reference before doing
other manipulations.  Here&#39;s an example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Scalar::Util</span> <span class="q">&#39;blessed&#39;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/eval.html">eval</a> <span class="s">{</span> ... <span class="sc">;</span> <a class="l_k" href="../functions/die.html">die</a> <span class="w">Some::Module::Exception</span><span class="w">-&gt;new</span><span class="s">(</span> <span class="w">FOO</span> <span class="cm">=&gt;</span> <span class="q">&quot;bar&quot;</span> <span class="s">)</span> <span class="s">}</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$ev_err</span> = <span class="i">$@</span><span class="s">)</span> <span class="s">{</span></li><li>        if <span class="s">(</span><span class="i">blessed</span><span class="s">(</span><span class="i">$ev_err</span><span class="s">)</span> &amp;&amp; <span class="i">$ev_err</span><span class="i">-&gt;isa</span><span class="s">(</span><span class="q">&quot;Some::Module::Exception&quot;</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>            <span class="c"># handle Some::Module::Exception</span></li><li>        <span class="s">}</span></li><li>        else <span class="s">{</span></li><li>            <span class="c"># handle all other possible exceptions</span></li><li>        <span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre><p>Because perl will stringify uncaught exception messages before displaying
them, you may want to overload stringification operations on such custom
exception objects.  See <a href="../overload.html">overload</a> for details about that.</p>
<p>You can arrange for a callback to be run just before the <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>
does its deed, by setting the <code class="inline"><span class="i">$SIG</span>{<span class="w">__DIE__</span>}</code>
 hook.  The associated
handler will be called with the error text and can change the error
message, if it sees fit, by calling <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code> again.  See
<a href="../perlvar.html#%24SIG%7bexpr%7d">"$SIG{expr}" in perlvar</a> for details on setting <code class="inline"><span class="i">%SIG</span></code>
 entries, and
<a href="#eval-BLOCK">"eval BLOCK"</a> for some examples.  Although this feature was 
to be run only right before your program was to exit, this is not
currently the case--the <code class="inline"><span class="i">$SIG</span>{<span class="w">__DIE__</span>}</code>
 hook is currently called
even inside eval()ed blocks/strings!  If one wants the hook to do
nothing in such situations, put</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="../functions/die.html">die</a> <span class="i">@_</span> if <span class="i">$^S</span><span class="sc">;</span></li></ol></pre><p>as the first line of the handler (see <a href="../perlvar.html#%24%5eS">"$^S" in perlvar</a>).  Because
this promotes strange action at a distance, this counterintuitive
behavior may be fixed in a future release.
-----

function do
block|
Not really a function.  Returns the value of the last command in the
sequence of commands indicated by BLOCK.  When modified by the <code class="inline">while</code>
 or
<code class="inline">until</code>
 loop modifier, executes the BLOCK once before testing the loop
condition. (On other statements the loop modifiers test the conditional
first.)</p>
<p><code class="inline"><a class="l_k" href="../functions/do.html">do</a> <span class="w">BLOCK</span></code>
 does <i>not</i> count as a loop, so the loop control statements
<code class="inline"><a class="l_k" href="../functions/next.html">next</a></code>, <code class="inline"><a class="l_k" href="../functions/last.html">last</a></code>, or <code class="inline"><a class="l_k" href="../functions/redo.html">redo</a></code> cannot be used to leave or restart the block.
See <a href="../perlsyn.html">perlsyn</a> for alternative strategies.
-----

function dump
label|
This function causes an immediate core dump.  See also the <b>-u</b>
command-line switch in <a href="../perlrun.html">perlrun</a>, which does the same thing.
Primarily this is so that you can use the <b>undump</b> program (not
supplied) to turn your core dump into an executable binary after
having initialized all your variables at the beginning of the
program.  When the new binary is executed it will begin by executing
a <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a> <span class="j">LABEL</span></code>
 (with all the restrictions that <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code> suffers).
Think of it as a goto with an intervening core dump and reincarnation.
If <code class="inline"><span class="w">LABEL</span></code>
 is omitted, restarts the program from the top.</p>
<p><b>WARNING</b>: Any files opened at the time of the dump will <i>not</i>
be open any more when the program is reincarnated, with possible
resulting confusion on the part of Perl.</p>
<p>This function is now largely obsolete, mostly because it&#39;s very hard to
convert a core file into an executable. That&#39;s why you should now invoke
it as <code class="inline"><span class="i">CORE::dump</span><span class="s">(</span><span class="s">)</span></code>
, if you don&#39;t want to be warned against a possible
typo.
-----

function dump
This function causes an immediate core dump.  See also the <b>-u</b>
command-line switch in <a href="../perlrun.html">perlrun</a>, which does the same thing.
Primarily this is so that you can use the <b>undump</b> program (not
supplied) to turn your core dump into an executable binary after
having initialized all your variables at the beginning of the
program.  When the new binary is executed it will begin by executing
a <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a> <span class="j">LABEL</span></code>
 (with all the restrictions that <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code> suffers).
Think of it as a goto with an intervening core dump and reincarnation.
If <code class="inline"><span class="w">LABEL</span></code>
 is omitted, restarts the program from the top.</p>
<p><b>WARNING</b>: Any files opened at the time of the dump will <i>not</i>
be open any more when the program is reincarnated, with possible
resulting confusion on the part of Perl.</p>
<p>This function is now largely obsolete, mostly because it&#39;s very hard to
convert a core file into an executable. That&#39;s why you should now invoke
it as <code class="inline"><span class="i">CORE::dump</span><span class="s">(</span><span class="s">)</span></code>
, if you don&#39;t want to be warned against a possible
typo.
-----

function each
hash|
When called in list context, returns a 2-element list consisting of the
key and value for the next element of a hash, so that you can iterate over
it.  When called in scalar context, returns only the key for the next
element in the hash.</p>
<p>Entries are returned in an apparently random order.  The actual random
order is subject to change in future versions of perl, but it is
guaranteed to be in the same order as either the <code class="inline"><a class="l_k" href="../functions/keys.html">keys</a></code> or <code class="inline"><a class="l_k" href="../functions/values.html">values</a></code>
function would produce on the same (unmodified) hash.  Since Perl
5.8.2 the ordering can be different even between different runs of Perl
for security reasons (see <a href="../perlsec.html#Algorithmic-Complexity-Attacks">"Algorithmic Complexity Attacks" in perlsec</a>).</p>
<p>When the hash is entirely read, a null array is returned in list context
(which when assigned produces a false (<code class="inline"><span class="n">0</span></code>
) value), and <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> in
scalar context.  The next call to <code class="inline"><a class="l_k" href="../functions/each.html">each</a></code> after that will start iterating
again.  There is a single iterator for each hash, shared by all <code class="inline"><a class="l_k" href="../functions/each.html">each</a></code>,
<code class="inline"><a class="l_k" href="../functions/keys.html">keys</a></code>, and <code class="inline"><a class="l_k" href="../functions/values.html">values</a></code> function calls in the program; it can be reset by
reading all the elements from the hash, or by evaluating <code class="inline"><a class="l_k" href="../functions/keys.html">keys</a> <span class="w">HASH</span></code>
 or
<code class="inline"><a class="l_k" href="../functions/values.html">values</a> <span class="w">HASH</span></code>
.  If you add or delete elements of a hash while you&#39;re
iterating over it, you may get entries skipped or duplicated, so
don&#39;t.  Exception: It is always safe to delete the item most recently
returned by <code class="inline"><a class="l_k" href="../functions/each.html">each()</a></code>, which means that the following code will work:</p>
<pre class="verbatim"><ol><li>        while <span class="s">(</span><span class="s">(</span><span class="i">$key</span><span class="cm">,</span> <span class="i">$value</span><span class="s">)</span> = <a class="l_k" href="../functions/each.html">each</a> <span class="i">%hash</span><span class="s">)</span> <span class="s">{</span></li><li>          <a class="l_k" href="../functions/print.html">print</a> <span class="i">$key</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>          <a class="l_k" href="../functions/delete.html">delete</a> <span class="i">$hash</span>{<span class="i">$key</span>}<span class="sc">;</span>   <span class="c"># This is safe</span></li><li>        <span class="s">}</span></li></ol></pre><p>The following prints out your environment like the printenv(1) program,
only in a different order:</p>
<pre class="verbatim"><ol><li>    while <span class="s">(</span><span class="s">(</span><span class="i">$key</span><span class="cm">,</span><span class="i">$value</span><span class="s">)</span> = <a class="l_k" href="../functions/each.html">each</a> <span class="i">%ENV</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;$key=$value\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>See also <code class="inline"><a class="l_k" href="../functions/keys.html">keys</a></code>, <code class="inline"><a class="l_k" href="../functions/values.html">values</a></code> and <code class="inline"><a class="l_k" href="../functions/sort.html">sort</a></code>.
-----

function endgrent
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function endhostent
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function endnetent
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function endprotoent
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function endpwent
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function endservent
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function eof
filehandle|
Returns 1 if the next read on FILEHANDLE will return end of file, or if
FILEHANDLE is not open.  FILEHANDLE may be an expression whose value
gives the real filehandle.  (Note that this function actually
reads a character and then <code class="inline"><span class="w">ungetc</span></code>
s it, so isn&#39;t very useful in an
interactive context.)  Do not read from a terminal file (or call
<code class="inline"><a class="l_k" href="../functions/eof.html">eof(FILEHANDLE)</a></code> on it) after end-of-file is reached.  File types such
as terminals may lose the end-of-file condition if you do.</p>
<p>An <code class="inline"><a class="l_k" href="../functions/eof.html">eof</a></code> without an argument uses the last file read.  Using <code class="inline"><a class="l_k" href="../functions/eof.html">eof()</a></code>
with empty parentheses is very different.  It refers to the pseudo file
formed from the files listed on the command line and accessed via the
<code class="inline">&lt;&gt;</code>
 operator.  Since <code class="inline">&lt;&gt;</code>
 isn&#39;t explicitly opened,
as a normal filehandle is, an <code class="inline"><a class="l_k" href="../functions/eof.html">eof()</a></code> before <code class="inline">&lt;&gt;</code>
 has been
used will cause <code class="inline"><span class="i">@ARGV</span></code>
 to be examined to determine if input is
available.   Similarly, an <code class="inline"><a class="l_k" href="../functions/eof.html">eof()</a></code> after <code class="inline">&lt;&gt;</code>
 has returned
end-of-file will assume you are processing another <code class="inline"><span class="i">@ARGV</span></code>
 list,
and if you haven&#39;t set <code class="inline"><span class="i">@ARGV</span></code>
, will read input from <code class="inline"><span class="w">STDIN</span></code>
;
see <a href="../perlop.html#I%2fO-Operators">"I/O Operators" in perlop</a>.</p>
<p>In a <code class="inline">while <span class="s">(</span>&lt;&gt;<span class="s">)</span></code>
 loop, <code class="inline"><a class="l_k" href="../functions/eof.html">eof</a></code> or <code class="inline"><a class="l_k" href="../functions/eof.html">eof(ARGV)</a></code> can be used to
detect the end of each file, <code class="inline"><a class="l_k" href="../functions/eof.html">eof()</a></code> will only detect the end of the
last file.  Examples:</p>
<pre class="verbatim"><ol><li>    <span class="c"># reset line numbering on each input file</span></li><li>    while <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/next.html">next</a> if <span class="q">/^\s*#/</span><span class="sc">;</span>	<span class="c"># skip comments</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;$.\t$_&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span> <a class="l_k" href="../functions/continue.html">continue</a> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/close.html">close</a> <span class="w">ARGV</span>  if <a class="l_k" href="../functions/eof.html">eof</a><span class="sc">;</span>	<span class="c"># Not eof()!</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="c"># insert dashes just before last line of last file</span></li><li>    while <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>	if <span class="s">(</span><a class="l_k" href="../functions/eof.html">eof</a><span class="s">(</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span>		<span class="c"># check for end of last file</span></li><li>	    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;--------------\n&quot;</span><span class="sc">;</span></li><li>	<span class="s">}</span></li><li>	<a class="l_k" href="../functions/print.html">print</a><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/last.html">last</a> if <a class="l_k" href="../functions/eof.html">eof</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span>          <span class="c"># needed if we&#39;re reading from a terminal</span></li><li>    <span class="s">}</span></li></ol></pre><p>Practical hint: you almost never need to use <code class="inline"><a class="l_k" href="../functions/eof.html">eof</a></code> in Perl, because the
input operators typically return <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> when they run out of data, or if
there was an error.
-----

function eof
()|
Returns 1 if the next read on FILEHANDLE will return end of file, or if
FILEHANDLE is not open.  FILEHANDLE may be an expression whose value
gives the real filehandle.  (Note that this function actually
reads a character and then <code class="inline"><span class="w">ungetc</span></code>
s it, so isn&#39;t very useful in an
interactive context.)  Do not read from a terminal file (or call
<code class="inline"><a class="l_k" href="../functions/eof.html">eof(FILEHANDLE)</a></code> on it) after end-of-file is reached.  File types such
as terminals may lose the end-of-file condition if you do.</p>
<p>An <code class="inline"><a class="l_k" href="../functions/eof.html">eof</a></code> without an argument uses the last file read.  Using <code class="inline"><a class="l_k" href="../functions/eof.html">eof()</a></code>
with empty parentheses is very different.  It refers to the pseudo file
formed from the files listed on the command line and accessed via the
<code class="inline">&lt;&gt;</code>
 operator.  Since <code class="inline">&lt;&gt;</code>
 isn&#39;t explicitly opened,
as a normal filehandle is, an <code class="inline"><a class="l_k" href="../functions/eof.html">eof()</a></code> before <code class="inline">&lt;&gt;</code>
 has been
used will cause <code class="inline"><span class="i">@ARGV</span></code>
 to be examined to determine if input is
available.   Similarly, an <code class="inline"><a class="l_k" href="../functions/eof.html">eof()</a></code> after <code class="inline">&lt;&gt;</code>
 has returned
end-of-file will assume you are processing another <code class="inline"><span class="i">@ARGV</span></code>
 list,
and if you haven&#39;t set <code class="inline"><span class="i">@ARGV</span></code>
, will read input from <code class="inline"><span class="w">STDIN</span></code>
;
see <a href="../perlop.html#I%2fO-Operators">"I/O Operators" in perlop</a>.</p>
<p>In a <code class="inline">while <span class="s">(</span>&lt;&gt;<span class="s">)</span></code>
 loop, <code class="inline"><a class="l_k" href="../functions/eof.html">eof</a></code> or <code class="inline"><a class="l_k" href="../functions/eof.html">eof(ARGV)</a></code> can be used to
detect the end of each file, <code class="inline"><a class="l_k" href="../functions/eof.html">eof()</a></code> will only detect the end of the
last file.  Examples:</p>
<pre class="verbatim"><ol><li>    <span class="c"># reset line numbering on each input file</span></li><li>    while <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/next.html">next</a> if <span class="q">/^\s*#/</span><span class="sc">;</span>	<span class="c"># skip comments</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;$.\t$_&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span> <a class="l_k" href="../functions/continue.html">continue</a> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/close.html">close</a> <span class="w">ARGV</span>  if <a class="l_k" href="../functions/eof.html">eof</a><span class="sc">;</span>	<span class="c"># Not eof()!</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="c"># insert dashes just before last line of last file</span></li><li>    while <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>	if <span class="s">(</span><a class="l_k" href="../functions/eof.html">eof</a><span class="s">(</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span>		<span class="c"># check for end of last file</span></li><li>	    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;--------------\n&quot;</span><span class="sc">;</span></li><li>	<span class="s">}</span></li><li>	<a class="l_k" href="../functions/print.html">print</a><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/last.html">last</a> if <a class="l_k" href="../functions/eof.html">eof</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span>          <span class="c"># needed if we&#39;re reading from a terminal</span></li><li>    <span class="s">}</span></li></ol></pre><p>Practical hint: you almost never need to use <code class="inline"><a class="l_k" href="../functions/eof.html">eof</a></code> in Perl, because the
input operators typically return <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> when they run out of data, or if
there was an error.
-----

function eof
Returns 1 if the next read on FILEHANDLE will return end of file, or if
FILEHANDLE is not open.  FILEHANDLE may be an expression whose value
gives the real filehandle.  (Note that this function actually
reads a character and then <code class="inline"><span class="w">ungetc</span></code>
s it, so isn&#39;t very useful in an
interactive context.)  Do not read from a terminal file (or call
<code class="inline"><a class="l_k" href="../functions/eof.html">eof(FILEHANDLE)</a></code> on it) after end-of-file is reached.  File types such
as terminals may lose the end-of-file condition if you do.</p>
<p>An <code class="inline"><a class="l_k" href="../functions/eof.html">eof</a></code> without an argument uses the last file read.  Using <code class="inline"><a class="l_k" href="../functions/eof.html">eof()</a></code>
with empty parentheses is very different.  It refers to the pseudo file
formed from the files listed on the command line and accessed via the
<code class="inline">&lt;&gt;</code>
 operator.  Since <code class="inline">&lt;&gt;</code>
 isn&#39;t explicitly opened,
as a normal filehandle is, an <code class="inline"><a class="l_k" href="../functions/eof.html">eof()</a></code> before <code class="inline">&lt;&gt;</code>
 has been
used will cause <code class="inline"><span class="i">@ARGV</span></code>
 to be examined to determine if input is
available.   Similarly, an <code class="inline"><a class="l_k" href="../functions/eof.html">eof()</a></code> after <code class="inline">&lt;&gt;</code>
 has returned
end-of-file will assume you are processing another <code class="inline"><span class="i">@ARGV</span></code>
 list,
and if you haven&#39;t set <code class="inline"><span class="i">@ARGV</span></code>
, will read input from <code class="inline"><span class="w">STDIN</span></code>
;
see <a href="../perlop.html#I%2fO-Operators">"I/O Operators" in perlop</a>.</p>
<p>In a <code class="inline">while <span class="s">(</span>&lt;&gt;<span class="s">)</span></code>
 loop, <code class="inline"><a class="l_k" href="../functions/eof.html">eof</a></code> or <code class="inline"><a class="l_k" href="../functions/eof.html">eof(ARGV)</a></code> can be used to
detect the end of each file, <code class="inline"><a class="l_k" href="../functions/eof.html">eof()</a></code> will only detect the end of the
last file.  Examples:</p>
<pre class="verbatim"><ol><li>    <span class="c"># reset line numbering on each input file</span></li><li>    while <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/next.html">next</a> if <span class="q">/^\s*#/</span><span class="sc">;</span>	<span class="c"># skip comments</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;$.\t$_&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span> <a class="l_k" href="../functions/continue.html">continue</a> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/close.html">close</a> <span class="w">ARGV</span>  if <a class="l_k" href="../functions/eof.html">eof</a><span class="sc">;</span>	<span class="c"># Not eof()!</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="c"># insert dashes just before last line of last file</span></li><li>    while <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>	if <span class="s">(</span><a class="l_k" href="../functions/eof.html">eof</a><span class="s">(</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span>		<span class="c"># check for end of last file</span></li><li>	    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;--------------\n&quot;</span><span class="sc">;</span></li><li>	<span class="s">}</span></li><li>	<a class="l_k" href="../functions/print.html">print</a><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/last.html">last</a> if <a class="l_k" href="../functions/eof.html">eof</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span>          <span class="c"># needed if we&#39;re reading from a terminal</span></li><li>    <span class="s">}</span></li></ol></pre><p>Practical hint: you almost never need to use <code class="inline"><a class="l_k" href="../functions/eof.html">eof</a></code> in Perl, because the
input operators typically return <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> when they run out of data, or if
there was an error.
-----

function eval
expr|
     
 
-----

function exec
list|
The <code class="inline"><a class="l_k" href="../functions/exec.html">exec</a></code> function executes a system command <i>and never returns</i>--
use <code class="inline"><a class="l_k" href="../functions/system.html">system</a></code> instead of <code class="inline"><a class="l_k" href="../functions/exec.html">exec</a></code> if you want it to return.  It fails and
returns false only if the command does not exist <i>and</i> it is executed
directly instead of via your system&#39;s command shell (see below).</p>
<p>Since it&#39;s a common mistake to use <code class="inline"><a class="l_k" href="../functions/exec.html">exec</a></code> instead of <code class="inline"><a class="l_k" href="../functions/system.html">system</a></code>, Perl
warns you if there is a following statement which isn&#39;t <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>, <code class="inline"><a class="l_k" href="../functions/warn.html">warn</a></code>,
or <code class="inline"><a class="l_k" href="../functions/exit.html">exit</a></code> (if <code class="inline">-w</code>
 is set  -  but you always do that).   If you
<i>really</i> want to follow an <code class="inline"><a class="l_k" href="../functions/exec.html">exec</a></code> with some other statement, you
can use one of these styles to avoid the warning:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/exec.html">exec</a> <span class="s">(</span><span class="q">&#39;foo&#39;</span><span class="s">)</span>   or <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;couldn&#39;t exec foo: $!&quot;</span><span class="sc">;</span></li><li>    <span class="s">{</span> <a class="l_k" href="../functions/exec.html">exec</a> <span class="s">(</span><span class="q">&#39;foo&#39;</span><span class="s">)</span> <span class="s">}</span><span class="sc">;</span> <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;couldn&#39;t exec foo: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>If there is more than one argument in LIST, or if LIST is an array
with more than one value, calls execvp(3) with the arguments in LIST.
If there is only one scalar argument or an array with one element in it,
the argument is checked for shell metacharacters, and if there are any,
the entire argument is passed to the system&#39;s command shell for parsing
(this is <code class="inline">/bin/sh -c</code> on Unix platforms, but varies on other platforms).
If there are no shell metacharacters in the argument, it is split into
words and passed directly to <code class="inline"><span class="w">execvp</span></code>
, which is more efficient.
Examples:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/exec.html">exec</a> <span class="q">&#39;/bin/echo&#39;</span><span class="cm">,</span> <span class="q">&#39;Your arguments are: &#39;</span><span class="cm">,</span> <span class="i">@ARGV</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/exec.html">exec</a> <span class="q">&quot;sort $outfile | uniq&quot;</span><span class="sc">;</span></li></ol></pre><p>If you don&#39;t really want to execute the first argument, but want to lie
to the program you are executing about its own name, you can specify
the program you actually want to run as an &quot;indirect object&quot; (without a
comma) in front of the LIST.  (This always forces interpretation of the
LIST as a multivalued list, even if there is only a single scalar in
the list.)  Example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$shell</span> = <span class="q">&#39;/bin/csh&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/exec.html">exec</a> <span class="i">$shell</span> <span class="q">&#39;-sh&#39;</span><span class="sc">;</span>		<span class="c"># pretend it&#39;s a login shell</span></li></ol></pre><p>or, more directly,</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/exec.html">exec</a> <span class="s">{</span><span class="q">&#39;/bin/csh&#39;</span><span class="s">}</span> <span class="q">&#39;-sh&#39;</span><span class="sc">;</span>	<span class="c"># pretend it&#39;s a login shell</span></li></ol></pre><p>When the arguments get executed via the system shell, results will
be subject to its quirks and capabilities.  See <a href="../perlop.html#%60STRING%60">"`STRING`" in perlop</a>
for details.</p>
<p>Using an indirect object with <code class="inline"><a class="l_k" href="../functions/exec.html">exec</a></code> or <code class="inline"><a class="l_k" href="../functions/system.html">system</a></code> is also more
secure.  This usage (which also works fine with system()) forces
interpretation of the arguments as a multivalued list, even if the
list had just one argument.  That way you&#39;re safe from the shell
expanding wildcards or splitting up words with whitespace in them.</p>
<pre class="verbatim"><ol><li>    <span class="i">@args</span> = <span class="s">(</span> <span class="q">&quot;echo surprise&quot;</span> <span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/exec.html">exec</a> <span class="i">@args</span><span class="sc">;</span>               <span class="c"># subject to shell escapes</span></li><li>                                <span class="c"># if @args == 1</span></li><li>    <a class="l_k" href="../functions/exec.html">exec</a> <span class="s">{</span> <span class="i">$args</span>[<span class="n">0</span>] <span class="s">}</span> <span class="i">@args</span><span class="sc">;</span>  <span class="c"># safe even with one-arg list</span></li></ol></pre><p>The first version, the one without the indirect object, ran the <i>echo</i>
program, passing it <code class="inline"><span class="q">&quot;surprise&quot;</span></code>
 an argument.  The second version
didn&#39;t--it tried to run a program literally called <i>&quot;echo surprise&quot;</i>,
didn&#39;t find it, and set <code class="inline"><span class="i">$?</span></code>
 to a non-zero value indicating failure.</p>
<p>Beginning with v5.6.0, Perl will attempt to flush all files opened for
output before the exec, but this may not be supported on some platforms
(see <a href="../perlport.html">perlport</a>).  To be safe, you may need to set <code class="inline"><span class="i">$|</span></code>
 ($AUTOFLUSH
in English) or call the <code class="inline"><span class="i">autoflush</span><span class="s">(</span><span class="s">)</span></code>
 method of <code class="inline"><span class="w">IO::Handle</span></code>
 on any
open handles in order to avoid lost output.</p>
<p>Note that <code class="inline"><a class="l_k" href="../functions/exec.html">exec</a></code> will not call your <code class="inline">END</code>
 blocks, nor will it call
any <code class="inline">DESTROY</code>
 methods in your objects.
-----

function exec
program list|
The <code class="inline"><a class="l_k" href="../functions/exec.html">exec</a></code> function executes a system command <i>and never returns</i>--
use <code class="inline"><a class="l_k" href="../functions/system.html">system</a></code> instead of <code class="inline"><a class="l_k" href="../functions/exec.html">exec</a></code> if you want it to return.  It fails and
returns false only if the command does not exist <i>and</i> it is executed
directly instead of via your system&#39;s command shell (see below).</p>
<p>Since it&#39;s a common mistake to use <code class="inline"><a class="l_k" href="../functions/exec.html">exec</a></code> instead of <code class="inline"><a class="l_k" href="../functions/system.html">system</a></code>, Perl
warns you if there is a following statement which isn&#39;t <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>, <code class="inline"><a class="l_k" href="../functions/warn.html">warn</a></code>,
or <code class="inline"><a class="l_k" href="../functions/exit.html">exit</a></code> (if <code class="inline">-w</code>
 is set  -  but you always do that).   If you
<i>really</i> want to follow an <code class="inline"><a class="l_k" href="../functions/exec.html">exec</a></code> with some other statement, you
can use one of these styles to avoid the warning:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/exec.html">exec</a> <span class="s">(</span><span class="q">&#39;foo&#39;</span><span class="s">)</span>   or <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;couldn&#39;t exec foo: $!&quot;</span><span class="sc">;</span></li><li>    <span class="s">{</span> <a class="l_k" href="../functions/exec.html">exec</a> <span class="s">(</span><span class="q">&#39;foo&#39;</span><span class="s">)</span> <span class="s">}</span><span class="sc">;</span> <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;couldn&#39;t exec foo: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>If there is more than one argument in LIST, or if LIST is an array
with more than one value, calls execvp(3) with the arguments in LIST.
If there is only one scalar argument or an array with one element in it,
the argument is checked for shell metacharacters, and if there are any,
the entire argument is passed to the system&#39;s command shell for parsing
(this is <code class="inline">/bin/sh -c</code> on Unix platforms, but varies on other platforms).
If there are no shell metacharacters in the argument, it is split into
words and passed directly to <code class="inline"><span class="w">execvp</span></code>
, which is more efficient.
Examples:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/exec.html">exec</a> <span class="q">&#39;/bin/echo&#39;</span><span class="cm">,</span> <span class="q">&#39;Your arguments are: &#39;</span><span class="cm">,</span> <span class="i">@ARGV</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/exec.html">exec</a> <span class="q">&quot;sort $outfile | uniq&quot;</span><span class="sc">;</span></li></ol></pre><p>If you don&#39;t really want to execute the first argument, but want to lie
to the program you are executing about its own name, you can specify
the program you actually want to run as an &quot;indirect object&quot; (without a
comma) in front of the LIST.  (This always forces interpretation of the
LIST as a multivalued list, even if there is only a single scalar in
the list.)  Example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$shell</span> = <span class="q">&#39;/bin/csh&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/exec.html">exec</a> <span class="i">$shell</span> <span class="q">&#39;-sh&#39;</span><span class="sc">;</span>		<span class="c"># pretend it&#39;s a login shell</span></li></ol></pre><p>or, more directly,</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/exec.html">exec</a> <span class="s">{</span><span class="q">&#39;/bin/csh&#39;</span><span class="s">}</span> <span class="q">&#39;-sh&#39;</span><span class="sc">;</span>	<span class="c"># pretend it&#39;s a login shell</span></li></ol></pre><p>When the arguments get executed via the system shell, results will
be subject to its quirks and capabilities.  See <a href="../perlop.html#%60STRING%60">"`STRING`" in perlop</a>
for details.</p>
<p>Using an indirect object with <code class="inline"><a class="l_k" href="../functions/exec.html">exec</a></code> or <code class="inline"><a class="l_k" href="../functions/system.html">system</a></code> is also more
secure.  This usage (which also works fine with system()) forces
interpretation of the arguments as a multivalued list, even if the
list had just one argument.  That way you&#39;re safe from the shell
expanding wildcards or splitting up words with whitespace in them.</p>
<pre class="verbatim"><ol><li>    <span class="i">@args</span> = <span class="s">(</span> <span class="q">&quot;echo surprise&quot;</span> <span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/exec.html">exec</a> <span class="i">@args</span><span class="sc">;</span>               <span class="c"># subject to shell escapes</span></li><li>                                <span class="c"># if @args == 1</span></li><li>    <a class="l_k" href="../functions/exec.html">exec</a> <span class="s">{</span> <span class="i">$args</span>[<span class="n">0</span>] <span class="s">}</span> <span class="i">@args</span><span class="sc">;</span>  <span class="c"># safe even with one-arg list</span></li></ol></pre><p>The first version, the one without the indirect object, ran the <i>echo</i>
program, passing it <code class="inline"><span class="q">&quot;surprise&quot;</span></code>
 an argument.  The second version
didn&#39;t--it tried to run a program literally called <i>&quot;echo surprise&quot;</i>,
didn&#39;t find it, and set <code class="inline"><span class="i">$?</span></code>
 to a non-zero value indicating failure.</p>
<p>Beginning with v5.6.0, Perl will attempt to flush all files opened for
output before the exec, but this may not be supported on some platforms
(see <a href="../perlport.html">perlport</a>).  To be safe, you may need to set <code class="inline"><span class="i">$|</span></code>
 ($AUTOFLUSH
in English) or call the <code class="inline"><span class="i">autoflush</span><span class="s">(</span><span class="s">)</span></code>
 method of <code class="inline"><span class="w">IO::Handle</span></code>
 on any
open handles in order to avoid lost output.</p>
<p>Note that <code class="inline"><a class="l_k" href="../functions/exec.html">exec</a></code> will not call your <code class="inline">END</code>
 blocks, nor will it call
any <code class="inline">DESTROY</code>
 methods in your objects.
-----

function exists
expr|
Given an expression that specifies a hash element or array element,
returns true if the specified element in the hash or array has ever
been initialized, even if the corresponding value is undefined.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Exists\n&quot;</span> 	if <a class="l_k" href="../functions/exists.html">exists</a> <span class="i">$hash</span>{<span class="i">$key</span>}<span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Defined\n&quot;</span> 	if <a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$hash</span>{<span class="i">$key</span>}<span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;True\n&quot;</span>      if <span class="i">$hash</span>{<span class="i">$key</span>}<span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Exists\n&quot;</span> 	if <a class="l_k" href="../functions/exists.html">exists</a> <span class="i">$array</span>[<span class="i">$index</span>]<span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Defined\n&quot;</span> 	if <a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$array</span>[<span class="i">$index</span>]<span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;True\n&quot;</span>      if <span class="i">$array</span>[<span class="i">$index</span>]<span class="sc">;</span></li></ol></pre><p>A hash or array element can be true only if it&#39;s defined, and defined if
it exists, but the reverse doesn&#39;t necessarily hold true.</p>
<p>Given an expression that specifies the name of a subroutine,
returns true if the specified subroutine has ever been declared, even
if it is undefined.  Mentioning a subroutine name for exists or defined
does not count as declaring it.  Note that a subroutine which does not
exist may still be callable: its package may have an <code class="inline">AUTOLOAD</code>

method that makes it spring into existence the first time that it is
called -- see <a href="../perlsub.html">perlsub</a>.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Exists\n&quot;</span> 	if <a class="l_k" href="../functions/exists.html">exists</a> <span class="i">&amp;subroutine</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Defined\n&quot;</span> 	if <a class="l_k" href="../functions/defined.html">defined</a> <span class="i">&amp;subroutine</span><span class="sc">;</span></li></ol></pre><p>Note that the EXPR can be arbitrarily complicated as long as the final
operation is a hash or array key lookup or subroutine name:</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span><a class="l_k" href="../functions/exists.html">exists</a> <span class="i">$ref</span>-&gt;{<span class="w">A</span>}-&gt;{<span class="w">B</span>}-&gt;{<span class="i">$key</span>}<span class="s">)</span> 	<span class="s">{</span> <span class="s">}</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/exists.html">exists</a> <span class="i">$hash</span>{<span class="w">A</span>}{<span class="w">B</span>}{<span class="i">$key</span>}<span class="s">)</span> 	<span class="s">{</span> <span class="s">}</span></li><li></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/exists.html">exists</a> <span class="i">$ref</span>-&gt;{<span class="w">A</span>}-&gt;{<span class="w">B</span>}-&gt;[<span class="i">$ix</span>]<span class="s">)</span> 	<span class="s">{</span> <span class="s">}</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/exists.html">exists</a> <span class="i">$hash</span>{<span class="w">A</span>}{<span class="w">B</span>}[<span class="i">$ix</span>]<span class="s">)</span> 	<span class="s">{</span> <span class="s">}</span></li><li></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/exists.html">exists</a> <span class="i">&amp;</span>{<span class="i">$ref</span>-&gt;{<span class="w">A</span>}{<span class="w">B</span>}{<span class="i">$key</span>}}<span class="s">)</span>   <span class="s">{</span> <span class="s">}</span></li></ol></pre><p>Although the deepest nested array or hash will not spring into existence
just because its existence was tested, any intervening ones will.
Thus <code class="inline"><span class="i">$ref</span>-&gt;{<span class="q">&quot;A&quot;</span>}</code>
 and <code class="inline"><span class="i">$ref</span>-&gt;{<span class="q">&quot;A&quot;</span>}-&gt;{<span class="q">&quot;B&quot;</span>}</code>
 will spring
into existence due to the existence test for the $key element above.
This happens anywhere the arrow operator is used, including even:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">$ref</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/exists.html">exists</a> <span class="i">$ref</span>-&gt;{<span class="q">&quot;Some key&quot;</span>}<span class="s">)</span>	<span class="s">{</span> <span class="s">}</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">$ref</span><span class="sc">;</span> 	    <span class="c"># prints HASH(0x80d3d5c)</span></li></ol></pre><p>This surprising autovivification in what does not at first--or even
second--glance appear to be an lvalue context may be fixed in a future
release.</p>
<p>Use of a subroutine call, rather than a subroutine name, as an argument
to exists() is an error.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/exists.html">exists</a> <span class="i">&amp;sub</span><span class="sc">;</span>	<span class="c"># OK</span></li><li>    <a class="l_k" href="../functions/exists.html">exists</a> <span class="i">&amp;sub</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span>	<span class="c"># Error</span></li></ol><
-----

function exit
expr|
Evaluates EXPR and exits immediately with that value.    Example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$ans</span> = <span class="q">&lt;STDIN&gt;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/exit.html">exit</a> <span class="n">0</span> if <span class="i">$ans</span> =~ <span class="q">/^[Xx]/</span><span class="sc">;</span></li></ol></pre><p>See also <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>.  If EXPR is omitted, exits with <code class="inline"><span class="n">0</span></code>
 status.  The only
universally recognized values for EXPR are <code class="inline"><span class="n">0</span></code>
 for success and <code class="inline"><span class="n">1</span></code>

for error; other values are subject to interpretation depending on the
environment in which the Perl program is running.  For example, exiting
69 (EX_UNAVAILABLE) from a <i>sendmail</i> incoming-mail filter will cause
the mailer to return the item undelivered, but that&#39;s not true everywhere.</p>
<p>Don&#39;t use <code class="inline"><a class="l_k" href="../functions/exit.html">exit</a></code> to abort a subroutine if there&#39;s any chance that
someone might want to trap whatever error happened.  Use <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code> instead,
which can be trapped by an <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code>.</p>
<p>The exit() function does not always exit immediately.  It calls any
defined <code class="inline">END</code>
 routines first, but these <code class="inline">END</code>
 routines may not
themselves abort the exit.  Likewise any object destructors that need to
be called are called before the real exit.  If this is a problem, you
can call <code class="inline"><span class="j">POSIX:</span><span class="i">_exit</span><span class="s">(</span><span class="i">$status</span><span class="s">)</span></code>
 to avoid END and destructor processing.
See <a href="../perlmod.html">perlmod</a> for details.
-----

function exit
Evaluates EXPR and exits immediately with that value.    Example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$ans</span> = <span class="q">&lt;STDIN&gt;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/exit.html">exit</a> <span class="n">0</span> if <span class="i">$ans</span> =~ <span class="q">/^[Xx]/</span><span class="sc">;</span></li></ol></pre><p>See also <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>.  If EXPR is omitted, exits with <code class="inline"><span class="n">0</span></code>
 status.  The only
universally recognized values for EXPR are <code class="inline"><span class="n">0</span></code>
 for success and <code class="inline"><span class="n">1</span></code>

for error; other values are subject to interpretation depending on the
environment in which the Perl program is running.  For example, exiting
69 (EX_UNAVAILABLE) from a <i>sendmail</i> incoming-mail filter will cause
the mailer to return the item undelivered, but that&#39;s not true everywhere.</p>
<p>Don&#39;t use <code class="inline"><a class="l_k" href="../functions/exit.html">exit</a></code> to abort a subroutine if there&#39;s any chance that
someone might want to trap whatever error happened.  Use <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code> instead,
which can be trapped by an <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code>.</p>
<p>The exit() function does not always exit immediately.  It calls any
defined <code class="inline">END</code>
 routines first, but these <code class="inline">END</code>
 routines may not
themselves abort the exit.  Likewise any object destructors that need to
be called are called before the real exit.  If this is a problem, you
can call <code class="inline"><span class="j">POSIX:</span><span class="i">_exit</span><span class="s">(</span><span class="i">$status</span><span class="s">)</span></code>
 to avoid END and destructor processing.
See <a href="../perlmod.html">perlmod</a> for details.
-----

function exp
expr|
Returns <i>e</i> (the natural logarithm base) to the power of EXPR.
If EXPR is omitted, gives <code class="inline"><a class="l_k" href="../functions/exp.html">exp($_)</a></code>.
-----

function exp
Returns <i>e</i> (the natural logarithm base) to the power of EXPR.
If EXPR is omitted, gives <code class="inline"><a class="l_k" href="../functions/exp.html">exp($_)</a></code>.
-----

function fcntl
filehandle|
function|
scalar|
Implements the fcntl(2) function.  You&#39;ll probably have to say</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Fcntl</span><span class="sc">;</span></li></ol></pre><p>first to get the correct constant definitions.  Argument processing and
value return works just like <code class="inline"><a class="l_k" href="../functions/ioctl.html">ioctl</a></code> below.
For example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Fcntl</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/fcntl.html">fcntl</a><span class="s">(</span><span class="i">$filehandle</span><span class="cm">,</span> <span class="w">F_GETFL</span><span class="cm">,</span> <span class="i">$packed_return_buffer</span><span class="s">)</span></li><li>	or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;can&#39;t fcntl F_GETFL: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>You don&#39;t have to check for <code class="inline"><a class="l_k" href="../functions/defined.html">defined</a></code> on the return from <code class="inline"><a class="l_k" href="../functions/fcntl.html">fcntl</a></code>.
Like <code class="inline"><a class="l_k" href="../functions/ioctl.html">ioctl</a></code>, it maps a <code class="inline"><span class="n">0</span></code>
 return from the system call into
<code class="inline"><span class="q">&quot;0 but true&quot;</span></code>
 in Perl.  This string is true in boolean context and <code class="inline"><span class="n">0</span></code>

in numeric context.  It is also exempt from the normal <b>-w</b> warnings
on improper numeric conversions.</p>
<p>Note that <code class="inline"><a class="l_k" href="../functions/fcntl.html">fcntl</a></code> will produce a fatal error if used on a machine that
doesn&#39;t implement fcntl(2).  See the Fcntl module or your fcntl(2)
manpage to learn what functions are available on your system.</p>
<p>Here&#39;s an example of setting a filehandle named <code class="inline"><span class="w">REMOTE</span></code>
 to be
non-blocking at the system level.  You&#39;ll have to negotiate <code class="inline"><span class="i">$|</span></code>

on your own, though.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Fcntl</span> <span class="q">qw(F_GETFL F_SETFL O_NONBLOCK)</span><span class="sc">;</span></li><li></li><li>    <span class="i">$flags</span> = <a class="l_k" href="../functions/fcntl.html">fcntl</a><span class="s">(</span><span class="w">REMOTE</span><span class="cm">,</span> <span class="w">F_GETFL</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span></li><li>                or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t get flags for the socket: $!\n&quot;</span><span class="sc">;</span></li><li></li><li>    <span class="i">$flags</span> = <a class="l_k" href="../functions/fcntl.html">fcntl</a><span class="s">(</span><span class="w">REMOTE</span><span class="cm">,</span> <span class="w">F_SETFL</span><span class="cm">,</span> <span class="i">$flags</span> | <span class="w">O_NONBLOCK</span><span class="s">)</span></li><li>                or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t set flags for the socket: $!\n&quot;</span><span class="sc">;</span></li></ol><
-----

function fileno
filehandle|
Returns the file descriptor for a filehandle, or undefined if the
filehandle is not open.  This is mainly useful for constructing
bitmaps for <code class="inline"><a class="l_k" href="../functions/select.html">select</a></code> and low-level POSIX tty-handling operations.
If FILEHANDLE is an expression, the value is taken as an indirect
filehandle, generally its name.</p>
<p>You can use this to find out whether two handles refer to the
same underlying descriptor:</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span><a class="l_k" href="../functions/fileno.html">fileno</a><span class="s">(</span><span class="w">THIS</span><span class="s">)</span> == <a class="l_k" href="../functions/fileno.html">fileno</a><span class="s">(</span><span class="w">THAT</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;THIS and THAT are dups\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>(Filehandles connected to memory objects via new features of <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> may
return undefined even though they are open.)
-----

function flock
filehandle|
operation|
Calls flock(2), or an emulation of it, on FILEHANDLE.  Returns true
for success, false on failure.  Produces a fatal error if used on a
machine that doesn&#39;t implement flock(2), fcntl(2) locking, or lockf(3).
<code class="inline"><a class="l_k" href="../functions/flock.html">flock</a></code> is Perl&#39;s portable file locking interface, although it locks
only entire files, not records.</p>
<p>Two potentially non-obvious but traditional <code class="inline"><a class="l_k" href="../functions/flock.html">flock</a></code> semantics are
that it waits indefinitely until the lock is granted, and that its locks
<b>merely advisory</b>.  Such discretionary locks are more flexible, but offer
fewer guarantees.  This means that programs that do not also use <code class="inline"><a class="l_k" href="../functions/flock.html">flock</a></code>
may modify files locked with <code class="inline"><a class="l_k" href="../functions/flock.html">flock</a></code>.  See <a href="../perlport.html">perlport</a>, 
your port&#39;s specific documentation, or your system-specific local manpages
for details.  It&#39;s best to assume traditional behavior if you&#39;re writing
portable programs.  (But if you&#39;re not, you should as always feel perfectly
free to write for your own system&#39;s idiosyncrasies (sometimes called
&quot;features&quot;).  Slavish adherence to portability concerns shouldn&#39;t get
in the way of your getting your job done.)</p>
<p>OPERATION is one of LOCK_SH, LOCK_EX, or LOCK_UN, possibly combined with
LOCK_NB.  These constants are traditionally valued 1, 2, 8 and 4, but
you can use the symbolic names if you import them from the Fcntl module,
either individually, or as a group using the &#39;:flock&#39; tag.  LOCK_SH
requests a shared lock, LOCK_EX requests an exclusive lock, and LOCK_UN
releases a previously requested lock.  If LOCK_NB is bitwise-or&#39;ed with
LOCK_SH or LOCK_EX then <code class="inline"><a class="l_k" href="../functions/flock.html">flock</a></code> will return immediately rather than blocking
waiting for the lock (check the return status to see if you got it).</p>
<p>To avoid the possibility of miscoordination, Perl now flushes FILEHANDLE
before locking or unlocking it.</p>
<p>Note that the emulation built with lockf(3) doesn&#39;t provide shared
locks, and it requires that FILEHANDLE be open with write intent.  These
are the semantics that lockf(3) implements.  Most if not all systems
implement lockf(3) in terms of fcntl(2) locking, though, so the
differing semantics shouldn&#39;t bite too many people.</p>
<p>Note that the fcntl(2) emulation of flock(3) requires that FILEHANDLE
be open with read intent to use LOCK_SH and requires that it be open
with write intent to use LOCK_EX.</p>
<p>Note also that some versions of <code class="inline"><a class="l_k" href="../functions/flock.html">flock</a></code> cannot lock things over the
network; you would need to use the more system-specific <code class="inline"><a class="l_k" href="../functions/fcntl.html">fcntl</a></code> for
that.  If you like you can force Perl to ignore your system&#39;s flock(2)
function, and so provide its own fcntl(2)-based emulation, by passing
the switch <code class="inline">-<span class="w">Ud_flock</span></code>
 to the <i>Configure</i> program when you configure
perl.</p>
<p>Here&#39;s a mailbox appender for BSD systems.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Fcntl</span> <span class="q">qw(:flock SEEK_END)</span><span class="sc">;</span> <span class="c"># import LOCK_* and SEEK_END constants</span></li><li></li><li><a name="lock"></a>    sub <span class="m">lock</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span><span class="i">$fh</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/flock.html">flock</a><span class="s">(</span><span class="i">$fh</span><span class="cm">,</span> <span class="w">LOCK_EX</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Cannot lock mailbox - $!\n&quot;</span><span class="sc">;</span></li><li></li><li>	<span class="c"># and, in case someone appended while we were waiting...</span></li><li>	<a class="l_k" href="../functions/seek.html">seek</a><span class="s">(</span><span class="i">$fh</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="w">SEEK_END</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Cannot seek - $!\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li><a name="unlock"></a>    sub <span class="m">unlock</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span><span class="i">$fh</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/flock.html">flock</a><span class="s">(</span><span class="i">$fh</span><span class="cm">,</span> <span class="w">LOCK_UN</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Cannot unlock mailbox - $!\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$mbox</span><span class="cm">,</span> <span class="q">&quot;&gt;&gt;&quot;</span><span class="cm">,</span> <span class="q">&quot;/usr/spool/mail/$ENV{'USER'}&quot;</span><span class="s">)</span></li><li>	    or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can't open mailbox: $!&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/lock.html">lock</a><span class="s">(</span><span class="i">$mbox</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">$mbox</span> <span class="i">$msg</span><span class="cm">,</span><span class="q">&quot;\n\n&quot;</span><span class="sc">;</span></li><li>    <span class="i">unlock</span><span class="s">(</span><span class="i">$mbox</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>On systems that support a real flock(), locks are inherited across fork()
calls, whereas those that must resort to the more capricious fcntl()
function lose the locks, making it harder to write servers.</p>
<p>See also <a href="../DB_File.html">DB_File</a> for other flock() examples.
-----

function fork
Does a fork(2) system call to create a new process running the
same program at the same point.  It returns the child pid to the
parent process, <code class="inline"><span class="n">0</span></code>
 to the child process, or <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> if the fork is
unsuccessful.  File descriptors (and sometimes locks on those descriptors)
are shared, while everything else is copied.  On most systems supporting
fork(), great care has gone into making it extremely efficient (for
example, using copy-on-write technology on data pages), making it the
dominant paradigm for multitasking over the last few decades.</p>
<p>Beginning with v5.6.0, Perl will attempt to flush all files opened for
output before forking the child process, but this may not be supported
on some platforms (see <a href="../perlport.html">perlport</a>).  To be safe, you may need to set
<code class="inline"><span class="i">$|</span></code>
 ($AUTOFLUSH in English) or call the <code class="inline"><span class="i">autoflush</span><span class="s">(</span><span class="s">)</span></code>
 method of
<code class="inline"><span class="w">IO::Handle</span></code>
 on any open handles in order to avoid duplicate output.</p>
<p>If you <code class="inline"><a class="l_k" href="../functions/fork.html">fork</a></code> without ever waiting on your children, you will
accumulate zombies.  On some systems, you can avoid this by setting
<code class="inline"><span class="i">$SIG</span>{<span class="w">CHLD</span>}</code>
 to <code class="inline"><span class="q">&quot;IGNORE&quot;</span></code>
.  See also <a href="../perlipc.html">perlipc</a> for more examples of
forking and reaping moribund children.</p>
<p>Note that if your forked child inherits system file descriptors like
STDIN and STDOUT that are actually connected by a pipe or socket, even
if you exit, then the remote server (such as, say, a CGI script or a
backgrounded job launched from a remote shell) won&#39;t think you&#39;re done.
You should reopen those to <i>/dev/null</i> if it&#39;s any issue.
-----

function format
Declare a picture format for use by the <code class="inline"><a class="l_k" href="../functions/write.html">write</a></code> function.  For
example:</p>
<pre class="verbatim"><ol><li>    format Something =</li><li>	Test: @&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; @||||| @&gt;&gt;&gt;&gt;&gt;</li><li>	      $str,     $%,    '$' . int($num)</li><li>    .</li><li></li><li>    $str = "widget";</li><li>    $num = $cost/$quantity;</li><li>    $~ = 'Something';</li><li>    write;</li></ol></pre><p>See <a href="../perlform.html">perlform</a> for many details and examples.
-----

function formline
picture|
list|
This is an internal function used by <code class="inline"><a class="l_k" href="../functions/format.html">format</a></code>s, though you may call it,
too.  It formats (see <a href="../perlform.html">perlform</a>) a list of values according to the
contents of PICTURE, placing the output into the format output
accumulator, <code class="inline"><span class="i">$^A</span></code>
 (or <code class="inline"><span class="i">$ACCUMULATOR</span></code>
 in English).
Eventually, when a <code class="inline"><a class="l_k" href="../functions/write.html">write</a></code> is done, the contents of
<code class="inline"><span class="i">$^A</span></code>
 are written to some filehandle.  You could also read <code class="inline"><span class="i">$^A</span></code>

and then set <code class="inline"><span class="i">$^A</span></code>
 back to <code class="inline"><span class="q">&quot;&quot;</span></code>
.  Note that a format typically
does one <code class="inline"><a class="l_k" href="../functions/formline.html">formline</a></code> per line of form, but the <code class="inline"><a class="l_k" href="../functions/formline.html">formline</a></code> function itself
doesn&#39;t care how many newlines are embedded in the PICTURE.  This means
that the <code class="inline">~</code>
 and <code class="inline">~~</code>
 tokens will treat the entire PICTURE as a single line.
You may therefore need to use multiple formlines to implement a single
record format, just like the format compiler.</p>
<p>Be careful if you put double quotes around the picture, because an <code class="inline"><span class="i">@</span></code>

character may be taken to mean the beginning of an array name.
<code class="inline"><a class="l_k" href="../functions/formline.html">formline</a></code> always returns true.  See <a href="../perlform.html">perlform</a> for other examples.
-----

function getc
filehandle|
Returns the next character from the input file attached to FILEHANDLE,
or the undefined value at end of file, or if there was an error (in
the latter case <code class="inline"><span class="i">$!</span></code>
 is set).  If FILEHANDLE is omitted, reads from
STDIN.  This is not particularly efficient.  However, it cannot be
used by itself to fetch single characters without waiting for the user
to hit enter.  For that, try something more like:</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span><span class="i">$BSD_STYLE</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/system.html">system</a> <span class="q">&quot;stty cbreak &lt;/dev/tty &gt;/dev/tty 2&gt;&amp;1&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    else <span class="s">{</span></li><li>	<a class="l_k" href="../functions/system.html">system</a> <span class="q">&quot;stty&quot;</span><span class="cm">,</span> <span class="q">&#39;-icanon&#39;</span><span class="cm">,</span> <span class="q">&#39;eol&#39;</span><span class="cm">,</span> <span class="q">&quot;\001&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="i">$key</span> = <a class="l_k" href="../functions/getc.html">getc</a><span class="s">(</span><span class="w">STDIN</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    if <span class="s">(</span><span class="i">$BSD_STYLE</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/system.html">system</a> <span class="q">&quot;stty -cbreak &lt;/dev/tty &gt;/dev/tty 2&gt;&amp;1&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    else <span class="s">{</span></li><li>	<a class="l_k" href="../functions/system.html">system</a> <span class="q">&quot;stty&quot;</span><span class="cm">,</span> <span class="q">&#39;icanon&#39;</span><span class="cm">,</span> <span class="q">&#39;eol&#39;</span><span class="cm">,</span> <span class="q">&#39;^@&#39;</span><span class="sc">;</span> <span class="c"># ASCII null</span></li><li>    <span class="s">}</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>Determination of whether $BSD_STYLE should be set
is left as an exercise to the reader.</p>
<p>The <code class="inline"><span class="w">POSIX::getattr</span></code>
 function can do this more portably on
systems purporting POSIX compliance.  See also the <code class="inline"><span class="w">Term::ReadKey</span></code>

module from your nearest CPAN site; details on CPAN can be found on
<a href="../perlmodlib.html#CPAN">"CPAN" in perlmodlib</a>.
-----

function getc
Returns the next character from the input file attached to FILEHANDLE,
or the undefined value at end of file, or if there was an error (in
the latter case <code class="inline"><span class="i">$!</span></code>
 is set).  If FILEHANDLE is omitted, reads from
STDIN.  This is not particularly efficient.  However, it cannot be
used by itself to fetch single characters without waiting for the user
to hit enter.  For that, try something more like:</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span><span class="i">$BSD_STYLE</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/system.html">system</a> <span class="q">&quot;stty cbreak &lt;/dev/tty &gt;/dev/tty 2&gt;&amp;1&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    else <span class="s">{</span></li><li>	<a class="l_k" href="../functions/system.html">system</a> <span class="q">&quot;stty&quot;</span><span class="cm">,</span> <span class="q">&#39;-icanon&#39;</span><span class="cm">,</span> <span class="q">&#39;eol&#39;</span><span class="cm">,</span> <span class="q">&quot;\001&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="i">$key</span> = <a class="l_k" href="../functions/getc.html">getc</a><span class="s">(</span><span class="w">STDIN</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    if <span class="s">(</span><span class="i">$BSD_STYLE</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/system.html">system</a> <span class="q">&quot;stty -cbreak &lt;/dev/tty &gt;/dev/tty 2&gt;&amp;1&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    else <span class="s">{</span></li><li>	<a class="l_k" href="../functions/system.html">system</a> <span class="q">&quot;stty&quot;</span><span class="cm">,</span> <span class="q">&#39;icanon&#39;</span><span class="cm">,</span> <span class="q">&#39;eol&#39;</span><span class="cm">,</span> <span class="q">&#39;^@&#39;</span><span class="sc">;</span> <span class="c"># ASCII null</span></li><li>    <span class="s">}</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>Determination of whether $BSD_STYLE should be set
is left as an exercise to the reader.</p>
<p>The <code class="inline"><span class="w">POSIX::getattr</span></code>
 function can do this more portably on
systems purporting POSIX compliance.  See also the <code class="inline"><span class="w">Term::ReadKey</span></code>

module from your nearest CPAN site; details on CPAN can be found on
<a href="../perlmodlib.html#CPAN">"CPAN" in perlmodlib</a>.
-----

function getgrent
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function getgrgid
gid|
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function getgrnam
name|
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function gethostbyaddr
addr|
addrtype|
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function gethostbyname
name|
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function gethostent
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function getlogin
This implements the C library function of the same name, which on most
systems returns the current login from <i>/etc/utmp</i>, if any.  If null,
use <code class="inline"><a class="l_k" href="../functions/getpwuid.html">getpwuid</a></code>.</p>
<pre class="verbatim"><ol><li>    <span class="i">$login</span> = <a class="l_k" href="../functions/getlogin.html">getlogin</a> || <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$&lt;</span><span class="s">)</span> || <span class="q">&quot;Kilroy&quot;</span><span class="sc">;</span></li></ol></pre><p>Do not consider <code class="inline"><a class="l_k" href="../functions/getlogin.html">getlogin</a></code> for authentication: it is not as
secure as <code class="inline"><a class="l_k" href="../functions/getpwuid.html">getpwuid</a></code>.
-----

function getnetbyaddr
addr|
addrtype|
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function getnetbyname
name|
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function getnetent
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function getpeername
socket|
Returns the packed sockaddr address of other end of the SOCKET connection.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$hersockaddr</span>    = <a class="l_k" href="../functions/getpeername.html">getpeername</a><span class="s">(</span><span class="w">SOCK</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">(</span><span class="i">$port</span><span class="cm">,</span> <span class="i">$iaddr</span><span class="s">)</span> = <span class="i">sockaddr_in</span><span class="s">(</span><span class="i">$hersockaddr</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$herhostname</span>    = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$herstraddr</span>     = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol><
-----

function getpgrp
pid|
Returns the current process group for the specified PID.  Use
a PID of <code class="inline"><span class="n">0</span></code>
 to get the current process group for the
current process.  Will raise an exception if used on a machine that
doesn&#39;t implement getpgrp(2).  If PID is omitted, returns process
group of current process.  Note that the POSIX version of <code class="inline"><a class="l_k" href="../functions/getpgrp.html">getpgrp</a></code>
does not accept a PID argument, so only <code class="inline"><span class="w">PID</span>==<span class="n">0</span></code>
 is truly portable.
-----

function getppid
Returns the process id of the parent process.</p>
<p>Note for Linux users: on Linux, the C functions <code class="inline"><span class="i">getpid</span><span class="s">(</span><span class="s">)</span></code>
 and
<code class="inline"><a class="l_k" href="../functions/getppid.html">getppid()</a></code> return different values from different threads. In order to
be portable, this behavior is not reflected by the perl-level function
<code class="inline"><a class="l_k" href="../functions/getppid.html">getppid()</a></code>, that returns a consistent value across threads. If you want
to call the underlying <code class="inline"><a class="l_k" href="../functions/getppid.html">getppid()</a></code>, you may use the CPAN module
<code class="inline"><span class="w">Linux::Pid</span></code>
.
-----

function getpriority
which|
who|
Returns the current priority for a process, a process group, or a user.
(See <code class="inline"><a class="l_k" href="../functions/getpriority.html">getpriority(2)</a></code>.)  Will raise a fatal exception if used on a
machine that doesn&#39;t implement getpriority(2).
-----

function getprotobyname
name|
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function getprotobynumber
number|
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function getprotoent
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function getpwent
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function getpwnam
name|
    
    
    
     
     
  
-----

function getpwuid
uid|
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function getservbyname
name|
proto|
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function getservbyport
port|
proto|
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function getservent
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function getsockname
socket|
Returns the packed sockaddr address of this end of the SOCKET connection,
in case you don&#39;t know the address because you have several different
IPs that the connection might have come in on.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$mysockaddr</span> = <a class="l_k" href="../functions/getsockname.html">getsockname</a><span class="s">(</span><span class="w">SOCK</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">(</span><span class="i">$port</span><span class="cm">,</span> <span class="i">$myaddr</span><span class="s">)</span> = <span class="i">sockaddr_in</span><span class="s">(</span><span class="i">$mysockaddr</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;Connect to %s [%s]\n&quot;</span><span class="cm">,</span></li><li>       <a class="l_k" href="../functions/scalar.html">scalar</a> <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$myaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="cm">,</span></li><li>       <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$myaddr</span><span class="s">)</span><span class="sc">;</span></li></ol><
-----

function getsockopt
socket|
level|
optname|
Queries the option named OPTNAME associated with SOCKET at a given LEVEL.
Options may exist at multiple protocol levels depending on the socket
type, but at least the uppermost socket level SOL_SOCKET (defined in the
<code class="inline"><span class="w">Socket</span></code>
 module) will exist. To query options at another level the
protocol number of the appropriate protocol controlling the option
should be supplied. For example, to indicate that an option is to be
interpreted by the TCP protocol, LEVEL should be set to the protocol
number of TCP, which you can get using getprotobyname.</p>
<p>The call returns a packed string representing the requested socket option,
or <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> if there is an error (the error reason will be in $!). What
exactly is in the packed string depends in the LEVEL and OPTNAME, consult
your system documentation for details. A very common case however is that
the option is an integer, in which case the result will be a packed
integer which you can decode using unpack with the <code class="inline"><span class="w">i</span></code>
 (or <code class="inline"><span class="w">I</span></code>
) format.</p>
<p>An example testing if Nagle&#39;s algorithm is turned on on a socket:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span> <span class="q">qw(:all)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/defined.html">defined</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$tcp</span> = <a class="l_k" href="../functions/getprotobyname.html">getprotobyname</a><span class="s">(</span><span class="q">&quot;tcp&quot;</span><span class="s">)</span><span class="s">)</span></li><li>	or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Could not determine the protocol number for tcp&quot;</span><span class="sc">;</span></li><li>    <span class="c"># my $tcp = IPPROTO_TCP; # Alternative</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$packed</span> = <a class="l_k" href="../functions/getsockopt.html">getsockopt</a><span class="s">(</span><span class="i">$socket</span><span class="cm">,</span> <span class="i">$tcp</span><span class="cm">,</span> <span class="w">TCP_NODELAY</span><span class="s">)</span></li><li>	or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Could not query TCP_NODELAY socket option: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$nodelay</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;I&quot;</span><span class="cm">,</span> <span class="i">$packed</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Nagle&#39;s algorithm is turned &quot;</span><span class="cm">,</span> <span class="i">$nodelay</span> ? <span class="q">&quot;off\n&quot;</span> <span class="co">:</span> <span class="q">&quot;on\n&quot;</span><span class="sc">;</span></li></ol><
-----

function glob
expr|
In list context, returns a (possibly empty) list of filename expansions on
the value of EXPR such as the standard Unix shell <i>/bin/csh</i> would do. In
scalar context, glob iterates through such filename expansions, returning
undef when the list is exhausted. This is the internal function
implementing the <code class="inline"><span class="q">&lt;*.c&gt;</span></code>
 operator, but you can use it directly. If
EXPR is omitted, <code class="inline"><span class="i">$_</span></code>
 is used.  The <code class="inline"><span class="q">&lt;*.c&gt;</span></code>
 operator is discussed in
more detail in <a href="../perlop.html#I%2fO-Operators">"I/O Operators" in perlop</a>.</p>
<p>Note that <code class="inline"><a class="l_k" href="../functions/glob.html">glob</a></code> will split its arguments on whitespace, treating
each segment as separate pattern.  As such, <code class="inline"><a class="l_k" href="../functions/glob.html">glob</a><span class="s">(</span><span class="q">'*.c *.h'</span><span class="s">)</span></code>
 would
match all files with a <i>.c</i> or <i>.h</i> extension.  The expression
<code class="inline"><a class="l_k" href="../functions/glob.html">glob</a><span class="s">(</span><span class="q">'.* *'</span><span class="s">)</span></code>
 would match all files in the current working directory.</p>
<p>Beginning with v5.6.0, this operator is implemented using the standard
<code class="inline"><span class="w">File::Glob</span></code>
 extension.  See <a href="../File/Glob.html">File::Glob</a> for details, including
<code class="inline"><span class="w">bsd_glob</span></code>
 which does not treat whitespace as a pattern separator.
-----

function glob
In list context, returns a (possibly empty) list of filename expansions on
the value of EXPR such as the standard Unix shell <i>/bin/csh</i> would do. In
scalar context, glob iterates through such filename expansions, returning
undef when the list is exhausted. This is the internal function
implementing the <code class="inline"><span class="q">&lt;*.c&gt;</span></code>
 operator, but you can use it directly. If
EXPR is omitted, <code class="inline"><span class="i">$_</span></code>
 is used.  The <code class="inline"><span class="q">&lt;*.c&gt;</span></code>
 operator is discussed in
more detail in <a href="../perlop.html#I%2fO-Operators">"I/O Operators" in perlop</a>.</p>
<p>Note that <code class="inline"><a class="l_k" href="../functions/glob.html">glob</a></code> will split its arguments on whitespace, treating
each segment as separate pattern.  As such, <code class="inline"><a class="l_k" href="../functions/glob.html">glob</a><span class="s">(</span><span class="q">'*.c *.h'</span><span class="s">)</span></code>
 would
match all files with a <i>.c</i> or <i>.h</i> extension.  The expression
<code class="inline"><a class="l_k" href="../functions/glob.html">glob</a><span class="s">(</span><span class="q">'.* *'</span><span class="s">)</span></code>
 would match all files in the current working directory.</p>
<p>Beginning with v5.6.0, this operator is implemented using the standard
<code class="inline"><span class="w">File::Glob</span></code>
 extension.  See <a href="../File/Glob.html">File::Glob</a> for details, including
<code class="inline"><span class="w">bsd_glob</span></code>
 which does not treat whitespace as a pattern separator.
-----

function gmtime
expr|
Works just like <a href="../functions/localtime.html">localtime</a> but the returned values are
localized for the standard Greenwich time zone.</p>
<p>Note: when called in list context, $isdst, the last value
returned by gmtime is always <code class="inline"><span class="n">0</span></code>
.  There is no
Daylight Saving Time in GMT.</p>
<p>See <a href="../perlport.html#gmtime">"gmtime" in perlport</a> for portability concerns.
-----

function gmtime
Works just like <a href="../functions/localtime.html">localtime</a> but the returned values are
localized for the standard Greenwich time zone.</p>
<p>Note: when called in list context, $isdst, the last value
returned by gmtime is always <code class="inline"><span class="n">0</span></code>
.  There is no
Daylight Saving Time in GMT.</p>
<p>See <a href="../perlport.html#gmtime">"gmtime" in perlport</a> for portability concerns.
-----

function goto
label|
The <code class="inline"><a class="l_k" href="../functions/goto.html">goto-LABEL</a></code> form finds the statement labeled with LABEL and resumes
execution there.  It may not be used to go into any construct that
requires initialization, such as a subroutine or a <code class="inline">foreach</code>
 loop.  It
also can&#39;t be used to go into a construct that is optimized away,
or to get out of a block or subroutine given to <code class="inline"><a class="l_k" href="../functions/sort.html">sort</a></code>.
It can be used to go almost anywhere else within the dynamic scope,
including out of subroutines, but it&#39;s usually better to use some other
construct such as <code class="inline"><a class="l_k" href="../functions/last.html">last</a></code> or <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>.  The author of Perl has never felt the
need to use this form of <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code> (in Perl, that is--C is another matter).
(The difference being that C does not offer named loops combined with
loop control.  Perl does, and this replaces most structured uses of <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code>
in other languages.)</p>
<p>The <code class="inline"><a class="l_k" href="../functions/goto.html">goto-EXPR</a></code> form expects a label name, whose scope will be resolved
dynamically.  This allows for computed <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code>s per FORTRAN, but isn&#39;t
necessarily recommended if you&#39;re optimizing for maintainability:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/goto.html">goto</a> <span class="s">(</span><span class="q">&quot;FOO&quot;</span><span class="cm">,</span> <span class="q">&quot;BAR&quot;</span><span class="cm">,</span> <span class="q">&quot;GLARCH&quot;</span><span class="s">)</span>[<span class="i">$i</span>]<span class="sc">;</span></li></ol></pre><p>The <code class="inline"><a class="l_k" href="../functions/goto.html">goto-&NAME</a></code> form is quite different from the other forms of
<code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code>.  In fact, it isn&#39;t a goto in the normal sense at all, and
doesn&#39;t have the stigma associated with other gotos.  Instead, it
exits the current subroutine (losing any changes set by local()) and
immediately calls in its place the named subroutine using the current
value of @_.  This is used by <code class="inline">AUTOLOAD</code>
 subroutines that wish to
load another subroutine and then pretend that the other subroutine had
been called in the first place (except that any modifications to <code class="inline"><span class="i">@_</span></code>

in the current subroutine are propagated to the other subroutine.)
After the <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code>, not even <code class="inline"><a class="l_k" href="../functions/caller.html">caller</a></code> will be able to tell that this
routine was called first.</p>
<p>NAME needn&#39;t be the name of a subroutine; it can be a scalar variable
containing a code reference, or a block that evaluates to a code
reference.
-----

function goto
expr|
The <code class="inline"><a class="l_k" href="../functions/goto.html">goto-LABEL</a></code> form finds the statement labeled with LABEL and resumes
execution there.  It may not be used to go into any construct that
requires initialization, such as a subroutine or a <code class="inline">foreach</code>
 loop.  It
also can&#39;t be used to go into a construct that is optimized away,
or to get out of a block or subroutine given to <code class="inline"><a class="l_k" href="../functions/sort.html">sort</a></code>.
It can be used to go almost anywhere else within the dynamic scope,
including out of subroutines, but it&#39;s usually better to use some other
construct such as <code class="inline"><a class="l_k" href="../functions/last.html">last</a></code> or <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>.  The author of Perl has never felt the
need to use this form of <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code> (in Perl, that is--C is another matter).
(The difference being that C does not offer named loops combined with
loop control.  Perl does, and this replaces most structured uses of <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code>
in other languages.)</p>
<p>The <code class="inline"><a class="l_k" href="../functions/goto.html">goto-EXPR</a></code> form expects a label name, whose scope will be resolved
dynamically.  This allows for computed <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code>s per FORTRAN, but isn&#39;t
necessarily recommended if you&#39;re optimizing for maintainability:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/goto.html">goto</a> <span class="s">(</span><span class="q">&quot;FOO&quot;</span><span class="cm">,</span> <span class="q">&quot;BAR&quot;</span><span class="cm">,</span> <span class="q">&quot;GLARCH&quot;</span><span class="s">)</span>[<span class="i">$i</span>]<span class="sc">;</span></li></ol></pre><p>The <code class="inline"><a class="l_k" href="../functions/goto.html">goto-&NAME</a></code> form is quite different from the other forms of
<code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code>.  In fact, it isn&#39;t a goto in the normal sense at all, and
doesn&#39;t have the stigma associated with other gotos.  Instead, it
exits the current subroutine (losing any changes set by local()) and
immediately calls in its place the named subroutine using the current
value of @_.  This is used by <code class="inline">AUTOLOAD</code>
 subroutines that wish to
load another subroutine and then pretend that the other subroutine had
been called in the first place (except that any modifications to <code class="inline"><span class="i">@_</span></code>

in the current subroutine are propagated to the other subroutine.)
After the <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code>, not even <code class="inline"><a class="l_k" href="../functions/caller.html">caller</a></code> will be able to tell that this
routine was called first.</p>
<p>NAME needn&#39;t be the name of a subroutine; it can be a scalar variable
containing a code reference, or a block that evaluates to a code
reference.
-----

function goto
&amp;name|
The <code class="inline"><a class="l_k" href="../functions/goto.html">goto-LABEL</a></code> form finds the statement labeled with LABEL and resumes
execution there.  It may not be used to go into any construct that
requires initialization, such as a subroutine or a <code class="inline">foreach</code>
 loop.  It
also can&#39;t be used to go into a construct that is optimized away,
or to get out of a block or subroutine given to <code class="inline"><a class="l_k" href="../functions/sort.html">sort</a></code>.
It can be used to go almost anywhere else within the dynamic scope,
including out of subroutines, but it&#39;s usually better to use some other
construct such as <code class="inline"><a class="l_k" href="../functions/last.html">last</a></code> or <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>.  The author of Perl has never felt the
need to use this form of <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code> (in Perl, that is--C is another matter).
(The difference being that C does not offer named loops combined with
loop control.  Perl does, and this replaces most structured uses of <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code>
in other languages.)</p>
<p>The <code class="inline"><a class="l_k" href="../functions/goto.html">goto-EXPR</a></code> form expects a label name, whose scope will be resolved
dynamically.  This allows for computed <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code>s per FORTRAN, but isn&#39;t
necessarily recommended if you&#39;re optimizing for maintainability:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/goto.html">goto</a> <span class="s">(</span><span class="q">&quot;FOO&quot;</span><span class="cm">,</span> <span class="q">&quot;BAR&quot;</span><span class="cm">,</span> <span class="q">&quot;GLARCH&quot;</span><span class="s">)</span>[<span class="i">$i</span>]<span class="sc">;</span></li></ol></pre><p>The <code class="inline"><a class="l_k" href="../functions/goto.html">goto-&NAME</a></code> form is quite different from the other forms of
<code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code>.  In fact, it isn&#39;t a goto in the normal sense at all, and
doesn&#39;t have the stigma associated with other gotos.  Instead, it
exits the current subroutine (losing any changes set by local()) and
immediately calls in its place the named subroutine using the current
value of @_.  This is used by <code class="inline">AUTOLOAD</code>
 subroutines that wish to
load another subroutine and then pretend that the other subroutine had
been called in the first place (except that any modifications to <code class="inline"><span class="i">@_</span></code>

in the current subroutine are propagated to the other subroutine.)
After the <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code>, not even <code class="inline"><a class="l_k" href="../functions/caller.html">caller</a></code> will be able to tell that this
routine was called first.</p>
<p>NAME needn&#39;t be the name of a subroutine; it can be a scalar variable
containing a code reference, or a block that evaluates to a code
reference.
-----

function grep
block list|
This is similar in spirit to, but not the same as, grep(1) and its
relatives.  In particular, it is not limited to using regular expressions.</p>
<p>Evaluates the BLOCK or EXPR for each element of LIST (locally setting
<code class="inline"><span class="i">$_</span></code>
 to each element) and returns the list value consisting of those
elements for which the expression evaluated to true.  In scalar
context, returns the number of times the expression was true.</p>
<pre class="verbatim"><ol><li>    <span class="i">@foo</span> = <a class="l_k" href="../functions/grep.html">grep</a><span class="s">(</span>!<span class="q">/^#/</span><span class="cm">,</span> <span class="i">@bar</span><span class="s">)</span><span class="sc">;</span>    <span class="c"># weed out comments</span></li></ol></pre><p>or equivalently,</p>
<pre class="verbatim"><ol><li>    <span class="i">@foo</span> = <a class="l_k" href="../functions/grep.html">grep</a> <span class="s">{</span>!<span class="q">/^#/</span><span class="s">}</span> <span class="i">@bar</span><span class="sc">;</span>    <span class="c"># weed out comments</span></li></ol></pre><p>Note that <code class="inline"><span class="i">$_</span></code>
 is an alias to the list value, so it can be used to
modify the elements of the LIST.  While this is useful and supported,
it can cause bizarre results if the elements of LIST are not variables.
Similarly, grep returns aliases into the original list, much as a for
loop&#39;s index variable aliases the list elements.  That is, modifying an
element of a list returned by grep (for example, in a <code class="inline">foreach</code>
, <code class="inline"><a class="l_k" href="../functions/map.html">map</a></code>
or another <code class="inline"><a class="l_k" href="../functions/grep.html">grep</a></code>) actually modifies the element in the original list.
This is usually something to be avoided when writing clear code.</p>
<p>If <code class="inline"><span class="i">$_</span></code>
 is lexical in the scope where the <code class="inline"><a class="l_k" href="../functions/grep.html">grep</a></code> appears (because it has
been declared with <code class="inline"><a class="l_k" href="../functions/my.html">my</a> <span class="i">$_</span></code>
) then, in addition to being locally aliased to
the list elements, <code class="inline"><span class="i">$_</span></code>
 keeps being lexical inside the block; i.e. it
can&#39;t be seen from the outside, avoiding any potential side-effects.</p>
<p>See also <a href="../functions/map.html">"map"</a> for a list composed of the results of the BLOCK or EXPR.
-----

function grep
expr|
list|
This is similar in spirit to, but not the same as, grep(1) and its
relatives.  In particular, it is not limited to using regular expressions.</p>
<p>Evaluates the BLOCK or EXPR for each element of LIST (locally setting
<code class="inline"><span class="i">$_</span></code>
 to each element) and returns the list value consisting of those
elements for which the expression evaluated to true.  In scalar
context, returns the number of times the expression was true.</p>
<pre class="verbatim"><ol><li>    <span class="i">@foo</span> = <a class="l_k" href="../functions/grep.html">grep</a><span class="s">(</span>!<span class="q">/^#/</span><span class="cm">,</span> <span class="i">@bar</span><span class="s">)</span><span class="sc">;</span>    <span class="c"># weed out comments</span></li></ol></pre><p>or equivalently,</p>
<pre class="verbatim"><ol><li>    <span class="i">@foo</span> = <a class="l_k" href="../functions/grep.html">grep</a> <span class="s">{</span>!<span class="q">/^#/</span><span class="s">}</span> <span class="i">@bar</span><span class="sc">;</span>    <span class="c"># weed out comments</span></li></ol></pre><p>Note that <code class="inline"><span class="i">$_</span></code>
 is an alias to the list value, so it can be used to
modify the elements of the LIST.  While this is useful and supported,
it can cause bizarre results if the elements of LIST are not variables.
Similarly, grep returns aliases into the original list, much as a for
loop&#39;s index variable aliases the list elements.  That is, modifying an
element of a list returned by grep (for example, in a <code class="inline">foreach</code>
, <code class="inline"><a class="l_k" href="../functions/map.html">map</a></code>
or another <code class="inline"><a class="l_k" href="../functions/grep.html">grep</a></code>) actually modifies the element in the original list.
This is usually something to be avoided when writing clear code.</p>
<p>If <code class="inline"><span class="i">$_</span></code>
 is lexical in the scope where the <code class="inline"><a class="l_k" href="../functions/grep.html">grep</a></code> appears (because it has
been declared with <code class="inline"><a class="l_k" href="../functions/my.html">my</a> <span class="i">$_</span></code>
) then, in addition to being locally aliased to
the list elements, <code class="inline"><span class="i">$_</span></code>
 keeps being lexical inside the block; i.e. it
can&#39;t be seen from the outside, avoiding any potential side-effects.</p>
<p>See also <a href="../functions/map.html">"map"</a> for a list composed of the results of the BLOCK or EXPR.
-----

function hex
expr|
Interprets EXPR as a hex string and returns the corresponding value.
(To convert strings that might start with either <code class="inline"><span class="n">0</span></code>
, <code class="inline"><span class="n">0</span>x</code>
, or <code class="inline">0b</code>, see
<a href="../functions/oct.html">"oct"</a>.)  If EXPR is omitted, uses <code class="inline"><span class="i">$_</span></code>
.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/hex.html">hex</a> <span class="q">&#39;0xAf&#39;</span><span class="sc">;</span> <span class="c"># prints &#39;175&#39;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/hex.html">hex</a> <span class="q">&#39;aF&#39;</span><span class="sc">;</span>   <span class="c"># same</span></li></ol></pre><p>Hex strings may only represent integers.  Strings that would cause
integer overflow trigger a warning.  Leading whitespace is not stripped,
unlike oct(). To present something as hex, look into <a href="../functions/printf.html">"printf"</a>,
<a href="../functions/sprintf.html">"sprintf"</a>, or <a href="../functions/unpack.html">"unpack"</a>.
-----

function hex
Interprets EXPR as a hex string and returns the corresponding value.
(To convert strings that might start with either <code class="inline"><span class="n">0</span></code>
, <code class="inline"><span class="n">0</span>x</code>
, or <code class="inline">0b</code>, see
<a href="../functions/oct.html">"oct"</a>.)  If EXPR is omitted, uses <code class="inline"><span class="i">$_</span></code>
.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/hex.html">hex</a> <span class="q">&#39;0xAf&#39;</span><span class="sc">;</span> <span class="c"># prints &#39;175&#39;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/hex.html">hex</a> <span class="q">&#39;aF&#39;</span><span class="sc">;</span>   <span class="c"># same</span></li></ol></pre><p>Hex strings may only represent integers.  Strings that would cause
integer overflow trigger a warning.  Leading whitespace is not stripped,
unlike oct(). To present something as hex, look into <a href="../functions/printf.html">"printf"</a>,
<a href="../functions/sprintf.html">"sprintf"</a>, or <a href="../functions/unpack.html">"unpack"</a>.
-----

function import
list|
There is no builtin <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> function.  It is just an ordinary
method (subroutine) defined (or inherited) by modules that wish to export
names to another module.  The <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code> function calls the <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> method
for the package used.  See also <a href="../functions/use.html">"use"</a>, <a href="../perlmod.html">perlmod</a>, and <a href="../Exporter.html">Exporter</a>.
-----

function index
str|
substr|
position|
The index function searches for one string within another, but without
the wildcard-like behavior of a full regular-expression pattern match.
It returns the position of the first occurrence of SUBSTR in STR at
or after POSITION.  If POSITION is omitted, starts searching from the
beginning of the string.  POSITION before the beginning of the string
or after its end is treated as if it were the beginning or the end,
respectively.  POSITION and the return value are based at <code class="inline"><span class="n">0</span></code>
 (or whatever
you&#39;ve set the <code class="inline"><span class="i">$[</span></code>
 variable to--but don&#39;t do that).  If the substring
is not found, <code class="inline"><a class="l_k" href="../functions/index.html">index</a></code> returns one less than the base, ordinarily <code class="inline"><span class="n">-1</span></code>
.
-----

function index
str|
substr|
The index function searches for one string within another, but without
the wildcard-like behavior of a full regular-expression pattern match.
It returns the position of the first occurrence of SUBSTR in STR at
or after POSITION.  If POSITION is omitted, starts searching from the
beginning of the string.  POSITION before the beginning of the string
or after its end is treated as if it were the beginning or the end,
respectively.  POSITION and the return value are based at <code class="inline"><span class="n">0</span></code>
 (or whatever
you&#39;ve set the <code class="inline"><span class="i">$[</span></code>
 variable to--but don&#39;t do that).  If the substring
is not found, <code class="inline"><a class="l_k" href="../functions/index.html">index</a></code> returns one less than the base, ordinarily <code class="inline"><span class="n">-1</span></code>
.
-----

function int
expr|
Returns the integer portion of EXPR.  If EXPR is omitted, uses <code class="inline"><span class="i">$_</span></code>
.
You should not use this function for rounding: one because it truncates
towards <code class="inline"><span class="n">0</span></code>
, and two because machine representations of floating point
numbers can sometimes produce counterintuitive results.  For example,
<code class="inline"><a class="l_k" href="../functions/int.html">int(-6.725/0.025)</a></code> produces -268 rather than the correct -269; that&#39;s
because it&#39;s really more like -268.99999999999994315658 instead.  Usually,
the <code class="inline"><a class="l_k" href="../functions/sprintf.html">sprintf</a></code>, <code class="inline"><a class="l_k" href="../functions/printf.html">printf</a></code>, or the <code class="inline"><span class="w">POSIX::floor</span></code>
 and <code class="inline"><span class="w">POSIX::ceil</span></code>

functions will serve you better than will int().
-----

function int
Returns the integer portion of EXPR.  If EXPR is omitted, uses <code class="inline"><span class="i">$_</span></code>
.
You should not use this function for rounding: one because it truncates
towards <code class="inline"><span class="n">0</span></code>
, and two because machine representations of floating point
numbers can sometimes produce counterintuitive results.  For example,
<code class="inline"><a class="l_k" href="../functions/int.html">int(-6.725/0.025)</a></code> produces -268 rather than the correct -269; that&#39;s
because it&#39;s really more like -268.99999999999994315658 instead.  Usually,
the <code class="inline"><a class="l_k" href="../functions/sprintf.html">sprintf</a></code>, <code class="inline"><a class="l_k" href="../functions/printf.html">printf</a></code>, or the <code class="inline"><span class="w">POSIX::floor</span></code>
 and <code class="inline"><span class="w">POSIX::ceil</span></code>

functions will serve you better than will int().
-----

function ioctl
filehandle|
function|
scalar|
Implements the ioctl(2) function.  You&#39;ll probably first have to say</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/require.html">require</a> <span class="q">&quot;sys/ioctl.ph&quot;</span><span class="sc">;</span>	<span class="c"># probably in $Config{archlib}/sys/ioctl.ph</span></li></ol></pre><p>to get the correct function definitions.  If <i>sys/ioctl.ph</i> doesn&#39;t
exist or doesn&#39;t have the correct definitions you&#39;ll have to roll your
own, based on your C header files such as <i>&lt;sys/ioctl.h&gt;</i>.
(There is a Perl script called <b>h2ph</b> that comes with the Perl kit that
may help you in this, but it&#39;s nontrivial.)  SCALAR will be read and/or
written depending on the FUNCTION--a pointer to the string value of SCALAR
will be passed as the third argument of the actual <code class="inline"><a class="l_k" href="../functions/ioctl.html">ioctl</a></code> call.  (If SCALAR
has no string value but does have a numeric value, that value will be
passed rather than a pointer to the string value.  To guarantee this to be
true, add a <code class="inline"><span class="n">0</span></code>
 to the scalar before using it.)  The <code class="inline"><a class="l_k" href="../functions/pack.html">pack</a></code> and <code class="inline"><a class="l_k" href="../functions/unpack.html">unpack</a></code>
functions may be needed to manipulate the values of structures used by
<code class="inline"><a class="l_k" href="../functions/ioctl.html">ioctl</a></code>.</p>
<p>The return value of <code class="inline"><a class="l_k" href="../functions/ioctl.html">ioctl</a></code> (and <code class="inline"><a class="l_k" href="../functions/fcntl.html">fcntl</a></code>) is as follows:</p>
<pre class="verbatim"><ol><li>	if OS returns:		then Perl returns:</li><li>	    -1	  		  undefined value</li><li>	     0	 		string "0 but true"</li><li>	anything else		    that number</li></ol></pre><p>Thus Perl returns true on success and false on failure, yet you can
still easily determine the actual value returned by the operating
system:</p>
<pre class="verbatim"><ol><li>    <span class="i">$retval</span> = <a class="l_k" href="../functions/ioctl.html">ioctl</a><span class="s">(</span>...<span class="s">)</span> || <span class="n">-1</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;System returned %d\n&quot;</span><span class="cm">,</span> <span class="i">$retval</span><span class="sc">;</span></li></ol></pre><p>The special string <code class="inline"><span class="q">&quot;0 but true&quot;</span></code>
 is exempt from <b>-w</b> complaints
about improper numeric conversions.
-----

function join
expr|
list|
Joins the separate strings of LIST into a single string with fields
separated by the value of EXPR, and returns that new string.  Example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$rec</span> = <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">&#39;:&#39;</span><span class="cm">,</span> <span class="i">$login</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$home</span><span class="cm">,</span><span class="i">$shell</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Beware that unlike <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code>, <code class="inline"><a class="l_k" href="../functions/join.html">join</a></code> doesn&#39;t take a pattern as its
first argument.  Compare <a href="../functions/split.html">"split"</a>.
-----

function keys
hash|
Returns a list consisting of all the keys of the named hash.
(In scalar context, returns the number of keys.)</p>
<p>The keys are returned in an apparently random order.  The actual
random order is subject to change in future versions of perl, but it
is guaranteed to be the same order as either the <code class="inline"><a class="l_k" href="../functions/values.html">values</a></code> or <code class="inline"><a class="l_k" href="../functions/each.html">each</a></code>
function produces (given that the hash has not been modified).  Since
Perl 5.8.1 the ordering is different even between different runs of
Perl for security reasons (see <a href="../perlsec.html#Algorithmic-Complexity-Attacks">"Algorithmic Complexity Attacks" in perlsec</a>).</p>
<p>As a side effect, calling keys() resets the HASH&#39;s internal iterator
(see <a href="../functions/each.html">"each"</a>).  In particular, calling keys() in void context resets
the iterator with no other overhead.</p>
<p>Here is yet another way to print your environment:</p>
<pre class="verbatim"><ol><li>    <span class="i">@keys</span> = <a class="l_k" href="../functions/keys.html">keys</a> <span class="i">%ENV</span><span class="sc">;</span></li><li>    <span class="i">@values</span> = <a class="l_k" href="../functions/values.html">values</a> <span class="i">%ENV</span><span class="sc">;</span></li><li>    while <span class="s">(</span><span class="i">@keys</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/pop.html">pop</a><span class="s">(</span><span class="i">@keys</span><span class="s">)</span><span class="cm">,</span> <span class="q">&#39;=&#39;</span><span class="cm">,</span> <a class="l_k" href="../functions/pop.html">pop</a><span class="s">(</span><span class="i">@values</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>or how about sorted by key:</p>
<pre class="verbatim"><ol><li>    foreach <span class="i">$key</span> <span class="s">(</span><a class="l_k" href="../functions/sort.html">sort</a><span class="s">(</span><a class="l_k" href="../functions/keys.html">keys</a> <span class="i">%ENV</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="i">$key</span><span class="cm">,</span> <span class="q">&#39;=&#39;</span><span class="cm">,</span> <span class="i">$ENV</span>{<span class="i">$key</span>}<span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>The returned values are copies of the original keys in the hash, so
modifying them will not affect the original hash.  Compare <a href="../functions/values.html">"values"</a>.</p>
<p>To sort a hash by value, you&#39;ll need to use a <code class="inline"><a class="l_k" href="../functions/sort.html">sort</a></code> function.
Here&#39;s a descending numeric sort of a hash by its values:</p>
<pre class="verbatim"><ol><li>    foreach <span class="i">$key</span> <span class="s">(</span><a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$hash</span>{<span class="i">$b</span>} &lt;=&gt; <span class="i">$hash</span>{<span class="i">$a</span>} <span class="s">}</span> <a class="l_k" href="../functions/keys.html">keys</a> <span class="i">%hash</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;%4d %s\n&quot;</span><span class="cm">,</span> <span class="i">$hash</span>{<span class="i">$key</span>}<span class="cm">,</span> <span class="i">$key</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>As an lvalue <code class="inline"><a class="l_k" href="../functions/keys.html">keys</a></code> allows you to increase the number of hash buckets
allocated for the given hash.  This can gain you a measure of efficiency if
you know the hash is going to get big.  (This is similar to pre-extending
an array by assigning a larger number to $#array.)  If you say</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/keys.html">keys</a> <span class="i">%hash</span> = <span class="n">200</span><span class="sc">;</span></li></ol></pre><p>then <code class="inline"><span class="i">%hash</span></code>
 will have at least 200 buckets allocated for it--256 of them,
in fact, since it rounds up to the next power of two.  These
buckets will be retained even if you do <code class="inline"><span class="i">%hash</span> = <span class="s">(</span><span class="s">)</span></code>
, use <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a>
<span class="i">%hash</span></code>
 if you want to free the storage while <code class="inline"><span class="i">%hash</span></code>
 is still in scope.
You can&#39;t shrink the number of buckets allocated for the hash using
<code class="inline"><a class="l_k" href="../functions/keys.html">keys</a></code> in this way (but you needn&#39;t worry about doing this by accident,
as trying has no effect).</p>
<p>See also <code class="inline"><a class="l_k" href="../functions/each.html">each</a></code>, <code class="inline"><a class="l_k" href="../functions/values.html">values</a></code> and <code class="inline"><a class="l_k" href="../functions/sort.html">sort</a></code>.
-----

function kill
signal|
list|
Sends a signal to a list of processes.  Returns the number of
processes successfully signaled (which is not necessarily the
same as the number actually killed).</p>
<pre class="verbatim"><ol><li>    <span class="i">$cnt</span> = <a class="l_k" href="../functions/kill.html">kill</a> <span class="n">1</span><span class="cm">,</span> <span class="i">$child1</span><span class="cm">,</span> <span class="i">$child2</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/kill.html">kill</a> <span class="n">9</span><span class="cm">,</span> <span class="i">@goners</span><span class="sc">;</span></li></ol></pre><p>If SIGNAL is zero, no signal is sent to the process, but the kill(2)
system call will check whether it&#39;s possible to send a signal to it (that
means, to be brief, that the process is owned by the same user, or we are
the super-user).  This is a useful way to check that a child process is
alive (even if only as a zombie) and hasn&#39;t changed its UID.  See
<a href="../perlport.html">perlport</a> for notes on the portability of this construct.</p>
<p>Unlike in the shell, if SIGNAL is negative, it kills
process groups instead of processes.  (On System V, a negative <i>PROCESS</i>
number will also kill process groups, but that&#39;s not portable.)  That
means you usually want to use positive not negative signals.  You may also
use a signal name in quotes.</p>
<p>See <a href="../perlipc.html#Signals">"Signals" in perlipc</a> for more details.
-----

function last
label|
The <code class="inline"><a class="l_k" href="../functions/last.html">last</a></code> command is like the <code class="inline"><span class="w">break</span></code>
 statement in C (as used in
loops); it immediately exits the loop in question.  If the LABEL is
omitted, the command refers to the innermost enclosing loop.  The
<code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code> block, if any, is not executed:</p>
<pre class="verbatim"><ol><li>    <span class="j">LINE:</span> while <span class="s">(</span><span class="q">&lt;STDIN&gt;</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/last.html">last</a> <span class="j">LINE</span> if <span class="q">/^$/</span><span class="sc">;</span>	<span class="c"># exit when done with header</span></li><li>	<span class="c">#...</span></li><li>    <span class="s">}</span></li></ol></pre><p><code class="inline"><a class="l_k" href="../functions/last.html">last</a></code> cannot be used to exit a block which returns a value such as
<code class="inline"><a class="l_k" href="../functions/eval.html">eval</a> <span class="s">{</span><span class="s">}</span></code>
, <code class="inline"><a class="l_k" href="../functions/sub.html">sub</a> <span class="s">{</span><span class="s">}</span></code>
 or <code class="inline"><a class="l_k" href="../functions/do.html">do</a> <span class="s">{</span><span class="s">}</span></code>
, and should not be used to exit
a grep() or map() operation.</p>
<p>Note that a block by itself is semantically identical to a loop
that executes once.  Thus <code class="inline"><a class="l_k" href="../functions/last.html">last</a></code> can be used to effect an early
exit out of such a block.</p>
<p>See also <a href="../functions/continue.html">"continue"</a> for an illustration of how <code class="inline"><a class="l_k" href="../functions/last.html">last</a></code>, <code class="inline"><a class="l_k" href="../functions/next.html">next</a></code>, and
<code class="inline"><a class="l_k" href="../functions/redo.html">redo</a></code> work.
-----

function last
The <code class="inline"><a class="l_k" href="../functions/last.html">last</a></code> command is like the <code class="inline"><span class="w">break</span></code>
 statement in C (as used in
loops); it immediately exits the loop in question.  If the LABEL is
omitted, the command refers to the innermost enclosing loop.  The
<code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code> block, if any, is not executed:</p>
<pre class="verbatim"><ol><li>    <span class="j">LINE:</span> while <span class="s">(</span><span class="q">&lt;STDIN&gt;</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/last.html">last</a> <span class="j">LINE</span> if <span class="q">/^$/</span><span class="sc">;</span>	<span class="c"># exit when done with header</span></li><li>	<span class="c">#...</span></li><li>    <span class="s">}</span></li></ol></pre><p><code class="inline"><a class="l_k" href="../functions/last.html">last</a></code> cannot be used to exit a block which returns a value such as
<code class="inline"><a class="l_k" href="../functions/eval.html">eval</a> <span class="s">{</span><span class="s">}</span></code>
, <code class="inline"><a class="l_k" href="../functions/sub.html">sub</a> <span class="s">{</span><span class="s">}</span></code>
 or <code class="inline"><a class="l_k" href="../functions/do.html">do</a> <span class="s">{</span><span class="s">}</span></code>
, and should not be used to exit
a grep() or map() operation.</p>
<p>Note that a block by itself is semantically identical to a loop
that executes once.  Thus <code class="inline"><a class="l_k" href="../functions/last.html">last</a></code> can be used to effect an early
exit out of such a block.</p>
<p>See also <a href="../functions/continue.html">"continue"</a> for an illustration of how <code class="inline"><a class="l_k" href="../functions/last.html">last</a></code>, <code class="inline"><a class="l_k" href="../functions/next.html">next</a></code>, and
<code class="inline"><a class="l_k" href="../functions/redo.html">redo</a></code> work.
-----

function lc
expr|
Returns a lowercased version of EXPR.  This is the internal function
implementing the <code class="inline">\<span class="w">L</span></code>
 escape in double-quoted strings.  Respects
current LC_CTYPE locale if <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">locale</span></code>
 in force.  See <a href="../perllocale.html">perllocale</a>
and <a href="../perlunicode.html">perlunicode</a> for more details about locale and Unicode support.</p>
<p>If EXPR is omitted, uses <code class="inline"><span class="i">$_</span></code>
.
-----

function lc
Returns a lowercased version of EXPR.  This is the internal function
implementing the <code class="inline">\<span class="w">L</span></code>
 escape in double-quoted strings.  Respects
current LC_CTYPE locale if <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">locale</span></code>
 in force.  See <a href="../perllocale.html">perllocale</a>
and <a href="../perlunicode.html">perlunicode</a> for more details about locale and Unicode support.</p>
<p>If EXPR is omitted, uses <code class="inline"><span class="i">$_</span></code>
.
-----

function lcfirst
expr|
Returns the value of EXPR with the first character lowercased.  This
is the internal function implementing the <code class="inline">\<span class="w">l</span></code>
 escape in
double-quoted strings.  Respects current LC_CTYPE locale if <code class="inline"><a class="l_k" href="../functions/use.html">use</a>
<span class="w">locale</span></code>
 in force.  See <a href="../perllocale.html">perllocale</a> and <a href="../perlunicode.html">perlunicode</a> for more
details about locale and Unicode support.</p>
<p>If EXPR is omitted, uses <code class="inline"><span class="i">$_</span></code>
.
-----

function lcfirst
Returns the value of EXPR with the first character lowercased.  This
is the internal function implementing the <code class="inline">\<span class="w">l</span></code>
 escape in
double-quoted strings.  Respects current LC_CTYPE locale if <code class="inline"><a class="l_k" href="../functions/use.html">use</a>
<span class="w">locale</span></code>
 in force.  See <a href="../perllocale.html">perllocale</a> and <a href="../perlunicode.html">perlunicode</a> for more
details about locale and Unicode support.</p>
<p>If EXPR is omitted, uses <code class="inline"><span class="i">$_</span></code>
.
-----

function length
expr|
Returns the length in <i>characters</i> of the value of EXPR.  If EXPR is
omitted, returns length of <code class="inline"><span class="i">$_</span></code>
.  Note that this cannot be used on
an entire array or hash to find out how many elements these have.
For that, use <code class="inline"><a class="l_k" href="../functions/scalar.html">scalar</a> <span class="i">@array</span></code>
 and <code class="inline"><a class="l_k" href="../functions/scalar.html">scalar</a> <a class="l_k" href="../functions/keys.html">keys</a> <span class="i">%hash</span></code>
 respectively.</p>
<p>Note the <i>characters</i>: if the EXPR is in Unicode, you will get the
number of characters, not the number of bytes.  To get the length
of the internal string in bytes, use <code class="inline"><span class="i">bytes::length</span><span class="s">(</span><span class="w">EXPR</span><span class="s">)</span></code>
, see
<a href="../bytes.html">bytes</a>.  Note that the internal encoding is variable, and the number
of bytes usually meaningless.  To get the number of bytes that the
string would have when encoded as UTF-8, use
<code class="inline"><a class="l_k" href="../functions/length.html">length(Encoding::encode_utf8(EXPR))</a></code>.
-----

function length
Returns the length in <i>characters</i> of the value of EXPR.  If EXPR is
omitted, returns length of <code class="inline"><span class="i">$_</span></code>
.  Note that this cannot be used on
an entire array or hash to find out how many elements these have.
For that, use <code class="inline"><a class="l_k" href="../functions/scalar.html">scalar</a> <span class="i">@array</span></code>
 and <code class="inline"><a class="l_k" href="../functions/scalar.html">scalar</a> <a class="l_k" href="../functions/keys.html">keys</a> <span class="i">%hash</span></code>
 respectively.</p>
<p>Note the <i>characters</i>: if the EXPR is in Unicode, you will get the
number of characters, not the number of bytes.  To get the length
of the internal string in bytes, use <code class="inline"><span class="i">bytes::length</span><span class="s">(</span><span class="w">EXPR</span><span class="s">)</span></code>
, see
<a href="../bytes.html">bytes</a>.  Note that the internal encoding is variable, and the number
of bytes usually meaningless.  To get the number of bytes that the
string would have when encoded as UTF-8, use
<code class="inline"><a class="l_k" href="../functions/length.html">length(Encoding::encode_utf8(EXPR))</a></code>.
-----

function link
oldfile|
newfile|
Creates a new filename linked to the old filename.  Returns true for
success, false otherwise.
-----

function listen
socket|
queuesize|
Does the same thing that the listen system call does.  Returns true if
it succeeded, false otherwise.  See the example in
<a href="../perlipc.html#Sockets%3a-Client%2fServer-Communication">"Sockets: Client/Server Communication" in perlipc</a>.
-----

function local
expr|
You really probably want to be using <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> instead, because <code class="inline"><a class="l_k" href="../functions/local.html">local</a></code> isn&#39;t
what most people think of as &quot;local&quot;.  See
<a href="../perlsub.html#Private-Variables-via-my()">"Private Variables via my()" in perlsub</a> for details.</p>
<p>A local modifies the listed variables to be local to the enclosing
block, file, or eval.  If more than one value is listed, the list must
be placed in parentheses.  See <a href="../perlsub.html#Temporary-Values-via-local()">"Temporary Values via local()" in perlsub</a>
for details, including issues with tied arrays and hashes.
-----

function localtime
expr|
Converts a time as returned by the time function to a 9-element list
with the time analyzed for the local time zone.  Typically used as
follows:</p>
<pre class="verbatim"><ol><li>    <span class="c">#  0    1    2     3     4    5     6     7     8</span></li><li>    <span class="s">(</span><span class="i">$sec</span><span class="cm">,</span><span class="i">$min</span><span class="cm">,</span><span class="i">$hour</span><span class="cm">,</span><span class="i">$mday</span><span class="cm">,</span><span class="i">$mon</span><span class="cm">,</span><span class="i">$year</span><span class="cm">,</span><span class="i">$wday</span><span class="cm">,</span><span class="i">$yday</span><span class="cm">,</span><span class="i">$isdst</span><span class="s">)</span> =</li><li>                                                <a class="l_k" href="../functions/localtime.html">localtime</a><span class="s">(</span><a class="l_k" href="../functions/time.html">time</a><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>All list elements are numeric, and come straight out of the C `struct
tm&#39;.  <code class="inline"><span class="i">$sec</span></code>
, <code class="inline"><span class="i">$min</span></code>
, and <code class="inline"><span class="i">$hour</span></code>
 are the seconds, minutes, and hours
of the specified time.</p>
<p><code class="inline"><span class="i">$mday</span></code>
 is the day of the month, and <code class="inline"><span class="i">$mon</span></code>
 is the month itself, in
the range <code class="inline"><span class="n">0</span>..<span class="n">11</span></code>
 with 0 indicating January and 11 indicating December.
This makes it easy to get a month name from a list:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">@abbr</span> = <span class="q">qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec )</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;$abbr[$mon] $mday&quot;</span><span class="sc">;</span></li><li>    <span class="c"># $mon=9, $mday=18 gives &quot;Oct 18&quot;</span></li></ol></pre><p><code class="inline"><span class="i">$year</span></code>
 is the number of years since 1900, not just the last two digits
of the year.  That is, <code class="inline"><span class="i">$year</span></code>
 is <code class="inline"><span class="n">123</span></code>
 in year 2023.  The proper way
to get a complete 4-digit year is simply:</p>
<pre class="verbatim"><ol><li>    <span class="i">$year</span> += <span class="n">1900</span><span class="sc">;</span></li></ol></pre><p>Otherwise you create non-Y2K-compliant programs--and you wouldn&#39;t want
to do that, would you?</p>
<p>To get the last two digits of the year (e.g., &#39;01&#39; in 2001) do:</p>
<pre class="verbatim"><ol><li>    <span class="i">$year</span> = <a class="l_k" href="../functions/sprintf.html">sprintf</a><span class="s">(</span><span class="q">&quot;%02d&quot;</span><span class="cm">,</span> <span class="i">$year</span> % <span class="n">100</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p><code class="inline"><span class="i">$wday</span></code>
 is the day of the week, with 0 indicating Sunday and 3 indicating
Wednesday.  <code class="inline"><span class="i">$yday</span></code>
 is the day of the year, in the range <code class="inline"><span class="n">0</span>..<span class="n">364</span></code>

(or <code class="inline"><span class="n">0</span>..<span class="n">365</span></code>
 in leap years.)</p>
<p><code class="inline"><span class="i">$isdst</span></code>
 is true if the specified time occurs during Daylight Saving
Time, false otherwise.</p>
<p>If EXPR is omitted, <code class="inline"><a class="l_k" href="../functions/localtime.html">localtime()</a></code> uses the current time (as returned
by time(3)).</p>
<p>In scalar context, <code class="inline"><a class="l_k" href="../functions/localtime.html">localtime()</a></code> returns the ctime(3) value:</p>
<pre class="verbatim"><ol><li>    <span class="i">$now_string</span> = <a class="l_k" href="../functions/localtime.html">localtime</a><span class="sc">;</span>  <span class="c"># e.g., &quot;Thu Oct 13 04:54:34 1994&quot;</span></li></ol></pre><p>This scalar value is <b>not</b> locale dependent but is a Perl builtin. For GMT
instead of local time use the <a href="../functions/gmtime.html">"gmtime"</a> builtin. See also the
<code class="inline"><span class="w">Time::Local</span></code>
 module (to convert the second, minutes, hours, ... back to
the integer value returned by time()), and the <a href="../POSIX.html">POSIX</a> module&#39;s strftime(3)
and mktime(3) functions.</p>
<p>To get somewhat similar but locale dependent date strings, set up your
locale environment variables appropriately (please see <a href="../perllocale.html">perllocale</a>) and
try for example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">POSIX</span> <span class="q">qw(strftime)</span><span class="sc">;</span></li><li>    <span class="i">$now_string</span> = <span class="w">strftime</span> <span class="q">&quot;%a %b %e %H:%M:%S %Y&quot;</span><span class="cm">,</span> <a class="l_k" href="../functions/localtime.html">localtime</a><span class="sc">;</span></li><li>    <span class="c"># or for GMT formatted appropriately for your locale:</span></li><li>    <span class="i">$now_string</span> = <span class="w">strftime</span> <span class="q">&quot;%a %b %e %H:%M:%S %Y&quot;</span><span class="cm">,</span> <a class="l_k" href="../functions/gmtime.html">gmtime</a><span class="sc">;</span></li></ol></pre><p>Note that the <code class="inline"><span class="i">%a</span></code>
 and <code class="inline"><span class="i">%b</span></code>
, the short forms of the day of the week
and the month of the year, may not necessarily be three characters wide.</p>
<p>See <a href="../perlport.html#localtime">"localtime" in perlport</a> for portability concerns.</p>
<p>The <a href="../Time/gmtime.html">Time::gmtime</a> and <a href="../Time/localtime.html">Time::localtime</a> modules provides a convenient,
by-name access mechanism to the gmtime() and localtime() functions,
respectively.</p>
<p>For a comprehensive date and time representation look at the
<a href="http://search.cpan.org/perldoc/DateTime">DateTime</a> module on CPAN.
-----

function localtime
Converts a time as returned by the time function to a 9-element list
with the time analyzed for the local time zone.  Typically used as
follows:</p>
<pre class="verbatim"><ol><li>    <span class="c">#  0    1    2     3     4    5     6     7     8</span></li><li>    <span class="s">(</span><span class="i">$sec</span><span class="cm">,</span><span class="i">$min</span><span class="cm">,</span><span class="i">$hour</span><span class="cm">,</span><span class="i">$mday</span><span class="cm">,</span><span class="i">$mon</span><span class="cm">,</span><span class="i">$year</span><span class="cm">,</span><span class="i">$wday</span><span class="cm">,</span><span class="i">$yday</span><span class="cm">,</span><span class="i">$isdst</span><span class="s">)</span> =</li><li>                                                <a class="l_k" href="../functions/localtime.html">localtime</a><span class="s">(</span><a class="l_k" href="../functions/time.html">time</a><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>All list elements are numeric, and come straight out of the C `struct
tm&#39;.  <code class="inline"><span class="i">$sec</span></code>
, <code class="inline"><span class="i">$min</span></code>
, and <code class="inline"><span class="i">$hour</span></code>
 are the seconds, minutes, and hours
of the specified time.</p>
<p><code class="inline"><span class="i">$mday</span></code>
 is the day of the month, and <code class="inline"><span class="i">$mon</span></code>
 is the month itself, in
the range <code class="inline"><span class="n">0</span>..<span class="n">11</span></code>
 with 0 indicating January and 11 indicating December.
This makes it easy to get a month name from a list:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">@abbr</span> = <span class="q">qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec )</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;$abbr[$mon] $mday&quot;</span><span class="sc">;</span></li><li>    <span class="c"># $mon=9, $mday=18 gives &quot;Oct 18&quot;</span></li></ol></pre><p><code class="inline"><span class="i">$year</span></code>
 is the number of years since 1900, not just the last two digits
of the year.  That is, <code class="inline"><span class="i">$year</span></code>
 is <code class="inline"><span class="n">123</span></code>
 in year 2023.  The proper way
to get a complete 4-digit year is simply:</p>
<pre class="verbatim"><ol><li>    <span class="i">$year</span> += <span class="n">1900</span><span class="sc">;</span></li></ol></pre><p>Otherwise you create non-Y2K-compliant programs--and you wouldn&#39;t want
to do that, would you?</p>
<p>To get the last two digits of the year (e.g., &#39;01&#39; in 2001) do:</p>
<pre class="verbatim"><ol><li>    <span class="i">$year</span> = <a class="l_k" href="../functions/sprintf.html">sprintf</a><span class="s">(</span><span class="q">&quot;%02d&quot;</span><span class="cm">,</span> <span class="i">$year</span> % <span class="n">100</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p><code class="inline"><span class="i">$wday</span></code>
 is the day of the week, with 0 indicating Sunday and 3 indicating
Wednesday.  <code class="inline"><span class="i">$yday</span></code>
 is the day of the year, in the range <code class="inline"><span class="n">0</span>..<span class="n">364</span></code>

(or <code class="inline"><span class="n">0</span>..<span class="n">365</span></code>
 in leap years.)</p>
<p><code class="inline"><span class="i">$isdst</span></code>
 is true if the specified time occurs during Daylight Saving
Time, false otherwise.</p>
<p>If EXPR is omitted, <code class="inline"><a class="l_k" href="../functions/localtime.html">localtime()</a></code> uses the current time (as returned
by time(3)).</p>
<p>In scalar context, <code class="inline"><a class="l_k" href="../functions/localtime.html">localtime()</a></code> returns the ctime(3) value:</p>
<pre class="verbatim"><ol><li>    <span class="i">$now_string</span> = <a class="l_k" href="../functions/localtime.html">localtime</a><span class="sc">;</span>  <span class="c"># e.g., &quot;Thu Oct 13 04:54:34 1994&quot;</span></li></ol></pre><p>This scalar value is <b>not</b> locale dependent but is a Perl builtin. For GMT
instead of local time use the <a href="../functions/gmtime.html">"gmtime"</a> builtin. See also the
<code class="inline"><span class="w">Time::Local</span></code>
 module (to convert the second, minutes, hours, ... back to
the integer value returned by time()), and the <a href="../POSIX.html">POSIX</a> module&#39;s strftime(3)
and mktime(3) functions.</p>
<p>To get somewhat similar but locale dependent date strings, set up your
locale environment variables appropriately (please see <a href="../perllocale.html">perllocale</a>) and
try for example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">POSIX</span> <span class="q">qw(strftime)</span><span class="sc">;</span></li><li>    <span class="i">$now_string</span> = <span class="w">strftime</span> <span class="q">&quot;%a %b %e %H:%M:%S %Y&quot;</span><span class="cm">,</span> <a class="l_k" href="../functions/localtime.html">localtime</a><span class="sc">;</span></li><li>    <span class="c"># or for GMT formatted appropriately for your locale:</span></li><li>    <span class="i">$now_string</span> = <span class="w">strftime</span> <span class="q">&quot;%a %b %e %H:%M:%S %Y&quot;</span><span class="cm">,</span> <a class="l_k" href="../functions/gmtime.html">gmtime</a><span class="sc">;</span></li></ol></pre><p>Note that the <code class="inline"><span class="i">%a</span></code>
 and <code class="inline"><span class="i">%b</span></code>
, the short forms of the day of the week
and the month of the year, may not necessarily be three characters wide.</p>
<p>See <a href="../perlport.html#localtime">"localtime" in perlport</a> for portability concerns.</p>
<p>The <a href="../Time/gmtime.html">Time::gmtime</a> and <a href="../Time/localtime.html">Time::localtime</a> modules provides a convenient,
by-name access mechanism to the gmtime() and localtime() functions,
respectively.</p>
<p>For a comprehensive date and time representation look at the
<a href="http://search.cpan.org/perldoc/DateTime">DateTime</a> module on CPAN.
-----

function lock
thing|
This function places an advisory lock on a shared variable, or referenced
object contained in <i>THING</i> until the lock goes out of scope.</p>
<p>lock() is a &quot;weak keyword&quot; : this means that if you&#39;ve defined a function
by this name (before any calls to it), that function will be called
instead. (However, if you&#39;ve said <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">threads</span></code>
, lock() is always a
keyword.) See <a href="../threads.html">threads</a>.
-----

function log
expr|
Returns the natural logarithm (base <i>e</i>) of EXPR.  If EXPR is omitted,
returns log of <code class="inline"><span class="i">$_</span></code>
.  To get the log of another base, use basic algebra:
The base-N log of a number is equal to the natural log of that number
divided by the natural log of N.  For example:</p>
<pre class="verbatim"><ol><li><a name="log10"></a>    sub <span class="m">log10</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="i">$n</span> = <a class="l_k" href="../functions/shift.html">shift</a><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/return.html">return</a> <a class="l_k" href="../functions/log.html">log</a><span class="s">(</span><span class="i">$n</span><span class="s">)</span>/<a class="l_k" href="../functions/log.html">log</a><span class="s">(</span><span class="n">10</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>See also <a href="../functions/exp.html">"exp"</a> for the inverse operation.
-----

function log
Returns the natural logarithm (base <i>e</i>) of EXPR.  If EXPR is omitted,
returns log of <code class="inline"><span class="i">$_</span></code>
.  To get the log of another base, use basic algebra:
The base-N log of a number is equal to the natural log of that number
divided by the natural log of N.  For example:</p>
<pre class="verbatim"><ol><li><a name="log10"></a>    sub <span class="m">log10</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="i">$n</span> = <a class="l_k" href="../functions/shift.html">shift</a><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/return.html">return</a> <a class="l_k" href="../functions/log.html">log</a><span class="s">(</span><span class="i">$n</span><span class="s">)</span>/<a class="l_k" href="../functions/log.html">log</a><span class="s">(</span><span class="n">10</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>See also <a href="../functions/exp.html">"exp"</a> for the inverse operation.
-----

function lstat
expr|
Does the same thing as the <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code> function (including setting the
special <code class="inline"><span class="w">_</span></code>
 filehandle) but stats a symbolic link instead of the file
the symbolic link points to.  If symbolic links are unimplemented on
your system, a normal <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code> is done.  For much more detailed
information, please see the documentation for <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code>.</p>
<p>If EXPR is omitted, stats <code class="inline"><span class="i">$_</span></code>
.
-----

function lstat
Does the same thing as the <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code> function (including setting the
special <code class="inline"><span class="w">_</span></code>
 filehandle) but stats a symbolic link instead of the file
the symbolic link points to.  If symbolic links are unimplemented on
your system, a normal <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code> is done.  For much more detailed
information, please see the documentation for <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code>.</p>
<p>If EXPR is omitted, stats <code class="inline"><span class="i">$_</span></code>
.
-----

function m
//|
The match operator.  See <a href="../perlop.html#Regexp-Quote-Like-Operators">"Regexp Quote-Like Operators" in perlop</a>.
-----

function map
block list|
Evaluates the BLOCK or EXPR for each element of LIST (locally setting
<code class="inline"><span class="i">$_</span></code>
 to each element) and returns the list value composed of the
results of each such evaluation.  In scalar context, returns the
total number of elements so generated.  Evaluates BLOCK or EXPR in
list context, so each element of LIST may produce zero, one, or
more elements in the returned value.</p>
<pre class="verbatim"><ol><li>    <span class="i">@chars</span> = <a class="l_k" href="../functions/map.html">map</a><span class="s">(</span><a class="l_k" href="../functions/chr.html">chr</a><span class="cm">,</span> <span class="i">@nums</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>translates a list of numbers to the corresponding characters.  And</p>
<pre class="verbatim"><ol><li>    <span class="i">%hash</span> = <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span> <span class="i">get_a_key_for</span><span class="s">(</span><span class="i">$_</span><span class="s">)</span> <span class="cm">=&gt;</span> <span class="i">$_</span> <span class="s">}</span> <span class="i">@array</span><span class="sc">;</span></li></ol></pre><p>is just a funny way to write</p>
<pre class="verbatim"><ol><li>    <span class="i">%hash</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    foreach <span class="s">(</span><span class="i">@array</span><span class="s">)</span> <span class="s">{</span></li><li>	<span class="i">$hash</span>{<span class="i">get_a_key_for</span><span class="s">(</span><span class="i">$_</span><span class="s">)</span>} = <span class="i">$_</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Note that <code class="inline"><span class="i">$_</span></code>
 is an alias to the list value, so it can be used to
modify the elements of the LIST.  While this is useful and supported,
it can cause bizarre results if the elements of LIST are not variables.
Using a regular <code class="inline">foreach</code>
 loop for this purpose would be clearer in
most cases.  See also <a href="../functions/grep.html">"grep"</a> for an array composed of those items of
the original list for which the BLOCK or EXPR evaluates to true.</p>
<p>If <code class="inline"><span class="i">$_</span></code>
 is lexical in the scope where the <code class="inline"><a class="l_k" href="../functions/map.html">map</a></code> appears (because it has
been declared with <code class="inline"><a class="l_k" href="../functions/my.html">my</a> <span class="i">$_</span></code>
), then, in addition to being locally aliased to
the list elements, <code class="inline"><span class="i">$_</span></code>
 keeps being lexical inside the block; that is, it
can&#39;t be seen from the outside, avoiding any potential side-effects.</p>
<p><code class="inline">{</code> starts both hash references and blocks, so <code class="inline">map { ...</code> could be either
the start of map BLOCK LIST or map EXPR, LIST. Because perl doesn&#39;t look
ahead for the closing <code class="inline">}</code> it has to take a guess at which its dealing with
based what it finds just after the <code class="inline">{</code>. Usually it gets it right, but if it
doesn&#39;t it won&#39;t realize something is wrong until it gets to the <code class="inline">}</code> and
encounters the missing (or unexpected) comma. The syntax error will be
reported close to the <code class="inline">}</code> but you&#39;ll need to change something near the <code class="inline">{</code>
such as using a unary <code class="inline">+</code>
 to give perl some help:</p>
<pre class="verbatim"><ol><li>    <span class="i">%hash</span> = <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span>  <span class="q">&quot;\L$_&quot;</span><span class="cm">,</span> <span class="n">1</span>  <span class="s">}</span> <span class="i">@array</span>  <span class="c"># perl guesses EXPR.  wrong</span></li><li>    %<span class="w">hash</span> = <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span> +<span class="q">&quot;\L$_&quot;</span><span class="cm">,</span> <span class="n">1</span>  <span class="s">}</span> <span class="i">@array</span>  <span class="c"># perl guesses BLOCK. right</span></li><li>    %<span class="w">hash</span> = <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span> <span class="s">(</span><span class="q">&quot;\L$_&quot;</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span> <span class="s">}</span> <span class="i">@array</span>  <span class="c"># this also works</span></li><li>    %<span class="w">hash</span> = <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span>  <a class="l_k" href="../functions/lc.html">lc</a><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="cm">,</span> <span class="n">1</span>  <span class="s">}</span> <span class="i">@array</span>  <span class="c"># as does this.</span></li><li>    %<span class="w">hash</span> = <a class="l_k" href="../functions/map.html">map</a> +<span class="s">(</span> <a class="l_k" href="../functions/lc.html">lc</a><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="cm">,</span> <span class="n">1</span> <span class="s">)</span><span class="cm">,</span> <span class="i">@array</span>  <span class="c"># this is EXPR and works!</span></li><li></li><li>    %<span class="w">hash</span> = <a class="l_k" href="../functions/map.html">map</a>  <span class="s">(</span> <a class="l_k" href="../functions/lc.html">lc</a><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="cm">,</span> <span class="n">1</span> <span class="s">)</span><span class="cm">,</span> <span class="i">@array</span>  <span class="c"># evaluates to (1, @array)</span></li></ol></pre><p>or to force an anon hash constructor use <code class="inline">+{</code>:</p>
<pre class="verbatim"><ol><li>   <span class="i">@hashes</span> = <a class="l_k" href="../functions/map.html">map</a> +<span class="s">{</span> <a class="l_k" href="../functions/lc.html">lc</a><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="cm">,</span> <span class="n">1</span> <span class="s">}</span><span class="cm">,</span> <span class="i">@array</span> <span class="c"># EXPR, so needs , at end</span></li></ol></pre><p>and you get list of anonymous hashes each with only 1 entry.
-----

function map
expr|
list|
Evaluates the BLOCK or EXPR for each element of LIST (locally setting
<code class="inline"><span class="i">$_</span></code>
 to each element) and returns the list value composed of the
results of each such evaluation.  In scalar context, returns the
total number of elements so generated.  Evaluates BLOCK or EXPR in
list context, so each element of LIST may produce zero, one, or
more elements in the returned value.</p>
<pre class="verbatim"><ol><li>    <span class="i">@chars</span> = <a class="l_k" href="../functions/map.html">map</a><span class="s">(</span><a class="l_k" href="../functions/chr.html">chr</a><span class="cm">,</span> <span class="i">@nums</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>translates a list of numbers to the corresponding characters.  And</p>
<pre class="verbatim"><ol><li>    <span class="i">%hash</span> = <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span> <span class="i">get_a_key_for</span><span class="s">(</span><span class="i">$_</span><span class="s">)</span> <span class="cm">=&gt;</span> <span class="i">$_</span> <span class="s">}</span> <span class="i">@array</span><span class="sc">;</span></li></ol></pre><p>is just a funny way to write</p>
<pre class="verbatim"><ol><li>    <span class="i">%hash</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    foreach <span class="s">(</span><span class="i">@array</span><span class="s">)</span> <span class="s">{</span></li><li>	<span class="i">$hash</span>{<span class="i">get_a_key_for</span><span class="s">(</span><span class="i">$_</span><span class="s">)</span>} = <span class="i">$_</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Note that <code class="inline"><span class="i">$_</span></code>
 is an alias to the list value, so it can be used to
modify the elements of the LIST.  While this is useful and supported,
it can cause bizarre results if the elements of LIST are not variables.
Using a regular <code class="inline">foreach</code>
 loop for this purpose would be clearer in
most cases.  See also <a href="../functions/grep.html">"grep"</a> for an array composed of those items of
the original list for which the BLOCK or EXPR evaluates to true.</p>
<p>If <code class="inline"><span class="i">$_</span></code>
 is lexical in the scope where the <code class="inline"><a class="l_k" href="../functions/map.html">map</a></code> appears (because it has
been declared with <code class="inline"><a class="l_k" href="../functions/my.html">my</a> <span class="i">$_</span></code>
), then, in addition to being locally aliased to
the list elements, <code class="inline"><span class="i">$_</span></code>
 keeps being lexical inside the block; that is, it
can&#39;t be seen from the outside, avoiding any potential side-effects.</p>
<p><code class="inline">{</code> starts both hash references and blocks, so <code class="inline">map { ...</code> could be either
the start of map BLOCK LIST or map EXPR, LIST. Because perl doesn&#39;t look
ahead for the closing <code class="inline">}</code> it has to take a guess at which its dealing with
based what it finds just after the <code class="inline">{</code>. Usually it gets it right, but if it
doesn&#39;t it won&#39;t realize something is wrong until it gets to the <code class="inline">}</code> and
encounters the missing (or unexpected) comma. The syntax error will be
reported close to the <code class="inline">}</code> but you&#39;ll need to change something near the <code class="inline">{</code>
such as using a unary <code class="inline">+</code>
 to give perl some help:</p>
<pre class="verbatim"><ol><li>    <span class="i">%hash</span> = <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span>  <span class="q">&quot;\L$_&quot;</span><span class="cm">,</span> <span class="n">1</span>  <span class="s">}</span> <span class="i">@array</span>  <span class="c"># perl guesses EXPR.  wrong</span></li><li>    %<span class="w">hash</span> = <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span> +<span class="q">&quot;\L$_&quot;</span><span class="cm">,</span> <span class="n">1</span>  <span class="s">}</span> <span class="i">@array</span>  <span class="c"># perl guesses BLOCK. right</span></li><li>    %<span class="w">hash</span> = <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span> <span class="s">(</span><span class="q">&quot;\L$_&quot;</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span> <span class="s">}</span> <span class="i">@array</span>  <span class="c"># this also works</span></li><li>    %<span class="w">hash</span> = <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span>  <a class="l_k" href="../functions/lc.html">lc</a><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="cm">,</span> <span class="n">1</span>  <span class="s">}</span> <span class="i">@array</span>  <span class="c"># as does this.</span></li><li>    %<span class="w">hash</span> = <a class="l_k" href="../functions/map.html">map</a> +<span class="s">(</span> <a class="l_k" href="../functions/lc.html">lc</a><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="cm">,</span> <span class="n">1</span> <span class="s">)</span><span class="cm">,</span> <span class="i">@array</span>  <span class="c"># this is EXPR and works!</span></li><li></li><li>    %<span class="w">hash</span> = <a class="l_k" href="../functions/map.html">map</a>  <span class="s">(</span> <a class="l_k" href="../functions/lc.html">lc</a><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="cm">,</span> <span class="n">1</span> <span class="s">)</span><span class="cm">,</span> <span class="i">@array</span>  <span class="c"># evaluates to (1, @array)</span></li></ol></pre><p>or to force an anon hash constructor use <code class="inline">+{</code>:</p>
<pre class="verbatim"><ol><li>   <span class="i">@hashes</span> = <a class="l_k" href="../functions/map.html">map</a> +<span class="s">{</span> <a class="l_k" href="../functions/lc.html">lc</a><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="cm">,</span> <span class="n">1</span> <span class="s">}</span><span class="cm">,</span> <span class="i">@array</span> <span class="c"># EXPR, so needs , at end</span></li></ol></pre><p>and you get list of anonymous hashes each with only 1 entry.
-----

function mkdir
filename|
mask|
Creates the directory specified by FILENAME, with permissions
specified by MASK (as modified by <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code>).  If it succeeds it
returns true, otherwise it returns false and sets <code class="inline"><span class="i">$!</span></code>
 (errno).
If omitted, MASK defaults to 0777. If omitted, FILENAME defaults
to <code class="inline"><span class="i">$_</span></code>
.</p>
<p>In general, it is better to create directories with permissive MASK,
and let the user modify that with their <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code>, than it is to supply
a restrictive MASK and give the user no way to be more permissive.
The exceptions to this rule are when the file or directory should be
kept private (mail files, for instance).  The perlfunc(1) entry on
<code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code> discusses the choice of MASK in more detail.</p>
<p>Note that according to the POSIX 1003.1-1996 the FILENAME may have any
number of trailing slashes.  Some operating and filesystems do not get
this right, so Perl automatically removes all trailing slashes to keep
everyone happy.</p>
<p>In order to recursively create a directory structure look at
the <code class="inline"><span class="w">mkpath</span></code>
 function of the <a href="../File/Path.html">File::Path</a> module.
-----

function mkdir
filename|
Creates the directory specified by FILENAME, with permissions
specified by MASK (as modified by <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code>).  If it succeeds it
returns true, otherwise it returns false and sets <code class="inline"><span class="i">$!</span></code>
 (errno).
If omitted, MASK defaults to 0777. If omitted, FILENAME defaults
to <code class="inline"><span class="i">$_</span></code>
.</p>
<p>In general, it is better to create directories with permissive MASK,
and let the user modify that with their <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code>, than it is to supply
a restrictive MASK and give the user no way to be more permissive.
The exceptions to this rule are when the file or directory should be
kept private (mail files, for instance).  The perlfunc(1) entry on
<code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code> discusses the choice of MASK in more detail.</p>
<p>Note that according to the POSIX 1003.1-1996 the FILENAME may have any
number of trailing slashes.  Some operating and filesystems do not get
this right, so Perl automatically removes all trailing slashes to keep
everyone happy.</p>
<p>In order to recursively create a directory structure look at
the <code class="inline"><span class="w">mkpath</span></code>
 function of the <a href="../File/Path.html">File::Path</a> module.
-----

function mkdir
Creates the directory specified by FILENAME, with permissions
specified by MASK (as modified by <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code>).  If it succeeds it
returns true, otherwise it returns false and sets <code class="inline"><span class="i">$!</span></code>
 (errno).
If omitted, MASK defaults to 0777. If omitted, FILENAME defaults
to <code class="inline"><span class="i">$_</span></code>
.</p>
<p>In general, it is better to create directories with permissive MASK,
and let the user modify that with their <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code>, than it is to supply
a restrictive MASK and give the user no way to be more permissive.
The exceptions to this rule are when the file or directory should be
kept private (mail files, for instance).  The perlfunc(1) entry on
<code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code> discusses the choice of MASK in more detail.</p>
<p>Note that according to the POSIX 1003.1-1996 the FILENAME may have any
number of trailing slashes.  Some operating and filesystems do not get
this right, so Perl automatically removes all trailing slashes to keep
everyone happy.</p>
<p>In order to recursively create a directory structure look at
the <code class="inline"><span class="w">mkpath</span></code>
 function of the <a href="../File/Path.html">File::Path</a> module.
-----

function msgctl
id|
cmd|
arg|
Calls the System V IPC function msgctl(2).  You&#39;ll probably have to say</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">IPC::SysV</span><span class="sc">;</span></li></ol></pre><p>first to get the correct constant definitions.  If CMD is <code class="inline"><span class="w">IPC_STAT</span></code>
,
then ARG must be a variable that will hold the returned <code class="inline"><span class="w">msqid_ds</span></code>

structure.  Returns like <code class="inline"><a class="l_k" href="../functions/ioctl.html">ioctl</a></code>: the undefined value for error,
<code class="inline"><span class="q">&quot;0 but true&quot;</span></code>
 for zero, or the actual return value otherwise.  See also
<a href="../perlipc.html#SysV-IPC">"SysV IPC" in perlipc</a>, <code class="inline"><span class="w">IPC::SysV</span></code>
, and <code class="inline"><span class="w">IPC::Semaphore</span></code>
 documentation.
-----

function msgget
key|
flags|
Calls the System V IPC function msgget(2).  Returns the message queue
id, or the undefined value if there is an error.  See also
<a href="../perlipc.html#SysV-IPC">"SysV IPC" in perlipc</a> and <code class="inline"><span class="w">IPC::SysV</span></code>
 and <code class="inline"><span class="w">IPC::Msg</span></code>
 documentation.
-----

function msgrcv
id|
var|
size|
type|
flags|
Calls the System V IPC function msgrcv to receive a message from
message queue ID into variable VAR with a maximum message size of
SIZE.  Note that when a message is received, the message type as a
native long integer will be the first thing in VAR, followed by the
actual message.  This packing may be opened with <code class="inline"><a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;l! a*&quot;</span><span class="s">)</span></code>
.
Taints the variable.  Returns true if successful, or false if there is
an error.  See also <a href="../perlipc.html#SysV-IPC">"SysV IPC" in perlipc</a>, <code class="inline"><span class="w">IPC::SysV</span></code>
, and
<code class="inline"><span class="w">IPC::SysV::Msg</span></code>
 documentation.
-----

function msgsnd
id|
msg|
flags|
Calls the System V IPC function msgsnd to send the message MSG to the
message queue ID.  MSG must begin with the native long integer message
type, and be followed by the length of the actual message, and finally
the message itself.  This kind of packing can be achieved with
<code class="inline"><a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">&quot;l! a*&quot;</span><span class="cm">,</span> <span class="i">$type</span><span class="cm">,</span> <span class="i">$message</span><span class="s">)</span></code>
.  Returns true if successful,
or false if there is an error.  See also <code class="inline"><span class="w">IPC::SysV</span></code>

and <code class="inline"><span class="w">IPC::SysV::Msg</span></code>
 documentation.
-----

function my
expr|
A <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> declares the listed variables to be local (lexically) to the
enclosing block, file, or <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code>.  If more than one value is listed,
the list must be placed in parentheses.</p>
<p>The exact semantics and interface of TYPE and ATTRS are still
evolving.  TYPE is currently bound to the use of <code class="inline"><span class="w">fields</span></code>
 pragma,
and attributes are handled using the <code class="inline"><span class="w">attributes</span></code>
 pragma, or starting
from Perl 5.8.0 also via the <code class="inline"><span class="w">Attribute::Handlers</span></code>
 module.  See
<a href="../perlsub.html#Private-Variables-via-my()">"Private Variables via my()" in perlsub</a> for details, and <a href="../fields.html">fields</a>,
<a href="../attributes.html">attributes</a>, and <a href="../Attribute/Handlers.html">Attribute::Handlers</a>.
-----

function my
type expr|
A <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> declares the listed variables to be local (lexically) to the
enclosing block, file, or <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code>.  If more than one value is listed,
the list must be placed in parentheses.</p>
<p>The exact semantics and interface of TYPE and ATTRS are still
evolving.  TYPE is currently bound to the use of <code class="inline"><span class="w">fields</span></code>
 pragma,
and attributes are handled using the <code class="inline"><span class="w">attributes</span></code>
 pragma, or starting
from Perl 5.8.0 also via the <code class="inline"><span class="w">Attribute::Handlers</span></code>
 module.  See
<a href="../perlsub.html#Private-Variables-via-my()">"Private Variables via my()" in perlsub</a> for details, and <a href="../fields.html">fields</a>,
<a href="../attributes.html">attributes</a>, and <a href="../Attribute/Handlers.html">Attribute::Handlers</a>.
-----

function my
expr : attrs|
A <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> declares the listed variables to be local (lexically) to the
enclosing block, file, or <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code>.  If more than one value is listed,
the list must be placed in parentheses.</p>
<p>The exact semantics and interface of TYPE and ATTRS are still
evolving.  TYPE is currently bound to the use of <code class="inline"><span class="w">fields</span></code>
 pragma,
and attributes are handled using the <code class="inline"><span class="w">attributes</span></code>
 pragma, or starting
from Perl 5.8.0 also via the <code class="inline"><span class="w">Attribute::Handlers</span></code>
 module.  See
<a href="../perlsub.html#Private-Variables-via-my()">"Private Variables via my()" in perlsub</a> for details, and <a href="../fields.html">fields</a>,
<a href="../attributes.html">attributes</a>, and <a href="../Attribute/Handlers.html">Attribute::Handlers</a>.
-----

function my
type expr : attrs|
A <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> declares the listed variables to be local (lexically) to the
enclosing block, file, or <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code>.  If more than one value is listed,
the list must be placed in parentheses.</p>
<p>The exact semantics and interface of TYPE and ATTRS are still
evolving.  TYPE is currently bound to the use of <code class="inline"><span class="w">fields</span></code>
 pragma,
and attributes are handled using the <code class="inline"><span class="w">attributes</span></code>
 pragma, or starting
from Perl 5.8.0 also via the <code class="inline"><span class="w">Attribute::Handlers</span></code>
 module.  See
<a href="../perlsub.html#Private-Variables-via-my()">"Private Variables via my()" in perlsub</a> for details, and <a href="../fields.html">fields</a>,
<a href="../attributes.html">attributes</a>, and <a href="../Attribute/Handlers.html">Attribute::Handlers</a>.
-----

function next
label|
The <code class="inline"><a class="l_k" href="../functions/next.html">next</a></code> command is like the <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code> statement in C; it starts
the next iteration of the loop:</p>
<pre class="verbatim"><ol><li>    <span class="j">LINE:</span> while <span class="s">(</span><span class="q">&lt;STDIN&gt;</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/next.html">next</a> <span class="j">LINE</span> if <span class="q">/^#/</span><span class="sc">;</span>	<span class="c"># discard comments</span></li><li>	<span class="c">#...</span></li><li>    <span class="s">}</span></li></ol></pre><p>Note that if there were a <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code> block on the above, it would get
executed even on discarded lines.  If the LABEL is omitted, the command
refers to the innermost enclosing loop.</p>
<p><code class="inline"><a class="l_k" href="../functions/next.html">next</a></code> cannot be used to exit a block which returns a value such as
<code class="inline"><a class="l_k" href="../functions/eval.html">eval</a> <span class="s">{</span><span class="s">}</span></code>
, <code class="inline"><a class="l_k" href="../functions/sub.html">sub</a> <span class="s">{</span><span class="s">}</span></code>
 or <code class="inline"><a class="l_k" href="../functions/do.html">do</a> <span class="s">{</span><span class="s">}</span></code>
, and should not be used to exit
a grep() or map() operation.</p>
<p>Note that a block by itself is semantically identical to a loop
that executes once.  Thus <code class="inline"><a class="l_k" href="../functions/next.html">next</a></code> will exit such a block early.</p>
<p>See also <a href="../functions/continue.html">"continue"</a> for an illustration of how <code class="inline"><a class="l_k" href="../functions/last.html">last</a></code>, <code class="inline"><a class="l_k" href="../functions/next.html">next</a></code>, and
<code class="inline"><a class="l_k" href="../functions/redo.html">redo</a></code> work.
-----

function next
The <code class="inline"><a class="l_k" href="../functions/next.html">next</a></code> command is like the <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code> statement in C; it starts
the next iteration of the loop:</p>
<pre class="verbatim"><ol><li>    <span class="j">LINE:</span> while <span class="s">(</span><span class="q">&lt;STDIN&gt;</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/next.html">next</a> <span class="j">LINE</span> if <span class="q">/^#/</span><span class="sc">;</span>	<span class="c"># discard comments</span></li><li>	<span class="c">#...</span></li><li>    <span class="s">}</span></li></ol></pre><p>Note that if there were a <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code> block on the above, it would get
executed even on discarded lines.  If the LABEL is omitted, the command
refers to the innermost enclosing loop.</p>
<p><code class="inline"><a class="l_k" href="../functions/next.html">next</a></code> cannot be used to exit a block which returns a value such as
<code class="inline"><a class="l_k" href="../functions/eval.html">eval</a> <span class="s">{</span><span class="s">}</span></code>
, <code class="inline"><a class="l_k" href="../functions/sub.html">sub</a> <span class="s">{</span><span class="s">}</span></code>
 or <code class="inline"><a class="l_k" href="../functions/do.html">do</a> <span class="s">{</span><span class="s">}</span></code>
, and should not be used to exit
a grep() or map() operation.</p>
<p>Note that a block by itself is semantically identical to a loop
that executes once.  Thus <code class="inline"><a class="l_k" href="../functions/next.html">next</a></code> will exit such a block early.</p>
<p>See also <a href="../functions/continue.html">"continue"</a> for an illustration of how <code class="inline"><a class="l_k" href="../functions/last.html">last</a></code>, <code class="inline"><a class="l_k" href="../functions/next.html">next</a></code>, and
<code class="inline"><a class="l_k" href="../functions/redo.html">redo</a></code> work.
-----

function no
module version list|
See the <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code> function, of which <code class="inline"><a class="l_k" href="../functions/no.html">no</a></code> is the opposite.
-----

function no
module version|
See the <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code> function, of which <code class="inline"><a class="l_k" href="../functions/no.html">no</a></code> is the opposite.
-----

function no
module list|
See the <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code> function, of which <code class="inline"><a class="l_k" href="../functions/no.html">no</a></code> is the opposite.
-----

function no
module|
See the <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code> function, of which <code class="inline"><a class="l_k" href="../functions/no.html">no</a></code> is the opposite.
-----

function no
version|
See the <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code> function, of which <code class="inline"><a class="l_k" href="../functions/no.html">no</a></code> is the opposite.
-----

function oct
expr|
Interprets EXPR as an octal string and returns the corresponding
value.  (If EXPR happens to start off with <code class="inline"><span class="n">0</span>x</code>
, interprets it as a
hex string.  If EXPR starts off with <code class="inline">0b</code>, it is interpreted as a
binary string.  Leading whitespace is ignored in all three cases.)
The following will handle decimal, binary, octal, and hex in the standard
Perl or C notation:</p>
<pre class="verbatim"><ol><li>    <span class="i">$val</span> = <a class="l_k" href="../functions/oct.html">oct</a><span class="s">(</span><span class="i">$val</span><span class="s">)</span> if <span class="i">$val</span> =~ <span class="q">/^0/</span><span class="sc">;</span></li></ol></pre><p>If EXPR is omitted, uses <code class="inline"><span class="i">$_</span></code>
.   To go the other way (produce a number
in octal), use sprintf() or printf():</p>
<pre class="verbatim"><ol><li>    <span class="i">$perms</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="q">&quot;filename&quot;</span><span class="s">)</span><span class="s">)</span>[<span class="n">2</span>] &amp; <span class="n">07777</span><span class="sc">;</span></li><li>    <span class="i">$oct_perms</span> = <a class="l_k" href="../functions/sprintf.html">sprintf</a> <span class="q">&quot;%lo&quot;</span><span class="cm">,</span> <span class="i">$perms</span><span class="sc">;</span></li></ol></pre><p>The oct() function is commonly used when a string such as <code class="inline"><span class="n">644</span></code>
 needs
to be converted into a file mode, for example. (Although perl will
automatically convert strings into numbers as needed, this automatic
conversion assumes base 10.)
-----

function oct
Interprets EXPR as an octal string and returns the corresponding
value.  (If EXPR happens to start off with <code class="inline"><span class="n">0</span>x</code>
, interprets it as a
hex string.  If EXPR starts off with <code class="inline">0b</code>, it is interpreted as a
binary string.  Leading whitespace is ignored in all three cases.)
The following will handle decimal, binary, octal, and hex in the standard
Perl or C notation:</p>
<pre class="verbatim"><ol><li>    <span class="i">$val</span> = <a class="l_k" href="../functions/oct.html">oct</a><span class="s">(</span><span class="i">$val</span><span class="s">)</span> if <span class="i">$val</span> =~ <span class="q">/^0/</span><span class="sc">;</span></li></ol></pre><p>If EXPR is omitted, uses <code class="inline"><span class="i">$_</span></code>
.   To go the other way (produce a number
in octal), use sprintf() or printf():</p>
<pre class="verbatim"><ol><li>    <span class="i">$perms</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="q">&quot;filename&quot;</span><span class="s">)</span><span class="s">)</span>[<span class="n">2</span>] &amp; <span class="n">07777</span><span class="sc">;</span></li><li>    <span class="i">$oct_perms</span> = <a class="l_k" href="../functions/sprintf.html">sprintf</a> <span class="q">&quot;%lo&quot;</span><span class="cm">,</span> <span class="i">$perms</span><span class="sc">;</span></li></ol></pre><p>The oct() function is commonly used when a string such as <code class="inline"><span class="n">644</span></code>
 needs
to be converted into a file mode, for example. (Although perl will
automatically convert strings into numbers as needed, this automatic
conversion assumes base 10.)
-----

function open
filehandle|
expr|
Opens the file whose filename is given by EXPR, and associates it with
FILEHANDLE.</p>
<p>Simple examples to open a file for reading:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">'&lt;'</span><span class="cm">,</span> <span class="q">&quot;input.txt&quot;</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="i">$!</span><span class="sc">;</span></li></ol></pre><p>and for writing:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">'&gt;'</span><span class="cm">,</span> <span class="q">&quot;output.txt&quot;</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="i">$!</span><span class="sc">;</span></li></ol></pre><p>(The following is a comprehensive reference to open(): for a gentler
introduction you may consider <a href="../perlopentut.html">perlopentut</a>.)</p>
<p>If FILEHANDLE is an undefined scalar variable (or array or hash element)
the variable is assigned a reference to a new anonymous filehandle,
otherwise if FILEHANDLE is an expression, its value is used as the name of
the real filehandle wanted.  (This is considered a symbolic reference, so
<code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">strict</span> <span class="q">&#39;refs&#39;</span></code>
 should <i>not</i> be in effect.)</p>
<p>If EXPR is omitted, the scalar variable of the same name as the
FILEHANDLE contains the filename.  (Note that lexical variables--those
declared with <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>--will not work for this purpose; so if you&#39;re
using <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>, specify EXPR in your call to open.)</p>
<p>If three or more arguments are specified then the mode of opening and
the file name are separate. If MODE is <code class="inline"><span class="q">&#39;&lt;&#39;</span></code>
 or nothing, the file
is opened for input.  If MODE is <code class="inline"><span class="q">&#39;&gt;&#39;</span></code>
, the file is truncated and
opened for output, being created if necessary.  If MODE is <code class="inline"><span class="q">&#39;&gt;&gt;&#39;</span></code>
,
the file is opened for appending, again being created if necessary.</p>
<p>You can put a <code class="inline"><span class="q">&#39;+&#39;</span></code>
 in front of the <code class="inline"><span class="q">&#39;&gt;&#39;</span></code>
 or <code class="inline"><span class="q">&#39;&lt;&#39;</span></code>
 to
indicate that you want both read and write access to the file; thus
<code class="inline"><span class="q">&#39;+&lt;&#39;</span></code>
 is almost always preferred for read/write updates--the <code class="inline"><span class="q">&#39;+&gt;&#39;</span></code>
 mode would clobber the file first.  You can&#39;t usually use
either read-write mode for updating textfiles, since they have
variable length records.  See the <b>-i</b> switch in <a href="../perlrun.html">perlrun</a> for a
better approach.  The file is created with permissions of <code class="inline"><span class="n">0666</span></code>

modified by the process&#39; <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code> value.</p>
<p>These various prefixes correspond to the fopen(3) modes of <code class="inline"><span class="q">&#39;r&#39;</span></code>
,
<code class="inline"><span class="q">&#39;r+&#39;</span></code>
, <code class="inline"><span class="q">&#39;w&#39;</span></code>
, <code class="inline"><span class="q">&#39;w+&#39;</span></code>
, <code class="inline"><span class="q">&#39;a&#39;</span></code>
, and <code class="inline"><span class="q">&#39;a+&#39;</span></code>
.</p>
<p>In the 2-arguments (and 1-argument) form of the call the mode and
filename should be concatenated (in this order), possibly separated by
spaces.  It is possible to omit the mode in these forms if the mode is
<code class="inline"><span class="q">&#39;&lt;&#39;</span></code>
.</p>
<p>If the filename begins with <code class="inline"><span class="q">&#39;|&#39;</span></code>
, the filename is interpreted as a
command to which output is to be piped, and if the filename ends with a
<code class="inline"><span class="q">&#39;|&#39;</span></code>
, the filename is interpreted as a command which pipes output to
us.  See <a href="../perlipc.html#Using-open()-for-IPC">"Using open() for IPC" in perlipc</a>
for more examples of this.  (You are not allowed to <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> to a command
that pipes both in <i>and</i> out, but see <a href="../IPC/Open2.html">IPC::Open2</a>, <a href="../IPC/Open3.html">IPC::Open3</a>,
and <a href="../perlipc.html#Bidirectional-Communication-with-Another-Process">"Bidirectional Communication with Another Process" in perlipc</a>
for alternatives.)</p>
<p>For three or more arguments if MODE is <code class="inline"><span class="q">&#39;|-&#39;</span></code>
, the filename is
interpreted as a command to which output is to be piped, and if MODE
is <code class="inline"><span class="q">&#39;-|&#39;</span></code>
, the filename is interpreted as a command which pipes
output to us.  In the 2-arguments (and 1-argument) form one should
replace dash (<code class="inline"><span class="q">&#39;-&#39;</span></code>
) with the command.
See <a href="../perlipc.html#Using-open()-for-IPC">"Using open() for IPC" in perlipc</a> for more examples of this.
(You are not allowed to <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> to a command that pipes both in <i>and</i>
out, but see <a href="../IPC/Open2.html">IPC::Open2</a>, <a href="../IPC/Open3.html">IPC::Open3</a>, and
<a href="../perlipc.html#Bidirectional-Communication">"Bidirectional Communication" in perlipc</a> for alternatives.)</p>
<p>In the three-or-more argument form of pipe opens, if LIST is specified
(extra arguments after the command name) then LIST becomes arguments
to the command invoked if the platform supports it.  The meaning of
<code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> with more than three arguments for non-pipe modes is not yet
specified. Experimental &quot;layers&quot; may give extra LIST arguments
meaning.</p>
<p>In the 2-arguments (and 1-argument) form opening <code class="inline"><span class="q">&#39;-&#39;</span></code>
 opens STDIN
and opening <code class="inline"><span class="q">&#39;&gt;-&#39;</span></code>
 opens STDOUT.</p>
<p>You may use the three-argument form of open to specify IO &quot;layers&quot;
(sometimes also referred to as &quot;disciplines&quot;) to be applied to the handle
that affect how the input and output are processed (see <a href="../open.html">open</a> and
<a href="../PerlIO.html">PerlIO</a> for more details). For example</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">&quot;&lt;:encoding(UTF-8)&quot;</span><span class="cm">,</span> <span class="q">&quot;file&quot;</span><span class="s">)</span></li></ol></pre><p>will open the UTF-8 encoded file containing Unicode characters,
see <a href="../perluniintro.html">perluniintro</a>. Note that if layers are specified in the
three-arg form then default layers stored in ${^OPEN} (see <a href="../perlvar.html">perlvar</a>;
usually set by the <b>open</b> pragma or the switch <b>-CioD</b>) are ignored.</p>
<p>Open returns nonzero upon success, the undefined value otherwise.  If
the <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> involved a pipe, the return value happens to be the pid of
the subprocess.</p>
<p>If you&#39;re running Perl on a system that distinguishes between text
files and binary files, then you should check out <a href="../functions/binmode.html">"binmode"</a> for tips
for dealing with this.  The key distinction between systems that need
<code class="inline"><a class="l_k" href="../functions/binmode.html">binmode</a></code> and those that don&#39;t is their text file formats.  Systems
like Unix, Mac OS, and Plan 9, which delimit lines with a single
character, and which encode that character in C as <code class="inline"><span class="q">&quot;\n&quot;</span></code>
, do not
need <code class="inline"><a class="l_k" href="../functions/binmode.html">binmode</a></code>.  The rest need it.</p>
<p>When opening a file, it&#39;s usually a bad idea to continue normal execution
if the request failed, so <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> is frequently used in connection with
<code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>.  Even if <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code> won&#39;t do what you want (say, in a CGI script,
where you want to make a nicely formatted error message (but there are
modules that can help with that problem)) you should always check
the return value from opening a file.  The infrequent exception is when
working with an unopened filehandle is actually what you want to do.</p>
<p>As a special case the 3-arg form with a read/write mode and the third
argument being <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$tmp</span><span class="cm">,</span> <span class="q">&quot;+&gt;&quot;</span><span class="cm">,</span> <a class="l_k" href="../functions/undef.html">undef</a><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> ...</li></ol></pre><p>opens a filehandle to an anonymous temporary file.  Also using &quot;+&lt;&quot;
works for symmetry, but you really should consider writing something
to the temporary file first.  You will need to seek() to do the
reading.</p>
<p>Since v5.8.0, perl has built using PerlIO by default.  Unless you&#39;ve
changed this (i.e. Configure -Uuseperlio), you can open file handles to
&quot;in memory&quot; files held in Perl scalars via:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="i">$fh</span><span class="cm">,</span> <span class="q">&#39;&gt;&#39;</span><span class="cm">,</span> \<span class="i">$variable</span><span class="s">)</span> || ..</li></ol></pre><p>Though if you try to re-open <code class="inline"><span class="w">STDOUT</span></code>
 or <code class="inline"><span class="w">STDERR</span></code>
 as an &quot;in memory&quot;
file, you have to close it first:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/close.html">close</a> <span class="w">STDOUT</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&#39;&gt;&#39;</span><span class="cm">,</span> \<span class="i">$variable</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t open STDOUT: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>Examples:</p>
<pre class="verbatim"><ol><li>    $ARTICLE = 100;</li><li>    open ARTICLE or die "Can't find article $ARTICLE: $!\n";</li><li>    while (&lt;ARTICLE&gt;) {...</li><li></li><li>    open(LOG, '&gt;&gt;/usr/spool/news/twitlog');	# (log is reserved)</li><li>    # if the open fails, output is discarded</li><li></li><li>    open(my $dbase, '+&lt;', 'dbase.mine')		# open for update</li><li>	or die "Can't open 'dbase.mine' for update: $!";</li><li></li><li>    open(my $dbase, '+&lt;dbase.mine')			# ditto</li><li>	or die "Can't open 'dbase.mine' for update: $!";</li><li></li><li>    open(ARTICLE, '-|', "caesar &lt;$article")     # decrypt article</li><li>	or die "Can't start caesar: $!";</li><li></li><li>    open(ARTICLE, "caesar &lt;$article |")		# ditto</li><li>	or die "Can't start caesar: $!";</li><li></li><li>    open(EXTRACT, "|sort &gt;Tmp$$")		# $$ is our process id</li><li>	or die "Can't start sort: $!";</li><li></li><li>    # in memory files</li><li>    open(MEMORY,'&gt;', \$var)</li><li>	or die "Can't open memory file: $!";</li><li>    print MEMORY "foo!\n";			# output will end up in $var</li><li></li><li>    # process argument list of files along with any includes</li><li></li><li>    foreach $file (@ARGV) {</li><li>	process($file, 'fh00');</li><li>    }</li><li></li><li>    sub process {</li><li>	my($filename, $input) = @_;</li><li>	$input++;		# this is a string increment</li><li>	unless (open($input, $filename)) {</li><li>	    print STDERR "Can't open $filename: $!\n";</li><li>	    return;</li><li>	}</li><li></li><li>	local $_;</li><li>	while (&lt;$input&gt;) {		# note use of indirection</li><li>	    if (/^#include "(.*)"/) {</li><li>		process($1, $input);</li><li>		next;</li><li>	    }</li><li>	    #...		# whatever</li><li>	}</li><li>    }</li></ol></pre><p>See <a href="../perliol.html">perliol</a> for detailed info on PerlIO.</p>
<p>You may also, in the Bourne shell tradition, specify an EXPR beginning
with <code class="inline"><span class="q">&#39;&gt;&amp;&#39;</span></code>
, in which case the rest of the string is interpreted
as the name of a filehandle (or file descriptor, if numeric) to be
duped (as <code class="inline"><span class="i">dup</span><span class="s">(</span><span class="n">2</span><span class="s">)</span></code>
) and opened.  You may use <code class="inline"><span class="i">&amp;</span></code>
 after <code class="inline">&gt;</code>,
<code class="inline">&gt;&gt;</code>
, <code class="inline">&lt;</code>
, <code class="inline">+&gt;</code>, <code class="inline">+&gt;&gt;</code>
, and <code class="inline">+&lt;</code>
.
The mode you specify should match the mode of the original filehandle.
(Duping a filehandle does not take into account any existing contents
of IO buffers.) If you use the 3-arg form then you can pass either a
number, the name of a filehandle or the normal &quot;reference to a glob&quot;.</p>
<p>Here is a script that saves, redirects, and restores <code class="inline"><span class="w">STDOUT</span></code>
 and
<code class="inline"><span class="w">STDERR</span></code>
 using various methods:</p>
<pre class="verbatim"><ol><li>    <span class="c">#!/usr/bin/perl</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <a class="l_k" href="../functions/my.html">my</a> <span class="i">$oldout</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;STDOUT&quot;</span>     or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup STDOUT: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">OLDERR</span><span class="cm">,</span>     <span class="q">&quot;&gt;&amp;&quot;</span><span class="cm">,</span> \<span class="i">*STDERR</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup STDERR: $!&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&#39;&gt;&#39;</span><span class="cm">,</span> <span class="q">&quot;foo.out&quot;</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t redirect STDOUT: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDERR</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;STDOUT&quot;</span>     or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup STDOUT: $!&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/select.html">select</a> <span class="w">STDERR</span><span class="sc">;</span> <span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span>	<span class="c"># make unbuffered</span></li><li>    <a class="l_k" href="../functions/select.html">select</a> <span class="w">STDOUT</span><span class="sc">;</span> <span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span>	<span class="c"># make unbuffered</span></li><li></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDOUT</span> <span class="q">&quot;stdout 1\n&quot;</span><span class="sc">;</span>	<span class="c"># this works for</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;stderr 1\n&quot;</span><span class="sc">;</span> 	<span class="c"># subprocesses too</span></li><li></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;&quot;</span><span class="cm">,</span> <span class="i">$oldout</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup \$oldout: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDERR</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;OLDERR&quot;</span>    or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup OLDERR: $!&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDOUT</span> <span class="q">&quot;stdout 2\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;stderr 2\n&quot;</span><span class="sc">;</span></li></ol></pre><p>If you specify <code class="inline"><span class="q">&#39;&lt;&amp;=X&#39;</span></code>
, where <code class="inline"><span class="w">X</span></code>
 is a file descriptor number
or a filehandle, then Perl will do an equivalent of C&#39;s <code class="inline"><span class="w">fdopen</span></code>
 of
that file descriptor (and not call <code class="inline"><span class="i">dup</span><span class="s">(</span><span class="n">2</span><span class="s">)</span></code>
); this is more
parsimonious of file descriptors.  For example:</p>
<pre class="verbatim"><ol><li>    <span class="c"># open for input, reusing the fileno of $fd</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FILEHANDLE</span><span class="cm">,</span> <span class="q">&quot;&lt;&amp;=$fd&quot;</span><span class="s">)</span></li></ol></pre><p>or</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FILEHANDLE</span><span class="cm">,</span> <span class="q">&quot;&lt;&amp;=&quot;</span><span class="cm">,</span> <span class="i">$fd</span><span class="s">)</span></li></ol></pre><p>or</p>
<pre class="verbatim"><ol><li>    <span class="c"># open for append, using the fileno of OLDFH</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FH</span><span class="cm">,</span> <span class="q">&quot;&gt;&gt;&amp;=&quot;</span><span class="cm">,</span> <span class="w">OLDFH</span><span class="s">)</span></li></ol></pre><p>or</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FH</span><span class="cm">,</span> <span class="q">&quot;&gt;&gt;&amp;=OLDFH&quot;</span><span class="s">)</span></li></ol></pre><p>Being parsimonious on filehandles is also useful (besides being
parsimonious) for example when something is dependent on file
descriptors, like for example locking using flock().  If you do just
<code class="inline"><a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">A</span><span class="cm">,</span> <span class="q">&#39;&gt;&gt;&amp;B&#39;</span><span class="s">)</span></code>
, the filehandle A will not have the same file
descriptor as B, and therefore flock(A) will not flock(B), and vice
versa.  But with <code class="inline"><a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">A</span><span class="cm">,</span> <span class="q">&#39;&gt;&gt;&amp;=B&#39;</span><span class="s">)</span></code>
 the filehandles will share
the same file descriptor.</p>
<p>Note that if you are using Perls older than 5.8.0, Perl will be using
the standard C libraries&#39; fdopen() to implement the &quot;=&quot; functionality.
On many UNIX systems fdopen() fails when file descriptors exceed a
certain value, typically 255.  For Perls 5.8.0 and later, PerlIO is
most often the default.</p>
<p>You can see whether Perl has been compiled with PerlIO or not by
running <code class="inline"><span class="w">perl</span> -<span class="w">V</span></code>
 and looking for <code class="inline"><span class="w">useperlio</span>=</code>
 line.  If <code class="inline"><span class="w">useperlio</span></code>

is <code class="inline"><span class="w">define</span></code>
, you have PerlIO, otherwise you don&#39;t.</p>
<p>If you open a pipe on the command <code class="inline"><span class="q">&#39;-&#39;</span></code>
, i.e., either <code class="inline"><span class="q">&#39;|-&#39;</span></code>
 or <code class="inline"><span class="q">&#39;-|&#39;</span></code>

with 2-arguments (or 1-argument) form of open(), then
there is an implicit fork done, and the return value of open is the pid
of the child within the parent process, and <code class="inline"><span class="n">0</span></code>
 within the child
process.  (Use <code class="inline"><a class="l_k" href="../functions/defined.html">defined($pid)</a></code> to determine whether the open was successful.)
The filehandle behaves normally for the parent, but i/o to that
filehandle is piped from/to the STDOUT/STDIN of the child process.
In the child process the filehandle isn&#39;t opened--i/o happens from/to
the new STDOUT or STDIN.  Typically this is used like the normal
piped open when you want to exercise more control over just how the
pipe command gets executed, such as when you are running setuid, and
don&#39;t want to have to scan shell commands for metacharacters.
The following triples are more or less equivalent:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&quot;|tr &#39;[a-z]&#39; &#39;[A-Z]&#39;&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;|-&#39;</span><span class="cm">,</span> <span class="q">&quot;tr &#39;[a-z]&#39; &#39;[A-Z]&#39;&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;|-&#39;</span><span class="s">)</span> || <a class="l_k" href="../functions/exec.html">exec</a> <span class="q">&#39;tr&#39;</span><span class="cm">,</span> <span class="q">&#39;[a-z]&#39;</span><span class="cm">,</span> <span class="q">&#39;[A-Z]&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;|-&#39;</span><span class="cm">,</span> <span class="q">&quot;tr&quot;</span><span class="cm">,</span> <span class="q">&#39;[a-z]&#39;</span><span class="cm">,</span> <span class="q">&#39;[A-Z]&#39;</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&quot;cat -n &#39;$file&#39;|&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;-|&#39;</span><span class="cm">,</span> <span class="q">&quot;cat -n &#39;$file&#39;&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;-|&#39;</span><span class="s">)</span> || <a class="l_k" href="../functions/exec.html">exec</a> <span class="q">&#39;cat&#39;</span><span class="cm">,</span> <span class="q">&#39;-n&#39;</span><span class="cm">,</span> <span class="i">$file</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;-|&#39;</span><span class="cm">,</span> <span class="q">&quot;cat&quot;</span><span class="cm">,</span> <span class="q">&#39;-n&#39;</span><span class="cm">,</span> <span class="i">$file</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The last example in each block shows the pipe as &quot;list form&quot;, which is
not yet supported on all platforms.  A good rule of thumb is that if
your platform has true <code class="inline"><a class="l_k" href="../functions/fork.html">fork()</a></code> (in other words, if your platform is
UNIX) you can use the list form.</p>
<p>See <a href="../perlipc.html#Safe-Pipe-Opens">"Safe Pipe Opens" in perlipc</a> for more examples of this.</p>
<p>Beginning with v5.6.0, Perl will attempt to flush all files opened for
output before any operation that may do a fork, but this may not be
supported on some platforms (see <a href="../perlport.html">perlport</a>).  To be safe, you may need
to set <code class="inline"><span class="i">$|</span></code>
 ($AUTOFLUSH in English) or call the <code class="inline"><span class="i">autoflush</span><span class="s">(</span><span class="s">)</span></code>
 method
of <code class="inline"><span class="w">IO::Handle</span></code>
 on any open handles.</p>
<p>On systems that support a close-on-exec flag on files, the flag will
be set for the newly opened file descriptor as determined by the value
of $^F.  See <a href="../perlvar.html#%24%5eF">"$^F" in perlvar</a>.</p>
<p>Closing any piped filehandle causes the parent process to wait for the
child to finish, and returns the status value in <code class="inline"><span class="i">$?</span></code>
 and
<code class="inline"><span class="i">$</span>{<span class="w">^CHILD_ERROR_NATIVE</span>}</code>
.</p>
<p>The filename passed to 2-argument (or 1-argument) form of open() will
have leading and trailing whitespace deleted, and the normal
redirection characters honored.  This property, known as &quot;magic open&quot;,
can often be used to good effect.  A user could specify a filename of
<i>&quot;rsh cat file |&quot;</i>, or you could change certain filenames as needed:</p>
<pre class="verbatim"><ol><li>    <span class="i">$filename</span> =~ <span class="q">s/(.*\.gz)\s*$/gzip -dc &lt; $1|/</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FH</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t open $filename: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>Use 3-argument form to open a file with arbitrary weird characters in it,</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;&lt;&#39;</span><span class="cm">,</span> <span class="i">$file</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>otherwise it&#39;s necessary to protect any leading and trailing whitespace:</p>
<pre class="verbatim"><ol><li>    <span class="i">$file</span> =~ <span class="q">s#^(\s)#./$1#</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&quot;&lt; $file\0&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>(this may not work on some bizarre filesystems).  One should
conscientiously choose between the <i>magic</i> and 3-arguments form
of open():</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">IN</span><span class="cm">,</span> <span class="i">$ARGV</span>[<span class="n">0</span>]<span class="sc">;</span></li></ol></pre><p>will allow the user to specify an argument of the form <code class="inline"><span class="q">&quot;rsh cat file |&quot;</span></code>
,
but will not work on a filename which happens to have a trailing space, while</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">IN</span><span class="cm">,</span> <span class="q">&#39;&lt;&#39;</span><span class="cm">,</span> <span class="i">$ARGV</span>[<span class="n">0</span>]<span class="sc">;</span></li></ol></pre><p>will have exactly the opposite restrictions.</p>
<p>If you want a &quot;real&quot; C <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> (see <code class="inline"><a class="l_k" href="../functions/open.html">open(2)</a></code> on your system), then you
should use the <code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code> function, which involves no such magic (but
may use subtly different filemodes than Perl open(), which is mapped
to C fopen()).  This is
another way to protect your filenames from interpretation.  For example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">IO::Handle</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/sysopen.html">sysopen</a><span class="s">(</span><span class="w">HANDLE</span><span class="cm">,</span> <span class="i">$path</span><span class="cm">,</span> <span class="w">O_RDWR</span>|<span class="w">O_CREAT</span>|<span class="w">O_EXCL</span><span class="s">)</span></li><li>	or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;sysopen $path: $!&quot;</span><span class="sc">;</span></li><li>    <span class="i">$oldfh</span> = <a class="l_k" href="../functions/select.html">select</a><span class="s">(</span><span class="w">HANDLE</span><span class="s">)</span><span class="sc">;</span> <span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span> <a class="l_k" href="../functions/select.html">select</a><span class="s">(</span><span class="i">$oldfh</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">HANDLE</span> <span class="q">&quot;stuff $$\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/seek.html">seek</a><span class="s">(</span><span class="w">HANDLE</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;File contains: &quot;</span><span class="cm">,</span> <span class="q">&lt;HANDLE&gt;</span><span class="sc">;</span></li></ol></pre><p>Using the constructor from the <code class="inline"><span class="w">IO::Handle</span></code>
 package (or one of its
subclasses, such as <code class="inline"><span class="w">IO::File</span></code>
 or <code class="inline"><span class="w">IO::Socket</span></code>
), you can generate anonymous
filehandles that have the scope of whatever variables hold references to
them, and automatically close whenever and however you leave that scope:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">IO::File</span><span class="sc">;</span></li><li>    <span class="c">#...</span></li><li><a name="read_myfile_munged"></a>    sub <span class="m">read_myfile_munged</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="i">$ALL</span> = <a class="l_k" href="../functions/shift.html">shift</a><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="i">$handle</span> = <span class="w">IO::File</span><span class="w">-&gt;new</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="i">$handle</span><span class="cm">,</span> <span class="q">&quot;myfile&quot;</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;myfile: $!&quot;</span><span class="sc">;</span></li><li>	<span class="i">$first</span> = <span class="q">&lt;$handle&gt;</span></li><li>	    or <a class="l_k" href="../functions/return.html">return</a> <span class="s">(</span><span class="s">)</span><span class="sc">;</span>     <span class="c"># Automatically closed here.</span></li><li>	<span class="w">mung</span> <span class="i">$first</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;mung failed&quot;</span><span class="sc">;</span>	<span class="c"># Or here.</span></li><li>	<a class="l_k" href="../functions/return.html">return</a> <span class="i">$first</span><span class="cm">,</span> <span class="q">&lt;$handle&gt;</span> if <span class="i">$ALL</span><span class="sc">;</span>	<span class="c"># Or here.</span></li><li>	<span class="i">$first</span><span class="sc">;</span>					<span class="c"># Or here.</span></li><li>    <span class="s">}</span></li></ol></pre><p>See <a href="../functions/seek.html">"seek"</a> for some details about mixing reading and writing.
-----

function open
filehandle|
mode|
expr|
Opens the file whose filename is given by EXPR, and associates it with
FILEHANDLE.</p>
<p>Simple examples to open a file for reading:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">'&lt;'</span><span class="cm">,</span> <span class="q">&quot;input.txt&quot;</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="i">$!</span><span class="sc">;</span></li></ol></pre><p>and for writing:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">'&gt;'</span><span class="cm">,</span> <span class="q">&quot;output.txt&quot;</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="i">$!</span><span class="sc">;</span></li></ol></pre><p>(The following is a comprehensive reference to open(): for a gentler
introduction you may consider <a href="../perlopentut.html">perlopentut</a>.)</p>
<p>If FILEHANDLE is an undefined scalar variable (or array or hash element)
the variable is assigned a reference to a new anonymous filehandle,
otherwise if FILEHANDLE is an expression, its value is used as the name of
the real filehandle wanted.  (This is considered a symbolic reference, so
<code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">strict</span> <span class="q">&#39;refs&#39;</span></code>
 should <i>not</i> be in effect.)</p>
<p>If EXPR is omitted, the scalar variable of the same name as the
FILEHANDLE contains the filename.  (Note that lexical variables--those
declared with <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>--will not work for this purpose; so if you&#39;re
using <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>, specify EXPR in your call to open.)</p>
<p>If three or more arguments are specified then the mode of opening and
the file name are separate. If MODE is <code class="inline"><span class="q">&#39;&lt;&#39;</span></code>
 or nothing, the file
is opened for input.  If MODE is <code class="inline"><span class="q">&#39;&gt;&#39;</span></code>
, the file is truncated and
opened for output, being created if necessary.  If MODE is <code class="inline"><span class="q">&#39;&gt;&gt;&#39;</span></code>
,
the file is opened for appending, again being created if necessary.</p>
<p>You can put a <code class="inline"><span class="q">&#39;+&#39;</span></code>
 in front of the <code class="inline"><span class="q">&#39;&gt;&#39;</span></code>
 or <code class="inline"><span class="q">&#39;&lt;&#39;</span></code>
 to
indicate that you want both read and write access to the file; thus
<code class="inline"><span class="q">&#39;+&lt;&#39;</span></code>
 is almost always preferred for read/write updates--the <code class="inline"><span class="q">&#39;+&gt;&#39;</span></code>
 mode would clobber the file first.  You can&#39;t usually use
either read-write mode for updating textfiles, since they have
variable length records.  See the <b>-i</b> switch in <a href="../perlrun.html">perlrun</a> for a
better approach.  The file is created with permissions of <code class="inline"><span class="n">0666</span></code>

modified by the process&#39; <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code> value.</p>
<p>These various prefixes correspond to the fopen(3) modes of <code class="inline"><span class="q">&#39;r&#39;</span></code>
,
<code class="inline"><span class="q">&#39;r+&#39;</span></code>
, <code class="inline"><span class="q">&#39;w&#39;</span></code>
, <code class="inline"><span class="q">&#39;w+&#39;</span></code>
, <code class="inline"><span class="q">&#39;a&#39;</span></code>
, and <code class="inline"><span class="q">&#39;a+&#39;</span></code>
.</p>
<p>In the 2-arguments (and 1-argument) form of the call the mode and
filename should be concatenated (in this order), possibly separated by
spaces.  It is possible to omit the mode in these forms if the mode is
<code class="inline"><span class="q">&#39;&lt;&#39;</span></code>
.</p>
<p>If the filename begins with <code class="inline"><span class="q">&#39;|&#39;</span></code>
, the filename is interpreted as a
command to which output is to be piped, and if the filename ends with a
<code class="inline"><span class="q">&#39;|&#39;</span></code>
, the filename is interpreted as a command which pipes output to
us.  See <a href="../perlipc.html#Using-open()-for-IPC">"Using open() for IPC" in perlipc</a>
for more examples of this.  (You are not allowed to <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> to a command
that pipes both in <i>and</i> out, but see <a href="../IPC/Open2.html">IPC::Open2</a>, <a href="../IPC/Open3.html">IPC::Open3</a>,
and <a href="../perlipc.html#Bidirectional-Communication-with-Another-Process">"Bidirectional Communication with Another Process" in perlipc</a>
for alternatives.)</p>
<p>For three or more arguments if MODE is <code class="inline"><span class="q">&#39;|-&#39;</span></code>
, the filename is
interpreted as a command to which output is to be piped, and if MODE
is <code class="inline"><span class="q">&#39;-|&#39;</span></code>
, the filename is interpreted as a command which pipes
output to us.  In the 2-arguments (and 1-argument) form one should
replace dash (<code class="inline"><span class="q">&#39;-&#39;</span></code>
) with the command.
See <a href="../perlipc.html#Using-open()-for-IPC">"Using open() for IPC" in perlipc</a> for more examples of this.
(You are not allowed to <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> to a command that pipes both in <i>and</i>
out, but see <a href="../IPC/Open2.html">IPC::Open2</a>, <a href="../IPC/Open3.html">IPC::Open3</a>, and
<a href="../perlipc.html#Bidirectional-Communication">"Bidirectional Communication" in perlipc</a> for alternatives.)</p>
<p>In the three-or-more argument form of pipe opens, if LIST is specified
(extra arguments after the command name) then LIST becomes arguments
to the command invoked if the platform supports it.  The meaning of
<code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> with more than three arguments for non-pipe modes is not yet
specified. Experimental &quot;layers&quot; may give extra LIST arguments
meaning.</p>
<p>In the 2-arguments (and 1-argument) form opening <code class="inline"><span class="q">&#39;-&#39;</span></code>
 opens STDIN
and opening <code class="inline"><span class="q">&#39;&gt;-&#39;</span></code>
 opens STDOUT.</p>
<p>You may use the three-argument form of open to specify IO &quot;layers&quot;
(sometimes also referred to as &quot;disciplines&quot;) to be applied to the handle
that affect how the input and output are processed (see <a href="../open.html">open</a> and
<a href="../PerlIO.html">PerlIO</a> for more details). For example</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">&quot;&lt;:encoding(UTF-8)&quot;</span><span class="cm">,</span> <span class="q">&quot;file&quot;</span><span class="s">)</span></li></ol></pre><p>will open the UTF-8 encoded file containing Unicode characters,
see <a href="../perluniintro.html">perluniintro</a>. Note that if layers are specified in the
three-arg form then default layers stored in ${^OPEN} (see <a href="../perlvar.html">perlvar</a>;
usually set by the <b>open</b> pragma or the switch <b>-CioD</b>) are ignored.</p>
<p>Open returns nonzero upon success, the undefined value otherwise.  If
the <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> involved a pipe, the return value happens to be the pid of
the subprocess.</p>
<p>If you&#39;re running Perl on a system that distinguishes between text
files and binary files, then you should check out <a href="../functions/binmode.html">"binmode"</a> for tips
for dealing with this.  The key distinction between systems that need
<code class="inline"><a class="l_k" href="../functions/binmode.html">binmode</a></code> and those that don&#39;t is their text file formats.  Systems
like Unix, Mac OS, and Plan 9, which delimit lines with a single
character, and which encode that character in C as <code class="inline"><span class="q">&quot;\n&quot;</span></code>
, do not
need <code class="inline"><a class="l_k" href="../functions/binmode.html">binmode</a></code>.  The rest need it.</p>
<p>When opening a file, it&#39;s usually a bad idea to continue normal execution
if the request failed, so <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> is frequently used in connection with
<code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>.  Even if <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code> won&#39;t do what you want (say, in a CGI script,
where you want to make a nicely formatted error message (but there are
modules that can help with that problem)) you should always check
the return value from opening a file.  The infrequent exception is when
working with an unopened filehandle is actually what you want to do.</p>
<p>As a special case the 3-arg form with a read/write mode and the third
argument being <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$tmp</span><span class="cm">,</span> <span class="q">&quot;+&gt;&quot;</span><span class="cm">,</span> <a class="l_k" href="../functions/undef.html">undef</a><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> ...</li></ol></pre><p>opens a filehandle to an anonymous temporary file.  Also using &quot;+&lt;&quot;
works for symmetry, but you really should consider writing something
to the temporary file first.  You will need to seek() to do the
reading.</p>
<p>Since v5.8.0, perl has built using PerlIO by default.  Unless you&#39;ve
changed this (i.e. Configure -Uuseperlio), you can open file handles to
&quot;in memory&quot; files held in Perl scalars via:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="i">$fh</span><span class="cm">,</span> <span class="q">&#39;&gt;&#39;</span><span class="cm">,</span> \<span class="i">$variable</span><span class="s">)</span> || ..</li></ol></pre><p>Though if you try to re-open <code class="inline"><span class="w">STDOUT</span></code>
 or <code class="inline"><span class="w">STDERR</span></code>
 as an &quot;in memory&quot;
file, you have to close it first:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/close.html">close</a> <span class="w">STDOUT</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&#39;&gt;&#39;</span><span class="cm">,</span> \<span class="i">$variable</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t open STDOUT: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>Examples:</p>
<pre class="verbatim"><ol><li>    $ARTICLE = 100;</li><li>    open ARTICLE or die "Can't find article $ARTICLE: $!\n";</li><li>    while (&lt;ARTICLE&gt;) {...</li><li></li><li>    open(LOG, '&gt;&gt;/usr/spool/news/twitlog');	# (log is reserved)</li><li>    # if the open fails, output is discarded</li><li></li><li>    open(my $dbase, '+&lt;', 'dbase.mine')		# open for update</li><li>	or die "Can't open 'dbase.mine' for update: $!";</li><li></li><li>    open(my $dbase, '+&lt;dbase.mine')			# ditto</li><li>	or die "Can't open 'dbase.mine' for update: $!";</li><li></li><li>    open(ARTICLE, '-|', "caesar &lt;$article")     # decrypt article</li><li>	or die "Can't start caesar: $!";</li><li></li><li>    open(ARTICLE, "caesar &lt;$article |")		# ditto</li><li>	or die "Can't start caesar: $!";</li><li></li><li>    open(EXTRACT, "|sort &gt;Tmp$$")		# $$ is our process id</li><li>	or die "Can't start sort: $!";</li><li></li><li>    # in memory files</li><li>    open(MEMORY,'&gt;', \$var)</li><li>	or die "Can't open memory file: $!";</li><li>    print MEMORY "foo!\n";			# output will end up in $var</li><li></li><li>    # process argument list of files along with any includes</li><li></li><li>    foreach $file (@ARGV) {</li><li>	process($file, 'fh00');</li><li>    }</li><li></li><li>    sub process {</li><li>	my($filename, $input) = @_;</li><li>	$input++;		# this is a string increment</li><li>	unless (open($input, $filename)) {</li><li>	    print STDERR "Can't open $filename: $!\n";</li><li>	    return;</li><li>	}</li><li></li><li>	local $_;</li><li>	while (&lt;$input&gt;) {		# note use of indirection</li><li>	    if (/^#include "(.*)"/) {</li><li>		process($1, $input);</li><li>		next;</li><li>	    }</li><li>	    #...		# whatever</li><li>	}</li><li>    }</li></ol></pre><p>See <a href="../perliol.html">perliol</a> for detailed info on PerlIO.</p>
<p>You may also, in the Bourne shell tradition, specify an EXPR beginning
with <code class="inline"><span class="q">&#39;&gt;&amp;&#39;</span></code>
, in which case the rest of the string is interpreted
as the name of a filehandle (or file descriptor, if numeric) to be
duped (as <code class="inline"><span class="i">dup</span><span class="s">(</span><span class="n">2</span><span class="s">)</span></code>
) and opened.  You may use <code class="inline"><span class="i">&amp;</span></code>
 after <code class="inline">&gt;</code>,
<code class="inline">&gt;&gt;</code>
, <code class="inline">&lt;</code>
, <code class="inline">+&gt;</code>, <code class="inline">+&gt;&gt;</code>
, and <code class="inline">+&lt;</code>
.
The mode you specify should match the mode of the original filehandle.
(Duping a filehandle does not take into account any existing contents
of IO buffers.) If you use the 3-arg form then you can pass either a
number, the name of a filehandle or the normal &quot;reference to a glob&quot;.</p>
<p>Here is a script that saves, redirects, and restores <code class="inline"><span class="w">STDOUT</span></code>
 and
<code class="inline"><span class="w">STDERR</span></code>
 using various methods:</p>
<pre class="verbatim"><ol><li>    <span class="c">#!/usr/bin/perl</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <a class="l_k" href="../functions/my.html">my</a> <span class="i">$oldout</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;STDOUT&quot;</span>     or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup STDOUT: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">OLDERR</span><span class="cm">,</span>     <span class="q">&quot;&gt;&amp;&quot;</span><span class="cm">,</span> \<span class="i">*STDERR</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup STDERR: $!&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&#39;&gt;&#39;</span><span class="cm">,</span> <span class="q">&quot;foo.out&quot;</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t redirect STDOUT: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDERR</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;STDOUT&quot;</span>     or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup STDOUT: $!&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/select.html">select</a> <span class="w">STDERR</span><span class="sc">;</span> <span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span>	<span class="c"># make unbuffered</span></li><li>    <a class="l_k" href="../functions/select.html">select</a> <span class="w">STDOUT</span><span class="sc">;</span> <span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span>	<span class="c"># make unbuffered</span></li><li></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDOUT</span> <span class="q">&quot;stdout 1\n&quot;</span><span class="sc">;</span>	<span class="c"># this works for</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;stderr 1\n&quot;</span><span class="sc">;</span> 	<span class="c"># subprocesses too</span></li><li></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;&quot;</span><span class="cm">,</span> <span class="i">$oldout</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup \$oldout: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDERR</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;OLDERR&quot;</span>    or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup OLDERR: $!&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDOUT</span> <span class="q">&quot;stdout 2\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;stderr 2\n&quot;</span><span class="sc">;</span></li></ol></pre><p>If you specify <code class="inline"><span class="q">&#39;&lt;&amp;=X&#39;</span></code>
, where <code class="inline"><span class="w">X</span></code>
 is a file descriptor number
or a filehandle, then Perl will do an equivalent of C&#39;s <code class="inline"><span class="w">fdopen</span></code>
 of
that file descriptor (and not call <code class="inline"><span class="i">dup</span><span class="s">(</span><span class="n">2</span><span class="s">)</span></code>
); this is more
parsimonious of file descriptors.  For example:</p>
<pre class="verbatim"><ol><li>    <span class="c"># open for input, reusing the fileno of $fd</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FILEHANDLE</span><span class="cm">,</span> <span class="q">&quot;&lt;&amp;=$fd&quot;</span><span class="s">)</span></li></ol></pre><p>or</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FILEHANDLE</span><span class="cm">,</span> <span class="q">&quot;&lt;&amp;=&quot;</span><span class="cm">,</span> <span class="i">$fd</span><span class="s">)</span></li></ol></pre><p>or</p>
<pre class="verbatim"><ol><li>    <span class="c"># open for append, using the fileno of OLDFH</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FH</span><span class="cm">,</span> <span class="q">&quot;&gt;&gt;&amp;=&quot;</span><span class="cm">,</span> <span class="w">OLDFH</span><span class="s">)</span></li></ol></pre><p>or</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FH</span><span class="cm">,</span> <span class="q">&quot;&gt;&gt;&amp;=OLDFH&quot;</span><span class="s">)</span></li></ol></pre><p>Being parsimonious on filehandles is also useful (besides being
parsimonious) for example when something is dependent on file
descriptors, like for example locking using flock().  If you do just
<code class="inline"><a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">A</span><span class="cm">,</span> <span class="q">&#39;&gt;&gt;&amp;B&#39;</span><span class="s">)</span></code>
, the filehandle A will not have the same file
descriptor as B, and therefore flock(A) will not flock(B), and vice
versa.  But with <code class="inline"><a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">A</span><span class="cm">,</span> <span class="q">&#39;&gt;&gt;&amp;=B&#39;</span><span class="s">)</span></code>
 the filehandles will share
the same file descriptor.</p>
<p>Note that if you are using Perls older than 5.8.0, Perl will be using
the standard C libraries&#39; fdopen() to implement the &quot;=&quot; functionality.
On many UNIX systems fdopen() fails when file descriptors exceed a
certain value, typically 255.  For Perls 5.8.0 and later, PerlIO is
most often the default.</p>
<p>You can see whether Perl has been compiled with PerlIO or not by
running <code class="inline"><span class="w">perl</span> -<span class="w">V</span></code>
 and looking for <code class="inline"><span class="w">useperlio</span>=</code>
 line.  If <code class="inline"><span class="w">useperlio</span></code>

is <code class="inline"><span class="w">define</span></code>
, you have PerlIO, otherwise you don&#39;t.</p>
<p>If you open a pipe on the command <code class="inline"><span class="q">&#39;-&#39;</span></code>
, i.e., either <code class="inline"><span class="q">&#39;|-&#39;</span></code>
 or <code class="inline"><span class="q">&#39;-|&#39;</span></code>

with 2-arguments (or 1-argument) form of open(), then
there is an implicit fork done, and the return value of open is the pid
of the child within the parent process, and <code class="inline"><span class="n">0</span></code>
 within the child
process.  (Use <code class="inline"><a class="l_k" href="../functions/defined.html">defined($pid)</a></code> to determine whether the open was successful.)
The filehandle behaves normally for the parent, but i/o to that
filehandle is piped from/to the STDOUT/STDIN of the child process.
In the child process the filehandle isn&#39;t opened--i/o happens from/to
the new STDOUT or STDIN.  Typically this is used like the normal
piped open when you want to exercise more control over just how the
pipe command gets executed, such as when you are running setuid, and
don&#39;t want to have to scan shell commands for metacharacters.
The following triples are more or less equivalent:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&quot;|tr &#39;[a-z]&#39; &#39;[A-Z]&#39;&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;|-&#39;</span><span class="cm">,</span> <span class="q">&quot;tr &#39;[a-z]&#39; &#39;[A-Z]&#39;&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;|-&#39;</span><span class="s">)</span> || <a class="l_k" href="../functions/exec.html">exec</a> <span class="q">&#39;tr&#39;</span><span class="cm">,</span> <span class="q">&#39;[a-z]&#39;</span><span class="cm">,</span> <span class="q">&#39;[A-Z]&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;|-&#39;</span><span class="cm">,</span> <span class="q">&quot;tr&quot;</span><span class="cm">,</span> <span class="q">&#39;[a-z]&#39;</span><span class="cm">,</span> <span class="q">&#39;[A-Z]&#39;</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&quot;cat -n &#39;$file&#39;|&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;-|&#39;</span><span class="cm">,</span> <span class="q">&quot;cat -n &#39;$file&#39;&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;-|&#39;</span><span class="s">)</span> || <a class="l_k" href="../functions/exec.html">exec</a> <span class="q">&#39;cat&#39;</span><span class="cm">,</span> <span class="q">&#39;-n&#39;</span><span class="cm">,</span> <span class="i">$file</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;-|&#39;</span><span class="cm">,</span> <span class="q">&quot;cat&quot;</span><span class="cm">,</span> <span class="q">&#39;-n&#39;</span><span class="cm">,</span> <span class="i">$file</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The last example in each block shows the pipe as &quot;list form&quot;, which is
not yet supported on all platforms.  A good rule of thumb is that if
your platform has true <code class="inline"><a class="l_k" href="../functions/fork.html">fork()</a></code> (in other words, if your platform is
UNIX) you can use the list form.</p>
<p>See <a href="../perlipc.html#Safe-Pipe-Opens">"Safe Pipe Opens" in perlipc</a> for more examples of this.</p>
<p>Beginning with v5.6.0, Perl will attempt to flush all files opened for
output before any operation that may do a fork, but this may not be
supported on some platforms (see <a href="../perlport.html">perlport</a>).  To be safe, you may need
to set <code class="inline"><span class="i">$|</span></code>
 ($AUTOFLUSH in English) or call the <code class="inline"><span class="i">autoflush</span><span class="s">(</span><span class="s">)</span></code>
 method
of <code class="inline"><span class="w">IO::Handle</span></code>
 on any open handles.</p>
<p>On systems that support a close-on-exec flag on files, the flag will
be set for the newly opened file descriptor as determined by the value
of $^F.  See <a href="../perlvar.html#%24%5eF">"$^F" in perlvar</a>.</p>
<p>Closing any piped filehandle causes the parent process to wait for the
child to finish, and returns the status value in <code class="inline"><span class="i">$?</span></code>
 and
<code class="inline"><span class="i">$</span>{<span class="w">^CHILD_ERROR_NATIVE</span>}</code>
.</p>
<p>The filename passed to 2-argument (or 1-argument) form of open() will
have leading and trailing whitespace deleted, and the normal
redirection characters honored.  This property, known as &quot;magic open&quot;,
can often be used to good effect.  A user could specify a filename of
<i>&quot;rsh cat file |&quot;</i>, or you could change certain filenames as needed:</p>
<pre class="verbatim"><ol><li>    <span class="i">$filename</span> =~ <span class="q">s/(.*\.gz)\s*$/gzip -dc &lt; $1|/</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FH</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t open $filename: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>Use 3-argument form to open a file with arbitrary weird characters in it,</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;&lt;&#39;</span><span class="cm">,</span> <span class="i">$file</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>otherwise it&#39;s necessary to protect any leading and trailing whitespace:</p>
<pre class="verbatim"><ol><li>    <span class="i">$file</span> =~ <span class="q">s#^(\s)#./$1#</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&quot;&lt; $file\0&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>(this may not work on some bizarre filesystems).  One should
conscientiously choose between the <i>magic</i> and 3-arguments form
of open():</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">IN</span><span class="cm">,</span> <span class="i">$ARGV</span>[<span class="n">0</span>]<span class="sc">;</span></li></ol></pre><p>will allow the user to specify an argument of the form <code class="inline"><span class="q">&quot;rsh cat file |&quot;</span></code>
,
but will not work on a filename which happens to have a trailing space, while</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">IN</span><span class="cm">,</span> <span class="q">&#39;&lt;&#39;</span><span class="cm">,</span> <span class="i">$ARGV</span>[<span class="n">0</span>]<span class="sc">;</span></li></ol></pre><p>will have exactly the opposite restrictions.</p>
<p>If you want a &quot;real&quot; C <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> (see <code class="inline"><a class="l_k" href="../functions/open.html">open(2)</a></code> on your system), then you
should use the <code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code> function, which involves no such magic (but
may use subtly different filemodes than Perl open(), which is mapped
to C fopen()).  This is
another way to protect your filenames from interpretation.  For example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">IO::Handle</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/sysopen.html">sysopen</a><span class="s">(</span><span class="w">HANDLE</span><span class="cm">,</span> <span class="i">$path</span><span class="cm">,</span> <span class="w">O_RDWR</span>|<span class="w">O_CREAT</span>|<span class="w">O_EXCL</span><span class="s">)</span></li><li>	or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;sysopen $path: $!&quot;</span><span class="sc">;</span></li><li>    <span class="i">$oldfh</span> = <a class="l_k" href="../functions/select.html">select</a><span class="s">(</span><span class="w">HANDLE</span><span class="s">)</span><span class="sc">;</span> <span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span> <a class="l_k" href="../functions/select.html">select</a><span class="s">(</span><span class="i">$oldfh</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">HANDLE</span> <span class="q">&quot;stuff $$\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/seek.html">seek</a><span class="s">(</span><span class="w">HANDLE</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;File contains: &quot;</span><span class="cm">,</span> <span class="q">&lt;HANDLE&gt;</span><span class="sc">;</span></li></ol></pre><p>Using the constructor from the <code class="inline"><span class="w">IO::Handle</span></code>
 package (or one of its
subclasses, such as <code class="inline"><span class="w">IO::File</span></code>
 or <code class="inline"><span class="w">IO::Socket</span></code>
), you can generate anonymous
filehandles that have the scope of whatever variables hold references to
them, and automatically close whenever and however you leave that scope:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">IO::File</span><span class="sc">;</span></li><li>    <span class="c">#...</span></li><li><a name="read_myfile_munged"></a>    sub <span class="m">read_myfile_munged</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="i">$ALL</span> = <a class="l_k" href="../functions/shift.html">shift</a><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="i">$handle</span> = <span class="w">IO::File</span><span class="w">-&gt;new</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="i">$handle</span><span class="cm">,</span> <span class="q">&quot;myfile&quot;</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;myfile: $!&quot;</span><span class="sc">;</span></li><li>	<span class="i">$first</span> = <span class="q">&lt;$handle&gt;</span></li><li>	    or <a class="l_k" href="../functions/return.html">return</a> <span class="s">(</span><span class="s">)</span><span class="sc">;</span>     <span class="c"># Automatically closed here.</span></li><li>	<span class="w">mung</span> <span class="i">$first</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;mung failed&quot;</span><span class="sc">;</span>	<span class="c"># Or here.</span></li><li>	<a class="l_k" href="../functions/return.html">return</a> <span class="i">$first</span><span class="cm">,</span> <span class="q">&lt;$handle&gt;</span> if <span class="i">$ALL</span><span class="sc">;</span>	<span class="c"># Or here.</span></li><li>	<span class="i">$first</span><span class="sc">;</span>					<span class="c"># Or here.</span></li><li>    <span class="s">}</span></li></ol></pre><p>See <a href="../functions/seek.html">"seek"</a> for some details about mixing reading and writing.
-----

function open
filehandle|
mode|
expr|
list|
Opens the file whose filename is given by EXPR, and associates it with
FILEHANDLE.</p>
<p>Simple examples to open a file for reading:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">'&lt;'</span><span class="cm">,</span> <span class="q">&quot;input.txt&quot;</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="i">$!</span><span class="sc">;</span></li></ol></pre><p>and for writing:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">'&gt;'</span><span class="cm">,</span> <span class="q">&quot;output.txt&quot;</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="i">$!</span><span class="sc">;</span></li></ol></pre><p>(The following is a comprehensive reference to open(): for a gentler
introduction you may consider <a href="../perlopentut.html">perlopentut</a>.)</p>
<p>If FILEHANDLE is an undefined scalar variable (or array or hash element)
the variable is assigned a reference to a new anonymous filehandle,
otherwise if FILEHANDLE is an expression, its value is used as the name of
the real filehandle wanted.  (This is considered a symbolic reference, so
<code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">strict</span> <span class="q">&#39;refs&#39;</span></code>
 should <i>not</i> be in effect.)</p>
<p>If EXPR is omitted, the scalar variable of the same name as the
FILEHANDLE contains the filename.  (Note that lexical variables--those
declared with <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>--will not work for this purpose; so if you&#39;re
using <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>, specify EXPR in your call to open.)</p>
<p>If three or more arguments are specified then the mode of opening and
the file name are separate. If MODE is <code class="inline"><span class="q">&#39;&lt;&#39;</span></code>
 or nothing, the file
is opened for input.  If MODE is <code class="inline"><span class="q">&#39;&gt;&#39;</span></code>
, the file is truncated and
opened for output, being created if necessary.  If MODE is <code class="inline"><span class="q">&#39;&gt;&gt;&#39;</span></code>
,
the file is opened for appending, again being created if necessary.</p>
<p>You can put a <code class="inline"><span class="q">&#39;+&#39;</span></code>
 in front of the <code class="inline"><span class="q">&#39;&gt;&#39;</span></code>
 or <code class="inline"><span class="q">&#39;&lt;&#39;</span></code>
 to
indicate that you want both read and write access to the file; thus
<code class="inline"><span class="q">&#39;+&lt;&#39;</span></code>
 is almost always preferred for read/write updates--the <code class="inline"><span class="q">&#39;+&gt;&#39;</span></code>
 mode would clobber the file first.  You can&#39;t usually use
either read-write mode for updating textfiles, since they have
variable length records.  See the <b>-i</b> switch in <a href="../perlrun.html">perlrun</a> for a
better approach.  The file is created with permissions of <code class="inline"><span class="n">0666</span></code>

modified by the process&#39; <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code> value.</p>
<p>These various prefixes correspond to the fopen(3) modes of <code class="inline"><span class="q">&#39;r&#39;</span></code>
,
<code class="inline"><span class="q">&#39;r+&#39;</span></code>
, <code class="inline"><span class="q">&#39;w&#39;</span></code>
, <code class="inline"><span class="q">&#39;w+&#39;</span></code>
, <code class="inline"><span class="q">&#39;a&#39;</span></code>
, and <code class="inline"><span class="q">&#39;a+&#39;</span></code>
.</p>
<p>In the 2-arguments (and 1-argument) form of the call the mode and
filename should be concatenated (in this order), possibly separated by
spaces.  It is possible to omit the mode in these forms if the mode is
<code class="inline"><span class="q">&#39;&lt;&#39;</span></code>
.</p>
<p>If the filename begins with <code class="inline"><span class="q">&#39;|&#39;</span></code>
, the filename is interpreted as a
command to which output is to be piped, and if the filename ends with a
<code class="inline"><span class="q">&#39;|&#39;</span></code>
, the filename is interpreted as a command which pipes output to
us.  See <a href="../perlipc.html#Using-open()-for-IPC">"Using open() for IPC" in perlipc</a>
for more examples of this.  (You are not allowed to <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> to a command
that pipes both in <i>and</i> out, but see <a href="../IPC/Open2.html">IPC::Open2</a>, <a href="../IPC/Open3.html">IPC::Open3</a>,
and <a href="../perlipc.html#Bidirectional-Communication-with-Another-Process">"Bidirectional Communication with Another Process" in perlipc</a>
for alternatives.)</p>
<p>For three or more arguments if MODE is <code class="inline"><span class="q">&#39;|-&#39;</span></code>
, the filename is
interpreted as a command to which output is to be piped, and if MODE
is <code class="inline"><span class="q">&#39;-|&#39;</span></code>
, the filename is interpreted as a command which pipes
output to us.  In the 2-arguments (and 1-argument) form one should
replace dash (<code class="inline"><span class="q">&#39;-&#39;</span></code>
) with the command.
See <a href="../perlipc.html#Using-open()-for-IPC">"Using open() for IPC" in perlipc</a> for more examples of this.
(You are not allowed to <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> to a command that pipes both in <i>and</i>
out, but see <a href="../IPC/Open2.html">IPC::Open2</a>, <a href="../IPC/Open3.html">IPC::Open3</a>, and
<a href="../perlipc.html#Bidirectional-Communication">"Bidirectional Communication" in perlipc</a> for alternatives.)</p>
<p>In the three-or-more argument form of pipe opens, if LIST is specified
(extra arguments after the command name) then LIST becomes arguments
to the command invoked if the platform supports it.  The meaning of
<code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> with more than three arguments for non-pipe modes is not yet
specified. Experimental &quot;layers&quot; may give extra LIST arguments
meaning.</p>
<p>In the 2-arguments (and 1-argument) form opening <code class="inline"><span class="q">&#39;-&#39;</span></code>
 opens STDIN
and opening <code class="inline"><span class="q">&#39;&gt;-&#39;</span></code>
 opens STDOUT.</p>
<p>You may use the three-argument form of open to specify IO &quot;layers&quot;
(sometimes also referred to as &quot;disciplines&quot;) to be applied to the handle
that affect how the input and output are processed (see <a href="../open.html">open</a> and
<a href="../PerlIO.html">PerlIO</a> for more details). For example</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">&quot;&lt;:encoding(UTF-8)&quot;</span><span class="cm">,</span> <span class="q">&quot;file&quot;</span><span class="s">)</span></li></ol></pre><p>will open the UTF-8 encoded file containing Unicode characters,
see <a href="../perluniintro.html">perluniintro</a>. Note that if layers are specified in the
three-arg form then default layers stored in ${^OPEN} (see <a href="../perlvar.html">perlvar</a>;
usually set by the <b>open</b> pragma or the switch <b>-CioD</b>) are ignored.</p>
<p>Open returns nonzero upon success, the undefined value otherwise.  If
the <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> involved a pipe, the return value happens to be the pid of
the subprocess.</p>
<p>If you&#39;re running Perl on a system that distinguishes between text
files and binary files, then you should check out <a href="../functions/binmode.html">"binmode"</a> for tips
for dealing with this.  The key distinction between systems that need
<code class="inline"><a class="l_k" href="../functions/binmode.html">binmode</a></code> and those that don&#39;t is their text file formats.  Systems
like Unix, Mac OS, and Plan 9, which delimit lines with a single
character, and which encode that character in C as <code class="inline"><span class="q">&quot;\n&quot;</span></code>
, do not
need <code class="inline"><a class="l_k" href="../functions/binmode.html">binmode</a></code>.  The rest need it.</p>
<p>When opening a file, it&#39;s usually a bad idea to continue normal execution
if the request failed, so <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> is frequently used in connection with
<code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>.  Even if <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code> won&#39;t do what you want (say, in a CGI script,
where you want to make a nicely formatted error message (but there are
modules that can help with that problem)) you should always check
the return value from opening a file.  The infrequent exception is when
working with an unopened filehandle is actually what you want to do.</p>
<p>As a special case the 3-arg form with a read/write mode and the third
argument being <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$tmp</span><span class="cm">,</span> <span class="q">&quot;+&gt;&quot;</span><span class="cm">,</span> <a class="l_k" href="../functions/undef.html">undef</a><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> ...</li></ol></pre><p>opens a filehandle to an anonymous temporary file.  Also using &quot;+&lt;&quot;
works for symmetry, but you really should consider writing something
to the temporary file first.  You will need to seek() to do the
reading.</p>
<p>Since v5.8.0, perl has built using PerlIO by default.  Unless you&#39;ve
changed this (i.e. Configure -Uuseperlio), you can open file handles to
&quot;in memory&quot; files held in Perl scalars via:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="i">$fh</span><span class="cm">,</span> <span class="q">&#39;&gt;&#39;</span><span class="cm">,</span> \<span class="i">$variable</span><span class="s">)</span> || ..</li></ol></pre><p>Though if you try to re-open <code class="inline"><span class="w">STDOUT</span></code>
 or <code class="inline"><span class="w">STDERR</span></code>
 as an &quot;in memory&quot;
file, you have to close it first:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/close.html">close</a> <span class="w">STDOUT</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&#39;&gt;&#39;</span><span class="cm">,</span> \<span class="i">$variable</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t open STDOUT: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>Examples:</p>
<pre class="verbatim"><ol><li>    $ARTICLE = 100;</li><li>    open ARTICLE or die "Can't find article $ARTICLE: $!\n";</li><li>    while (&lt;ARTICLE&gt;) {...</li><li></li><li>    open(LOG, '&gt;&gt;/usr/spool/news/twitlog');	# (log is reserved)</li><li>    # if the open fails, output is discarded</li><li></li><li>    open(my $dbase, '+&lt;', 'dbase.mine')		# open for update</li><li>	or die "Can't open 'dbase.mine' for update: $!";</li><li></li><li>    open(my $dbase, '+&lt;dbase.mine')			# ditto</li><li>	or die "Can't open 'dbase.mine' for update: $!";</li><li></li><li>    open(ARTICLE, '-|', "caesar &lt;$article")     # decrypt article</li><li>	or die "Can't start caesar: $!";</li><li></li><li>    open(ARTICLE, "caesar &lt;$article |")		# ditto</li><li>	or die "Can't start caesar: $!";</li><li></li><li>    open(EXTRACT, "|sort &gt;Tmp$$")		# $$ is our process id</li><li>	or die "Can't start sort: $!";</li><li></li><li>    # in memory files</li><li>    open(MEMORY,'&gt;', \$var)</li><li>	or die "Can't open memory file: $!";</li><li>    print MEMORY "foo!\n";			# output will end up in $var</li><li></li><li>    # process argument list of files along with any includes</li><li></li><li>    foreach $file (@ARGV) {</li><li>	process($file, 'fh00');</li><li>    }</li><li></li><li>    sub process {</li><li>	my($filename, $input) = @_;</li><li>	$input++;		# this is a string increment</li><li>	unless (open($input, $filename)) {</li><li>	    print STDERR "Can't open $filename: $!\n";</li><li>	    return;</li><li>	}</li><li></li><li>	local $_;</li><li>	while (&lt;$input&gt;) {		# note use of indirection</li><li>	    if (/^#include "(.*)"/) {</li><li>		process($1, $input);</li><li>		next;</li><li>	    }</li><li>	    #...		# whatever</li><li>	}</li><li>    }</li></ol></pre><p>See <a href="../perliol.html">perliol</a> for detailed info on PerlIO.</p>
<p>You may also, in the Bourne shell tradition, specify an EXPR beginning
with <code class="inline"><span class="q">&#39;&gt;&amp;&#39;</span></code>
, in which case the rest of the string is interpreted
as the name of a filehandle (or file descriptor, if numeric) to be
duped (as <code class="inline"><span class="i">dup</span><span class="s">(</span><span class="n">2</span><span class="s">)</span></code>
) and opened.  You may use <code class="inline"><span class="i">&amp;</span></code>
 after <code class="inline">&gt;</code>,
<code class="inline">&gt;&gt;</code>
, <code class="inline">&lt;</code>
, <code class="inline">+&gt;</code>, <code class="inline">+&gt;&gt;</code>
, and <code class="inline">+&lt;</code>
.
The mode you specify should match the mode of the original filehandle.
(Duping a filehandle does not take into account any existing contents
of IO buffers.) If you use the 3-arg form then you can pass either a
number, the name of a filehandle or the normal &quot;reference to a glob&quot;.</p>
<p>Here is a script that saves, redirects, and restores <code class="inline"><span class="w">STDOUT</span></code>
 and
<code class="inline"><span class="w">STDERR</span></code>
 using various methods:</p>
<pre class="verbatim"><ol><li>    <span class="c">#!/usr/bin/perl</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <a class="l_k" href="../functions/my.html">my</a> <span class="i">$oldout</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;STDOUT&quot;</span>     or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup STDOUT: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">OLDERR</span><span class="cm">,</span>     <span class="q">&quot;&gt;&amp;&quot;</span><span class="cm">,</span> \<span class="i">*STDERR</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup STDERR: $!&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&#39;&gt;&#39;</span><span class="cm">,</span> <span class="q">&quot;foo.out&quot;</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t redirect STDOUT: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDERR</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;STDOUT&quot;</span>     or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup STDOUT: $!&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/select.html">select</a> <span class="w">STDERR</span><span class="sc">;</span> <span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span>	<span class="c"># make unbuffered</span></li><li>    <a class="l_k" href="../functions/select.html">select</a> <span class="w">STDOUT</span><span class="sc">;</span> <span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span>	<span class="c"># make unbuffered</span></li><li></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDOUT</span> <span class="q">&quot;stdout 1\n&quot;</span><span class="sc">;</span>	<span class="c"># this works for</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;stderr 1\n&quot;</span><span class="sc">;</span> 	<span class="c"># subprocesses too</span></li><li></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;&quot;</span><span class="cm">,</span> <span class="i">$oldout</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup \$oldout: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDERR</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;OLDERR&quot;</span>    or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup OLDERR: $!&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDOUT</span> <span class="q">&quot;stdout 2\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;stderr 2\n&quot;</span><span class="sc">;</span></li></ol></pre><p>If you specify <code class="inline"><span class="q">&#39;&lt;&amp;=X&#39;</span></code>
, where <code class="inline"><span class="w">X</span></code>
 is a file descriptor number
or a filehandle, then Perl will do an equivalent of C&#39;s <code class="inline"><span class="w">fdopen</span></code>
 of
that file descriptor (and not call <code class="inline"><span class="i">dup</span><span class="s">(</span><span class="n">2</span><span class="s">)</span></code>
); this is more
parsimonious of file descriptors.  For example:</p>
<pre class="verbatim"><ol><li>    <span class="c"># open for input, reusing the fileno of $fd</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FILEHANDLE</span><span class="cm">,</span> <span class="q">&quot;&lt;&amp;=$fd&quot;</span><span class="s">)</span></li></ol></pre><p>or</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FILEHANDLE</span><span class="cm">,</span> <span class="q">&quot;&lt;&amp;=&quot;</span><span class="cm">,</span> <span class="i">$fd</span><span class="s">)</span></li></ol></pre><p>or</p>
<pre class="verbatim"><ol><li>    <span class="c"># open for append, using the fileno of OLDFH</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FH</span><span class="cm">,</span> <span class="q">&quot;&gt;&gt;&amp;=&quot;</span><span class="cm">,</span> <span class="w">OLDFH</span><span class="s">)</span></li></ol></pre><p>or</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FH</span><span class="cm">,</span> <span class="q">&quot;&gt;&gt;&amp;=OLDFH&quot;</span><span class="s">)</span></li></ol></pre><p>Being parsimonious on filehandles is also useful (besides being
parsimonious) for example when something is dependent on file
descriptors, like for example locking using flock().  If you do just
<code class="inline"><a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">A</span><span class="cm">,</span> <span class="q">&#39;&gt;&gt;&amp;B&#39;</span><span class="s">)</span></code>
, the filehandle A will not have the same file
descriptor as B, and therefore flock(A) will not flock(B), and vice
versa.  But with <code class="inline"><a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">A</span><span class="cm">,</span> <span class="q">&#39;&gt;&gt;&amp;=B&#39;</span><span class="s">)</span></code>
 the filehandles will share
the same file descriptor.</p>
<p>Note that if you are using Perls older than 5.8.0, Perl will be using
the standard C libraries&#39; fdopen() to implement the &quot;=&quot; functionality.
On many UNIX systems fdopen() fails when file descriptors exceed a
certain value, typically 255.  For Perls 5.8.0 and later, PerlIO is
most often the default.</p>
<p>You can see whether Perl has been compiled with PerlIO or not by
running <code class="inline"><span class="w">perl</span> -<span class="w">V</span></code>
 and looking for <code class="inline"><span class="w">useperlio</span>=</code>
 line.  If <code class="inline"><span class="w">useperlio</span></code>

is <code class="inline"><span class="w">define</span></code>
, you have PerlIO, otherwise you don&#39;t.</p>
<p>If you open a pipe on the command <code class="inline"><span class="q">&#39;-&#39;</span></code>
, i.e., either <code class="inline"><span class="q">&#39;|-&#39;</span></code>
 or <code class="inline"><span class="q">&#39;-|&#39;</span></code>

with 2-arguments (or 1-argument) form of open(), then
there is an implicit fork done, and the return value of open is the pid
of the child within the parent process, and <code class="inline"><span class="n">0</span></code>
 within the child
process.  (Use <code class="inline"><a class="l_k" href="../functions/defined.html">defined($pid)</a></code> to determine whether the open was successful.)
The filehandle behaves normally for the parent, but i/o to that
filehandle is piped from/to the STDOUT/STDIN of the child process.
In the child process the filehandle isn&#39;t opened--i/o happens from/to
the new STDOUT or STDIN.  Typically this is used like the normal
piped open when you want to exercise more control over just how the
pipe command gets executed, such as when you are running setuid, and
don&#39;t want to have to scan shell commands for metacharacters.
The following triples are more or less equivalent:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&quot;|tr &#39;[a-z]&#39; &#39;[A-Z]&#39;&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;|-&#39;</span><span class="cm">,</span> <span class="q">&quot;tr &#39;[a-z]&#39; &#39;[A-Z]&#39;&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;|-&#39;</span><span class="s">)</span> || <a class="l_k" href="../functions/exec.html">exec</a> <span class="q">&#39;tr&#39;</span><span class="cm">,</span> <span class="q">&#39;[a-z]&#39;</span><span class="cm">,</span> <span class="q">&#39;[A-Z]&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;|-&#39;</span><span class="cm">,</span> <span class="q">&quot;tr&quot;</span><span class="cm">,</span> <span class="q">&#39;[a-z]&#39;</span><span class="cm">,</span> <span class="q">&#39;[A-Z]&#39;</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&quot;cat -n &#39;$file&#39;|&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;-|&#39;</span><span class="cm">,</span> <span class="q">&quot;cat -n &#39;$file&#39;&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;-|&#39;</span><span class="s">)</span> || <a class="l_k" href="../functions/exec.html">exec</a> <span class="q">&#39;cat&#39;</span><span class="cm">,</span> <span class="q">&#39;-n&#39;</span><span class="cm">,</span> <span class="i">$file</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;-|&#39;</span><span class="cm">,</span> <span class="q">&quot;cat&quot;</span><span class="cm">,</span> <span class="q">&#39;-n&#39;</span><span class="cm">,</span> <span class="i">$file</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The last example in each block shows the pipe as &quot;list form&quot;, which is
not yet supported on all platforms.  A good rule of thumb is that if
your platform has true <code class="inline"><a class="l_k" href="../functions/fork.html">fork()</a></code> (in other words, if your platform is
UNIX) you can use the list form.</p>
<p>See <a href="../perlipc.html#Safe-Pipe-Opens">"Safe Pipe Opens" in perlipc</a> for more examples of this.</p>
<p>Beginning with v5.6.0, Perl will attempt to flush all files opened for
output before any operation that may do a fork, but this may not be
supported on some platforms (see <a href="../perlport.html">perlport</a>).  To be safe, you may need
to set <code class="inline"><span class="i">$|</span></code>
 ($AUTOFLUSH in English) or call the <code class="inline"><span class="i">autoflush</span><span class="s">(</span><span class="s">)</span></code>
 method
of <code class="inline"><span class="w">IO::Handle</span></code>
 on any open handles.</p>
<p>On systems that support a close-on-exec flag on files, the flag will
be set for the newly opened file descriptor as determined by the value
of $^F.  See <a href="../perlvar.html#%24%5eF">"$^F" in perlvar</a>.</p>
<p>Closing any piped filehandle causes the parent process to wait for the
child to finish, and returns the status value in <code class="inline"><span class="i">$?</span></code>
 and
<code class="inline"><span class="i">$</span>{<span class="w">^CHILD_ERROR_NATIVE</span>}</code>
.</p>
<p>The filename passed to 2-argument (or 1-argument) form of open() will
have leading and trailing whitespace deleted, and the normal
redirection characters honored.  This property, known as &quot;magic open&quot;,
can often be used to good effect.  A user could specify a filename of
<i>&quot;rsh cat file |&quot;</i>, or you could change certain filenames as needed:</p>
<pre class="verbatim"><ol><li>    <span class="i">$filename</span> =~ <span class="q">s/(.*\.gz)\s*$/gzip -dc &lt; $1|/</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FH</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t open $filename: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>Use 3-argument form to open a file with arbitrary weird characters in it,</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;&lt;&#39;</span><span class="cm">,</span> <span class="i">$file</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>otherwise it&#39;s necessary to protect any leading and trailing whitespace:</p>
<pre class="verbatim"><ol><li>    <span class="i">$file</span> =~ <span class="q">s#^(\s)#./$1#</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&quot;&lt; $file\0&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>(this may not work on some bizarre filesystems).  One should
conscientiously choose between the <i>magic</i> and 3-arguments form
of open():</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">IN</span><span class="cm">,</span> <span class="i">$ARGV</span>[<span class="n">0</span>]<span class="sc">;</span></li></ol></pre><p>will allow the user to specify an argument of the form <code class="inline"><span class="q">&quot;rsh cat file |&quot;</span></code>
,
but will not work on a filename which happens to have a trailing space, while</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">IN</span><span class="cm">,</span> <span class="q">&#39;&lt;&#39;</span><span class="cm">,</span> <span class="i">$ARGV</span>[<span class="n">0</span>]<span class="sc">;</span></li></ol></pre><p>will have exactly the opposite restrictions.</p>
<p>If you want a &quot;real&quot; C <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> (see <code class="inline"><a class="l_k" href="../functions/open.html">open(2)</a></code> on your system), then you
should use the <code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code> function, which involves no such magic (but
may use subtly different filemodes than Perl open(), which is mapped
to C fopen()).  This is
another way to protect your filenames from interpretation.  For example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">IO::Handle</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/sysopen.html">sysopen</a><span class="s">(</span><span class="w">HANDLE</span><span class="cm">,</span> <span class="i">$path</span><span class="cm">,</span> <span class="w">O_RDWR</span>|<span class="w">O_CREAT</span>|<span class="w">O_EXCL</span><span class="s">)</span></li><li>	or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;sysopen $path: $!&quot;</span><span class="sc">;</span></li><li>    <span class="i">$oldfh</span> = <a class="l_k" href="../functions/select.html">select</a><span class="s">(</span><span class="w">HANDLE</span><span class="s">)</span><span class="sc">;</span> <span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span> <a class="l_k" href="../functions/select.html">select</a><span class="s">(</span><span class="i">$oldfh</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">HANDLE</span> <span class="q">&quot;stuff $$\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/seek.html">seek</a><span class="s">(</span><span class="w">HANDLE</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;File contains: &quot;</span><span class="cm">,</span> <span class="q">&lt;HANDLE&gt;</span><span class="sc">;</span></li></ol></pre><p>Using the constructor from the <code class="inline"><span class="w">IO::Handle</span></code>
 package (or one of its
subclasses, such as <code class="inline"><span class="w">IO::File</span></code>
 or <code class="inline"><span class="w">IO::Socket</span></code>
), you can generate anonymous
filehandles that have the scope of whatever variables hold references to
them, and automatically close whenever and however you leave that scope:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">IO::File</span><span class="sc">;</span></li><li>    <span class="c">#...</span></li><li><a name="read_myfile_munged"></a>    sub <span class="m">read_myfile_munged</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="i">$ALL</span> = <a class="l_k" href="../functions/shift.html">shift</a><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="i">$handle</span> = <span class="w">IO::File</span><span class="w">-&gt;new</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="i">$handle</span><span class="cm">,</span> <span class="q">&quot;myfile&quot;</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;myfile: $!&quot;</span><span class="sc">;</span></li><li>	<span class="i">$first</span> = <span class="q">&lt;$handle&gt;</span></li><li>	    or <a class="l_k" href="../functions/return.html">return</a> <span class="s">(</span><span class="s">)</span><span class="sc">;</span>     <span class="c"># Automatically closed here.</span></li><li>	<span class="w">mung</span> <span class="i">$first</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;mung failed&quot;</span><span class="sc">;</span>	<span class="c"># Or here.</span></li><li>	<a class="l_k" href="../functions/return.html">return</a> <span class="i">$first</span><span class="cm">,</span> <span class="q">&lt;$handle&gt;</span> if <span class="i">$ALL</span><span class="sc">;</span>	<span class="c"># Or here.</span></li><li>	<span class="i">$first</span><span class="sc">;</span>					<span class="c"># Or here.</span></li><li>    <span class="s">}</span></li></ol></pre><p>See <a href="../functions/seek.html">"seek"</a> for some details about mixing reading and writing.
-----

function open
filehandle|
mode|
reference|
Opens the file whose filename is given by EXPR, and associates it with
FILEHANDLE.</p>
<p>Simple examples to open a file for reading:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">'&lt;'</span><span class="cm">,</span> <span class="q">&quot;input.txt&quot;</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="i">$!</span><span class="sc">;</span></li></ol></pre><p>and for writing:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">'&gt;'</span><span class="cm">,</span> <span class="q">&quot;output.txt&quot;</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="i">$!</span><span class="sc">;</span></li></ol></pre><p>(The following is a comprehensive reference to open(): for a gentler
introduction you may consider <a href="../perlopentut.html">perlopentut</a>.)</p>
<p>If FILEHANDLE is an undefined scalar variable (or array or hash element)
the variable is assigned a reference to a new anonymous filehandle,
otherwise if FILEHANDLE is an expression, its value is used as the name of
the real filehandle wanted.  (This is considered a symbolic reference, so
<code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">strict</span> <span class="q">&#39;refs&#39;</span></code>
 should <i>not</i> be in effect.)</p>
<p>If EXPR is omitted, the scalar variable of the same name as the
FILEHANDLE contains the filename.  (Note that lexical variables--those
declared with <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>--will not work for this purpose; so if you&#39;re
using <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>, specify EXPR in your call to open.)</p>
<p>If three or more arguments are specified then the mode of opening and
the file name are separate. If MODE is <code class="inline"><span class="q">&#39;&lt;&#39;</span></code>
 or nothing, the file
is opened for input.  If MODE is <code class="inline"><span class="q">&#39;&gt;&#39;</span></code>
, the file is truncated and
opened for output, being created if necessary.  If MODE is <code class="inline"><span class="q">&#39;&gt;&gt;&#39;</span></code>
,
the file is opened for appending, again being created if necessary.</p>
<p>You can put a <code class="inline"><span class="q">&#39;+&#39;</span></code>
 in front of the <code class="inline"><span class="q">&#39;&gt;&#39;</span></code>
 or <code class="inline"><span class="q">&#39;&lt;&#39;</span></code>
 to
indicate that you want both read and write access to the file; thus
<code class="inline"><span class="q">&#39;+&lt;&#39;</span></code>
 is almost always preferred for read/write updates--the <code class="inline"><span class="q">&#39;+&gt;&#39;</span></code>
 mode would clobber the file first.  You can&#39;t usually use
either read-write mode for updating textfiles, since they have
variable length records.  See the <b>-i</b> switch in <a href="../perlrun.html">perlrun</a> for a
better approach.  The file is created with permissions of <code class="inline"><span class="n">0666</span></code>

modified by the process&#39; <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code> value.</p>
<p>These various prefixes correspond to the fopen(3) modes of <code class="inline"><span class="q">&#39;r&#39;</span></code>
,
<code class="inline"><span class="q">&#39;r+&#39;</span></code>
, <code class="inline"><span class="q">&#39;w&#39;</span></code>
, <code class="inline"><span class="q">&#39;w+&#39;</span></code>
, <code class="inline"><span class="q">&#39;a&#39;</span></code>
, and <code class="inline"><span class="q">&#39;a+&#39;</span></code>
.</p>
<p>In the 2-arguments (and 1-argument) form of the call the mode and
filename should be concatenated (in this order), possibly separated by
spaces.  It is possible to omit the mode in these forms if the mode is
<code class="inline"><span class="q">&#39;&lt;&#39;</span></code>
.</p>
<p>If the filename begins with <code class="inline"><span class="q">&#39;|&#39;</span></code>
, the filename is interpreted as a
command to which output is to be piped, and if the filename ends with a
<code class="inline"><span class="q">&#39;|&#39;</span></code>
, the filename is interpreted as a command which pipes output to
us.  See <a href="../perlipc.html#Using-open()-for-IPC">"Using open() for IPC" in perlipc</a>
for more examples of this.  (You are not allowed to <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> to a command
that pipes both in <i>and</i> out, but see <a href="../IPC/Open2.html">IPC::Open2</a>, <a href="../IPC/Open3.html">IPC::Open3</a>,
and <a href="../perlipc.html#Bidirectional-Communication-with-Another-Process">"Bidirectional Communication with Another Process" in perlipc</a>
for alternatives.)</p>
<p>For three or more arguments if MODE is <code class="inline"><span class="q">&#39;|-&#39;</span></code>
, the filename is
interpreted as a command to which output is to be piped, and if MODE
is <code class="inline"><span class="q">&#39;-|&#39;</span></code>
, the filename is interpreted as a command which pipes
output to us.  In the 2-arguments (and 1-argument) form one should
replace dash (<code class="inline"><span class="q">&#39;-&#39;</span></code>
) with the command.
See <a href="../perlipc.html#Using-open()-for-IPC">"Using open() for IPC" in perlipc</a> for more examples of this.
(You are not allowed to <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> to a command that pipes both in <i>and</i>
out, but see <a href="../IPC/Open2.html">IPC::Open2</a>, <a href="../IPC/Open3.html">IPC::Open3</a>, and
<a href="../perlipc.html#Bidirectional-Communication">"Bidirectional Communication" in perlipc</a> for alternatives.)</p>
<p>In the three-or-more argument form of pipe opens, if LIST is specified
(extra arguments after the command name) then LIST becomes arguments
to the command invoked if the platform supports it.  The meaning of
<code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> with more than three arguments for non-pipe modes is not yet
specified. Experimental &quot;layers&quot; may give extra LIST arguments
meaning.</p>
<p>In the 2-arguments (and 1-argument) form opening <code class="inline"><span class="q">&#39;-&#39;</span></code>
 opens STDIN
and opening <code class="inline"><span class="q">&#39;&gt;-&#39;</span></code>
 opens STDOUT.</p>
<p>You may use the three-argument form of open to specify IO &quot;layers&quot;
(sometimes also referred to as &quot;disciplines&quot;) to be applied to the handle
that affect how the input and output are processed (see <a href="../open.html">open</a> and
<a href="../PerlIO.html">PerlIO</a> for more details). For example</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">&quot;&lt;:encoding(UTF-8)&quot;</span><span class="cm">,</span> <span class="q">&quot;file&quot;</span><span class="s">)</span></li></ol></pre><p>will open the UTF-8 encoded file containing Unicode characters,
see <a href="../perluniintro.html">perluniintro</a>. Note that if layers are specified in the
three-arg form then default layers stored in ${^OPEN} (see <a href="../perlvar.html">perlvar</a>;
usually set by the <b>open</b> pragma or the switch <b>-CioD</b>) are ignored.</p>
<p>Open returns nonzero upon success, the undefined value otherwise.  If
the <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> involved a pipe, the return value happens to be the pid of
the subprocess.</p>
<p>If you&#39;re running Perl on a system that distinguishes between text
files and binary files, then you should check out <a href="../functions/binmode.html">"binmode"</a> for tips
for dealing with this.  The key distinction between systems that need
<code class="inline"><a class="l_k" href="../functions/binmode.html">binmode</a></code> and those that don&#39;t is their text file formats.  Systems
like Unix, Mac OS, and Plan 9, which delimit lines with a single
character, and which encode that character in C as <code class="inline"><span class="q">&quot;\n&quot;</span></code>
, do not
need <code class="inline"><a class="l_k" href="../functions/binmode.html">binmode</a></code>.  The rest need it.</p>
<p>When opening a file, it&#39;s usually a bad idea to continue normal execution
if the request failed, so <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> is frequently used in connection with
<code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>.  Even if <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code> won&#39;t do what you want (say, in a CGI script,
where you want to make a nicely formatted error message (but there are
modules that can help with that problem)) you should always check
the return value from opening a file.  The infrequent exception is when
working with an unopened filehandle is actually what you want to do.</p>
<p>As a special case the 3-arg form with a read/write mode and the third
argument being <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$tmp</span><span class="cm">,</span> <span class="q">&quot;+&gt;&quot;</span><span class="cm">,</span> <a class="l_k" href="../functions/undef.html">undef</a><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> ...</li></ol></pre><p>opens a filehandle to an anonymous temporary file.  Also using &quot;+&lt;&quot;
works for symmetry, but you really should consider writing something
to the temporary file first.  You will need to seek() to do the
reading.</p>
<p>Since v5.8.0, perl has built using PerlIO by default.  Unless you&#39;ve
changed this (i.e. Configure -Uuseperlio), you can open file handles to
&quot;in memory&quot; files held in Perl scalars via:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="i">$fh</span><span class="cm">,</span> <span class="q">&#39;&gt;&#39;</span><span class="cm">,</span> \<span class="i">$variable</span><span class="s">)</span> || ..</li></ol></pre><p>Though if you try to re-open <code class="inline"><span class="w">STDOUT</span></code>
 or <code class="inline"><span class="w">STDERR</span></code>
 as an &quot;in memory&quot;
file, you have to close it first:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/close.html">close</a> <span class="w">STDOUT</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&#39;&gt;&#39;</span><span class="cm">,</span> \<span class="i">$variable</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t open STDOUT: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>Examples:</p>
<pre class="verbatim"><ol><li>    $ARTICLE = 100;</li><li>    open ARTICLE or die "Can't find article $ARTICLE: $!\n";</li><li>    while (&lt;ARTICLE&gt;) {...</li><li></li><li>    open(LOG, '&gt;&gt;/usr/spool/news/twitlog');	# (log is reserved)</li><li>    # if the open fails, output is discarded</li><li></li><li>    open(my $dbase, '+&lt;', 'dbase.mine')		# open for update</li><li>	or die "Can't open 'dbase.mine' for update: $!";</li><li></li><li>    open(my $dbase, '+&lt;dbase.mine')			# ditto</li><li>	or die "Can't open 'dbase.mine' for update: $!";</li><li></li><li>    open(ARTICLE, '-|', "caesar &lt;$article")     # decrypt article</li><li>	or die "Can't start caesar: $!";</li><li></li><li>    open(ARTICLE, "caesar &lt;$article |")		# ditto</li><li>	or die "Can't start caesar: $!";</li><li></li><li>    open(EXTRACT, "|sort &gt;Tmp$$")		# $$ is our process id</li><li>	or die "Can't start sort: $!";</li><li></li><li>    # in memory files</li><li>    open(MEMORY,'&gt;', \$var)</li><li>	or die "Can't open memory file: $!";</li><li>    print MEMORY "foo!\n";			# output will end up in $var</li><li></li><li>    # process argument list of files along with any includes</li><li></li><li>    foreach $file (@ARGV) {</li><li>	process($file, 'fh00');</li><li>    }</li><li></li><li>    sub process {</li><li>	my($filename, $input) = @_;</li><li>	$input++;		# this is a string increment</li><li>	unless (open($input, $filename)) {</li><li>	    print STDERR "Can't open $filename: $!\n";</li><li>	    return;</li><li>	}</li><li></li><li>	local $_;</li><li>	while (&lt;$input&gt;) {		# note use of indirection</li><li>	    if (/^#include "(.*)"/) {</li><li>		process($1, $input);</li><li>		next;</li><li>	    }</li><li>	    #...		# whatever</li><li>	}</li><li>    }</li></ol></pre><p>See <a href="../perliol.html">perliol</a> for detailed info on PerlIO.</p>
<p>You may also, in the Bourne shell tradition, specify an EXPR beginning
with <code class="inline"><span class="q">&#39;&gt;&amp;&#39;</span></code>
, in which case the rest of the string is interpreted
as the name of a filehandle (or file descriptor, if numeric) to be
duped (as <code class="inline"><span class="i">dup</span><span class="s">(</span><span class="n">2</span><span class="s">)</span></code>
) and opened.  You may use <code class="inline"><span class="i">&amp;</span></code>
 after <code class="inline">&gt;</code>,
<code class="inline">&gt;&gt;</code>
, <code class="inline">&lt;</code>
, <code class="inline">+&gt;</code>, <code class="inline">+&gt;&gt;</code>
, and <code class="inline">+&lt;</code>
.
The mode you specify should match the mode of the original filehandle.
(Duping a filehandle does not take into account any existing contents
of IO buffers.) If you use the 3-arg form then you can pass either a
number, the name of a filehandle or the normal &quot;reference to a glob&quot;.</p>
<p>Here is a script that saves, redirects, and restores <code class="inline"><span class="w">STDOUT</span></code>
 and
<code class="inline"><span class="w">STDERR</span></code>
 using various methods:</p>
<pre class="verbatim"><ol><li>    <span class="c">#!/usr/bin/perl</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <a class="l_k" href="../functions/my.html">my</a> <span class="i">$oldout</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;STDOUT&quot;</span>     or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup STDOUT: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">OLDERR</span><span class="cm">,</span>     <span class="q">&quot;&gt;&amp;&quot;</span><span class="cm">,</span> \<span class="i">*STDERR</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup STDERR: $!&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&#39;&gt;&#39;</span><span class="cm">,</span> <span class="q">&quot;foo.out&quot;</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t redirect STDOUT: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDERR</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;STDOUT&quot;</span>     or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup STDOUT: $!&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/select.html">select</a> <span class="w">STDERR</span><span class="sc">;</span> <span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span>	<span class="c"># make unbuffered</span></li><li>    <a class="l_k" href="../functions/select.html">select</a> <span class="w">STDOUT</span><span class="sc">;</span> <span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span>	<span class="c"># make unbuffered</span></li><li></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDOUT</span> <span class="q">&quot;stdout 1\n&quot;</span><span class="sc">;</span>	<span class="c"># this works for</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;stderr 1\n&quot;</span><span class="sc">;</span> 	<span class="c"># subprocesses too</span></li><li></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;&quot;</span><span class="cm">,</span> <span class="i">$oldout</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup \$oldout: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDERR</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;OLDERR&quot;</span>    or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup OLDERR: $!&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDOUT</span> <span class="q">&quot;stdout 2\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;stderr 2\n&quot;</span><span class="sc">;</span></li></ol></pre><p>If you specify <code class="inline"><span class="q">&#39;&lt;&amp;=X&#39;</span></code>
, where <code class="inline"><span class="w">X</span></code>
 is a file descriptor number
or a filehandle, then Perl will do an equivalent of C&#39;s <code class="inline"><span class="w">fdopen</span></code>
 of
that file descriptor (and not call <code class="inline"><span class="i">dup</span><span class="s">(</span><span class="n">2</span><span class="s">)</span></code>
); this is more
parsimonious of file descriptors.  For example:</p>
<pre class="verbatim"><ol><li>    <span class="c"># open for input, reusing the fileno of $fd</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FILEHANDLE</span><span class="cm">,</span> <span class="q">&quot;&lt;&amp;=$fd&quot;</span><span class="s">)</span></li></ol></pre><p>or</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FILEHANDLE</span><span class="cm">,</span> <span class="q">&quot;&lt;&amp;=&quot;</span><span class="cm">,</span> <span class="i">$fd</span><span class="s">)</span></li></ol></pre><p>or</p>
<pre class="verbatim"><ol><li>    <span class="c"># open for append, using the fileno of OLDFH</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FH</span><span class="cm">,</span> <span class="q">&quot;&gt;&gt;&amp;=&quot;</span><span class="cm">,</span> <span class="w">OLDFH</span><span class="s">)</span></li></ol></pre><p>or</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FH</span><span class="cm">,</span> <span class="q">&quot;&gt;&gt;&amp;=OLDFH&quot;</span><span class="s">)</span></li></ol></pre><p>Being parsimonious on filehandles is also useful (besides being
parsimonious) for example when something is dependent on file
descriptors, like for example locking using flock().  If you do just
<code class="inline"><a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">A</span><span class="cm">,</span> <span class="q">&#39;&gt;&gt;&amp;B&#39;</span><span class="s">)</span></code>
, the filehandle A will not have the same file
descriptor as B, and therefore flock(A) will not flock(B), and vice
versa.  But with <code class="inline"><a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">A</span><span class="cm">,</span> <span class="q">&#39;&gt;&gt;&amp;=B&#39;</span><span class="s">)</span></code>
 the filehandles will share
the same file descriptor.</p>
<p>Note that if you are using Perls older than 5.8.0, Perl will be using
the standard C libraries&#39; fdopen() to implement the &quot;=&quot; functionality.
On many UNIX systems fdopen() fails when file descriptors exceed a
certain value, typically 255.  For Perls 5.8.0 and later, PerlIO is
most often the default.</p>
<p>You can see whether Perl has been compiled with PerlIO or not by
running <code class="inline"><span class="w">perl</span> -<span class="w">V</span></code>
 and looking for <code class="inline"><span class="w">useperlio</span>=</code>
 line.  If <code class="inline"><span class="w">useperlio</span></code>

is <code class="inline"><span class="w">define</span></code>
, you have PerlIO, otherwise you don&#39;t.</p>
<p>If you open a pipe on the command <code class="inline"><span class="q">&#39;-&#39;</span></code>
, i.e., either <code class="inline"><span class="q">&#39;|-&#39;</span></code>
 or <code class="inline"><span class="q">&#39;-|&#39;</span></code>

with 2-arguments (or 1-argument) form of open(), then
there is an implicit fork done, and the return value of open is the pid
of the child within the parent process, and <code class="inline"><span class="n">0</span></code>
 within the child
process.  (Use <code class="inline"><a class="l_k" href="../functions/defined.html">defined($pid)</a></code> to determine whether the open was successful.)
The filehandle behaves normally for the parent, but i/o to that
filehandle is piped from/to the STDOUT/STDIN of the child process.
In the child process the filehandle isn&#39;t opened--i/o happens from/to
the new STDOUT or STDIN.  Typically this is used like the normal
piped open when you want to exercise more control over just how the
pipe command gets executed, such as when you are running setuid, and
don&#39;t want to have to scan shell commands for metacharacters.
The following triples are more or less equivalent:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&quot;|tr &#39;[a-z]&#39; &#39;[A-Z]&#39;&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;|-&#39;</span><span class="cm">,</span> <span class="q">&quot;tr &#39;[a-z]&#39; &#39;[A-Z]&#39;&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;|-&#39;</span><span class="s">)</span> || <a class="l_k" href="../functions/exec.html">exec</a> <span class="q">&#39;tr&#39;</span><span class="cm">,</span> <span class="q">&#39;[a-z]&#39;</span><span class="cm">,</span> <span class="q">&#39;[A-Z]&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;|-&#39;</span><span class="cm">,</span> <span class="q">&quot;tr&quot;</span><span class="cm">,</span> <span class="q">&#39;[a-z]&#39;</span><span class="cm">,</span> <span class="q">&#39;[A-Z]&#39;</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&quot;cat -n &#39;$file&#39;|&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;-|&#39;</span><span class="cm">,</span> <span class="q">&quot;cat -n &#39;$file&#39;&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;-|&#39;</span><span class="s">)</span> || <a class="l_k" href="../functions/exec.html">exec</a> <span class="q">&#39;cat&#39;</span><span class="cm">,</span> <span class="q">&#39;-n&#39;</span><span class="cm">,</span> <span class="i">$file</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;-|&#39;</span><span class="cm">,</span> <span class="q">&quot;cat&quot;</span><span class="cm">,</span> <span class="q">&#39;-n&#39;</span><span class="cm">,</span> <span class="i">$file</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The last example in each block shows the pipe as &quot;list form&quot;, which is
not yet supported on all platforms.  A good rule of thumb is that if
your platform has true <code class="inline"><a class="l_k" href="../functions/fork.html">fork()</a></code> (in other words, if your platform is
UNIX) you can use the list form.</p>
<p>See <a href="../perlipc.html#Safe-Pipe-Opens">"Safe Pipe Opens" in perlipc</a> for more examples of this.</p>
<p>Beginning with v5.6.0, Perl will attempt to flush all files opened for
output before any operation that may do a fork, but this may not be
supported on some platforms (see <a href="../perlport.html">perlport</a>).  To be safe, you may need
to set <code class="inline"><span class="i">$|</span></code>
 ($AUTOFLUSH in English) or call the <code class="inline"><span class="i">autoflush</span><span class="s">(</span><span class="s">)</span></code>
 method
of <code class="inline"><span class="w">IO::Handle</span></code>
 on any open handles.</p>
<p>On systems that support a close-on-exec flag on files, the flag will
be set for the newly opened file descriptor as determined by the value
of $^F.  See <a href="../perlvar.html#%24%5eF">"$^F" in perlvar</a>.</p>
<p>Closing any piped filehandle causes the parent process to wait for the
child to finish, and returns the status value in <code class="inline"><span class="i">$?</span></code>
 and
<code class="inline"><span class="i">$</span>{<span class="w">^CHILD_ERROR_NATIVE</span>}</code>
.</p>
<p>The filename passed to 2-argument (or 1-argument) form of open() will
have leading and trailing whitespace deleted, and the normal
redirection characters honored.  This property, known as &quot;magic open&quot;,
can often be used to good effect.  A user could specify a filename of
<i>&quot;rsh cat file |&quot;</i>, or you could change certain filenames as needed:</p>
<pre class="verbatim"><ol><li>    <span class="i">$filename</span> =~ <span class="q">s/(.*\.gz)\s*$/gzip -dc &lt; $1|/</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FH</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t open $filename: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>Use 3-argument form to open a file with arbitrary weird characters in it,</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;&lt;&#39;</span><span class="cm">,</span> <span class="i">$file</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>otherwise it&#39;s necessary to protect any leading and trailing whitespace:</p>
<pre class="verbatim"><ol><li>    <span class="i">$file</span> =~ <span class="q">s#^(\s)#./$1#</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&quot;&lt; $file\0&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>(this may not work on some bizarre filesystems).  One should
conscientiously choose between the <i>magic</i> and 3-arguments form
of open():</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">IN</span><span class="cm">,</span> <span class="i">$ARGV</span>[<span class="n">0</span>]<span class="sc">;</span></li></ol></pre><p>will allow the user to specify an argument of the form <code class="inline"><span class="q">&quot;rsh cat file |&quot;</span></code>
,
but will not work on a filename which happens to have a trailing space, while</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">IN</span><span class="cm">,</span> <span class="q">&#39;&lt;&#39;</span><span class="cm">,</span> <span class="i">$ARGV</span>[<span class="n">0</span>]<span class="sc">;</span></li></ol></pre><p>will have exactly the opposite restrictions.</p>
<p>If you want a &quot;real&quot; C <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> (see <code class="inline"><a class="l_k" href="../functions/open.html">open(2)</a></code> on your system), then you
should use the <code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code> function, which involves no such magic (but
may use subtly different filemodes than Perl open(), which is mapped
to C fopen()).  This is
another way to protect your filenames from interpretation.  For example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">IO::Handle</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/sysopen.html">sysopen</a><span class="s">(</span><span class="w">HANDLE</span><span class="cm">,</span> <span class="i">$path</span><span class="cm">,</span> <span class="w">O_RDWR</span>|<span class="w">O_CREAT</span>|<span class="w">O_EXCL</span><span class="s">)</span></li><li>	or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;sysopen $path: $!&quot;</span><span class="sc">;</span></li><li>    <span class="i">$oldfh</span> = <a class="l_k" href="../functions/select.html">select</a><span class="s">(</span><span class="w">HANDLE</span><span class="s">)</span><span class="sc">;</span> <span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span> <a class="l_k" href="../functions/select.html">select</a><span class="s">(</span><span class="i">$oldfh</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">HANDLE</span> <span class="q">&quot;stuff $$\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/seek.html">seek</a><span class="s">(</span><span class="w">HANDLE</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;File contains: &quot;</span><span class="cm">,</span> <span class="q">&lt;HANDLE&gt;</span><span class="sc">;</span></li></ol></pre><p>Using the constructor from the <code class="inline"><span class="w">IO::Handle</span></code>
 package (or one of its
subclasses, such as <code class="inline"><span class="w">IO::File</span></code>
 or <code class="inline"><span class="w">IO::Socket</span></code>
), you can generate anonymous
filehandles that have the scope of whatever variables hold references to
them, and automatically close whenever and however you leave that scope:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">IO::File</span><span class="sc">;</span></li><li>    <span class="c">#...</span></li><li><a name="read_myfile_munged"></a>    sub <span class="m">read_myfile_munged</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="i">$ALL</span> = <a class="l_k" href="../functions/shift.html">shift</a><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="i">$handle</span> = <span class="w">IO::File</span><span class="w">-&gt;new</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="i">$handle</span><span class="cm">,</span> <span class="q">&quot;myfile&quot;</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;myfile: $!&quot;</span><span class="sc">;</span></li><li>	<span class="i">$first</span> = <span class="q">&lt;$handle&gt;</span></li><li>	    or <a class="l_k" href="../functions/return.html">return</a> <span class="s">(</span><span class="s">)</span><span class="sc">;</span>     <span class="c"># Automatically closed here.</span></li><li>	<span class="w">mung</span> <span class="i">$first</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;mung failed&quot;</span><span class="sc">;</span>	<span class="c"># Or here.</span></li><li>	<a class="l_k" href="../functions/return.html">return</a> <span class="i">$first</span><span class="cm">,</span> <span class="q">&lt;$handle&gt;</span> if <span class="i">$ALL</span><span class="sc">;</span>	<span class="c"># Or here.</span></li><li>	<span class="i">$first</span><span class="sc">;</span>					<span class="c"># Or here.</span></li><li>    <span class="s">}</span></li></ol></pre><p>See <a href="../functions/seek.html">"seek"</a> for some details about mixing reading and writing.
-----

function open
filehandle|
Opens the file whose filename is given by EXPR, and associates it with
FILEHANDLE.</p>
<p>Simple examples to open a file for reading:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">'&lt;'</span><span class="cm">,</span> <span class="q">&quot;input.txt&quot;</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="i">$!</span><span class="sc">;</span></li></ol></pre><p>and for writing:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">'&gt;'</span><span class="cm">,</span> <span class="q">&quot;output.txt&quot;</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="i">$!</span><span class="sc">;</span></li></ol></pre><p>(The following is a comprehensive reference to open(): for a gentler
introduction you may consider <a href="../perlopentut.html">perlopentut</a>.)</p>
<p>If FILEHANDLE is an undefined scalar variable (or array or hash element)
the variable is assigned a reference to a new anonymous filehandle,
otherwise if FILEHANDLE is an expression, its value is used as the name of
the real filehandle wanted.  (This is considered a symbolic reference, so
<code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">strict</span> <span class="q">&#39;refs&#39;</span></code>
 should <i>not</i> be in effect.)</p>
<p>If EXPR is omitted, the scalar variable of the same name as the
FILEHANDLE contains the filename.  (Note that lexical variables--those
declared with <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>--will not work for this purpose; so if you&#39;re
using <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>, specify EXPR in your call to open.)</p>
<p>If three or more arguments are specified then the mode of opening and
the file name are separate. If MODE is <code class="inline"><span class="q">&#39;&lt;&#39;</span></code>
 or nothing, the file
is opened for input.  If MODE is <code class="inline"><span class="q">&#39;&gt;&#39;</span></code>
, the file is truncated and
opened for output, being created if necessary.  If MODE is <code class="inline"><span class="q">&#39;&gt;&gt;&#39;</span></code>
,
the file is opened for appending, again being created if necessary.</p>
<p>You can put a <code class="inline"><span class="q">&#39;+&#39;</span></code>
 in front of the <code class="inline"><span class="q">&#39;&gt;&#39;</span></code>
 or <code class="inline"><span class="q">&#39;&lt;&#39;</span></code>
 to
indicate that you want both read and write access to the file; thus
<code class="inline"><span class="q">&#39;+&lt;&#39;</span></code>
 is almost always preferred for read/write updates--the <code class="inline"><span class="q">&#39;+&gt;&#39;</span></code>
 mode would clobber the file first.  You can&#39;t usually use
either read-write mode for updating textfiles, since they have
variable length records.  See the <b>-i</b> switch in <a href="../perlrun.html">perlrun</a> for a
better approach.  The file is created with permissions of <code class="inline"><span class="n">0666</span></code>

modified by the process&#39; <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code> value.</p>
<p>These various prefixes correspond to the fopen(3) modes of <code class="inline"><span class="q">&#39;r&#39;</span></code>
,
<code class="inline"><span class="q">&#39;r+&#39;</span></code>
, <code class="inline"><span class="q">&#39;w&#39;</span></code>
, <code class="inline"><span class="q">&#39;w+&#39;</span></code>
, <code class="inline"><span class="q">&#39;a&#39;</span></code>
, and <code class="inline"><span class="q">&#39;a+&#39;</span></code>
.</p>
<p>In the 2-arguments (and 1-argument) form of the call the mode and
filename should be concatenated (in this order), possibly separated by
spaces.  It is possible to omit the mode in these forms if the mode is
<code class="inline"><span class="q">&#39;&lt;&#39;</span></code>
.</p>
<p>If the filename begins with <code class="inline"><span class="q">&#39;|&#39;</span></code>
, the filename is interpreted as a
command to which output is to be piped, and if the filename ends with a
<code class="inline"><span class="q">&#39;|&#39;</span></code>
, the filename is interpreted as a command which pipes output to
us.  See <a href="../perlipc.html#Using-open()-for-IPC">"Using open() for IPC" in perlipc</a>
for more examples of this.  (You are not allowed to <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> to a command
that pipes both in <i>and</i> out, but see <a href="../IPC/Open2.html">IPC::Open2</a>, <a href="../IPC/Open3.html">IPC::Open3</a>,
and <a href="../perlipc.html#Bidirectional-Communication-with-Another-Process">"Bidirectional Communication with Another Process" in perlipc</a>
for alternatives.)</p>
<p>For three or more arguments if MODE is <code class="inline"><span class="q">&#39;|-&#39;</span></code>
, the filename is
interpreted as a command to which output is to be piped, and if MODE
is <code class="inline"><span class="q">&#39;-|&#39;</span></code>
, the filename is interpreted as a command which pipes
output to us.  In the 2-arguments (and 1-argument) form one should
replace dash (<code class="inline"><span class="q">&#39;-&#39;</span></code>
) with the command.
See <a href="../perlipc.html#Using-open()-for-IPC">"Using open() for IPC" in perlipc</a> for more examples of this.
(You are not allowed to <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> to a command that pipes both in <i>and</i>
out, but see <a href="../IPC/Open2.html">IPC::Open2</a>, <a href="../IPC/Open3.html">IPC::Open3</a>, and
<a href="../perlipc.html#Bidirectional-Communication">"Bidirectional Communication" in perlipc</a> for alternatives.)</p>
<p>In the three-or-more argument form of pipe opens, if LIST is specified
(extra arguments after the command name) then LIST becomes arguments
to the command invoked if the platform supports it.  The meaning of
<code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> with more than three arguments for non-pipe modes is not yet
specified. Experimental &quot;layers&quot; may give extra LIST arguments
meaning.</p>
<p>In the 2-arguments (and 1-argument) form opening <code class="inline"><span class="q">&#39;-&#39;</span></code>
 opens STDIN
and opening <code class="inline"><span class="q">&#39;&gt;-&#39;</span></code>
 opens STDOUT.</p>
<p>You may use the three-argument form of open to specify IO &quot;layers&quot;
(sometimes also referred to as &quot;disciplines&quot;) to be applied to the handle
that affect how the input and output are processed (see <a href="../open.html">open</a> and
<a href="../PerlIO.html">PerlIO</a> for more details). For example</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">&quot;&lt;:encoding(UTF-8)&quot;</span><span class="cm">,</span> <span class="q">&quot;file&quot;</span><span class="s">)</span></li></ol></pre><p>will open the UTF-8 encoded file containing Unicode characters,
see <a href="../perluniintro.html">perluniintro</a>. Note that if layers are specified in the
three-arg form then default layers stored in ${^OPEN} (see <a href="../perlvar.html">perlvar</a>;
usually set by the <b>open</b> pragma or the switch <b>-CioD</b>) are ignored.</p>
<p>Open returns nonzero upon success, the undefined value otherwise.  If
the <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> involved a pipe, the return value happens to be the pid of
the subprocess.</p>
<p>If you&#39;re running Perl on a system that distinguishes between text
files and binary files, then you should check out <a href="../functions/binmode.html">"binmode"</a> for tips
for dealing with this.  The key distinction between systems that need
<code class="inline"><a class="l_k" href="../functions/binmode.html">binmode</a></code> and those that don&#39;t is their text file formats.  Systems
like Unix, Mac OS, and Plan 9, which delimit lines with a single
character, and which encode that character in C as <code class="inline"><span class="q">&quot;\n&quot;</span></code>
, do not
need <code class="inline"><a class="l_k" href="../functions/binmode.html">binmode</a></code>.  The rest need it.</p>
<p>When opening a file, it&#39;s usually a bad idea to continue normal execution
if the request failed, so <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> is frequently used in connection with
<code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>.  Even if <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code> won&#39;t do what you want (say, in a CGI script,
where you want to make a nicely formatted error message (but there are
modules that can help with that problem)) you should always check
the return value from opening a file.  The infrequent exception is when
working with an unopened filehandle is actually what you want to do.</p>
<p>As a special case the 3-arg form with a read/write mode and the third
argument being <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$tmp</span><span class="cm">,</span> <span class="q">&quot;+&gt;&quot;</span><span class="cm">,</span> <a class="l_k" href="../functions/undef.html">undef</a><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> ...</li></ol></pre><p>opens a filehandle to an anonymous temporary file.  Also using &quot;+&lt;&quot;
works for symmetry, but you really should consider writing something
to the temporary file first.  You will need to seek() to do the
reading.</p>
<p>Since v5.8.0, perl has built using PerlIO by default.  Unless you&#39;ve
changed this (i.e. Configure -Uuseperlio), you can open file handles to
&quot;in memory&quot; files held in Perl scalars via:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="i">$fh</span><span class="cm">,</span> <span class="q">&#39;&gt;&#39;</span><span class="cm">,</span> \<span class="i">$variable</span><span class="s">)</span> || ..</li></ol></pre><p>Though if you try to re-open <code class="inline"><span class="w">STDOUT</span></code>
 or <code class="inline"><span class="w">STDERR</span></code>
 as an &quot;in memory&quot;
file, you have to close it first:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/close.html">close</a> <span class="w">STDOUT</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&#39;&gt;&#39;</span><span class="cm">,</span> \<span class="i">$variable</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t open STDOUT: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>Examples:</p>
<pre class="verbatim"><ol><li>    $ARTICLE = 100;</li><li>    open ARTICLE or die "Can't find article $ARTICLE: $!\n";</li><li>    while (&lt;ARTICLE&gt;) {...</li><li></li><li>    open(LOG, '&gt;&gt;/usr/spool/news/twitlog');	# (log is reserved)</li><li>    # if the open fails, output is discarded</li><li></li><li>    open(my $dbase, '+&lt;', 'dbase.mine')		# open for update</li><li>	or die "Can't open 'dbase.mine' for update: $!";</li><li></li><li>    open(my $dbase, '+&lt;dbase.mine')			# ditto</li><li>	or die "Can't open 'dbase.mine' for update: $!";</li><li></li><li>    open(ARTICLE, '-|', "caesar &lt;$article")     # decrypt article</li><li>	or die "Can't start caesar: $!";</li><li></li><li>    open(ARTICLE, "caesar &lt;$article |")		# ditto</li><li>	or die "Can't start caesar: $!";</li><li></li><li>    open(EXTRACT, "|sort &gt;Tmp$$")		# $$ is our process id</li><li>	or die "Can't start sort: $!";</li><li></li><li>    # in memory files</li><li>    open(MEMORY,'&gt;', \$var)</li><li>	or die "Can't open memory file: $!";</li><li>    print MEMORY "foo!\n";			# output will end up in $var</li><li></li><li>    # process argument list of files along with any includes</li><li></li><li>    foreach $file (@ARGV) {</li><li>	process($file, 'fh00');</li><li>    }</li><li></li><li>    sub process {</li><li>	my($filename, $input) = @_;</li><li>	$input++;		# this is a string increment</li><li>	unless (open($input, $filename)) {</li><li>	    print STDERR "Can't open $filename: $!\n";</li><li>	    return;</li><li>	}</li><li></li><li>	local $_;</li><li>	while (&lt;$input&gt;) {		# note use of indirection</li><li>	    if (/^#include "(.*)"/) {</li><li>		process($1, $input);</li><li>		next;</li><li>	    }</li><li>	    #...		# whatever</li><li>	}</li><li>    }</li></ol></pre><p>See <a href="../perliol.html">perliol</a> for detailed info on PerlIO.</p>
<p>You may also, in the Bourne shell tradition, specify an EXPR beginning
with <code class="inline"><span class="q">&#39;&gt;&amp;&#39;</span></code>
, in which case the rest of the string is interpreted
as the name of a filehandle (or file descriptor, if numeric) to be
duped (as <code class="inline"><span class="i">dup</span><span class="s">(</span><span class="n">2</span><span class="s">)</span></code>
) and opened.  You may use <code class="inline"><span class="i">&amp;</span></code>
 after <code class="inline">&gt;</code>,
<code class="inline">&gt;&gt;</code>
, <code class="inline">&lt;</code>
, <code class="inline">+&gt;</code>, <code class="inline">+&gt;&gt;</code>
, and <code class="inline">+&lt;</code>
.
The mode you specify should match the mode of the original filehandle.
(Duping a filehandle does not take into account any existing contents
of IO buffers.) If you use the 3-arg form then you can pass either a
number, the name of a filehandle or the normal &quot;reference to a glob&quot;.</p>
<p>Here is a script that saves, redirects, and restores <code class="inline"><span class="w">STDOUT</span></code>
 and
<code class="inline"><span class="w">STDERR</span></code>
 using various methods:</p>
<pre class="verbatim"><ol><li>    <span class="c">#!/usr/bin/perl</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <a class="l_k" href="../functions/my.html">my</a> <span class="i">$oldout</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;STDOUT&quot;</span>     or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup STDOUT: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">OLDERR</span><span class="cm">,</span>     <span class="q">&quot;&gt;&amp;&quot;</span><span class="cm">,</span> \<span class="i">*STDERR</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup STDERR: $!&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&#39;&gt;&#39;</span><span class="cm">,</span> <span class="q">&quot;foo.out&quot;</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t redirect STDOUT: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDERR</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;STDOUT&quot;</span>     or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup STDOUT: $!&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/select.html">select</a> <span class="w">STDERR</span><span class="sc">;</span> <span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span>	<span class="c"># make unbuffered</span></li><li>    <a class="l_k" href="../functions/select.html">select</a> <span class="w">STDOUT</span><span class="sc">;</span> <span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span>	<span class="c"># make unbuffered</span></li><li></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDOUT</span> <span class="q">&quot;stdout 1\n&quot;</span><span class="sc">;</span>	<span class="c"># this works for</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;stderr 1\n&quot;</span><span class="sc">;</span> 	<span class="c"># subprocesses too</span></li><li></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDOUT</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;&quot;</span><span class="cm">,</span> <span class="i">$oldout</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup \$oldout: $!&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">STDERR</span><span class="cm">,</span> <span class="q">&quot;&gt;&amp;OLDERR&quot;</span>    or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t dup OLDERR: $!&quot;</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDOUT</span> <span class="q">&quot;stdout 2\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;stderr 2\n&quot;</span><span class="sc">;</span></li></ol></pre><p>If you specify <code class="inline"><span class="q">&#39;&lt;&amp;=X&#39;</span></code>
, where <code class="inline"><span class="w">X</span></code>
 is a file descriptor number
or a filehandle, then Perl will do an equivalent of C&#39;s <code class="inline"><span class="w">fdopen</span></code>
 of
that file descriptor (and not call <code class="inline"><span class="i">dup</span><span class="s">(</span><span class="n">2</span><span class="s">)</span></code>
); this is more
parsimonious of file descriptors.  For example:</p>
<pre class="verbatim"><ol><li>    <span class="c"># open for input, reusing the fileno of $fd</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FILEHANDLE</span><span class="cm">,</span> <span class="q">&quot;&lt;&amp;=$fd&quot;</span><span class="s">)</span></li></ol></pre><p>or</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FILEHANDLE</span><span class="cm">,</span> <span class="q">&quot;&lt;&amp;=&quot;</span><span class="cm">,</span> <span class="i">$fd</span><span class="s">)</span></li></ol></pre><p>or</p>
<pre class="verbatim"><ol><li>    <span class="c"># open for append, using the fileno of OLDFH</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FH</span><span class="cm">,</span> <span class="q">&quot;&gt;&gt;&amp;=&quot;</span><span class="cm">,</span> <span class="w">OLDFH</span><span class="s">)</span></li></ol></pre><p>or</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FH</span><span class="cm">,</span> <span class="q">&quot;&gt;&gt;&amp;=OLDFH&quot;</span><span class="s">)</span></li></ol></pre><p>Being parsimonious on filehandles is also useful (besides being
parsimonious) for example when something is dependent on file
descriptors, like for example locking using flock().  If you do just
<code class="inline"><a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">A</span><span class="cm">,</span> <span class="q">&#39;&gt;&gt;&amp;B&#39;</span><span class="s">)</span></code>
, the filehandle A will not have the same file
descriptor as B, and therefore flock(A) will not flock(B), and vice
versa.  But with <code class="inline"><a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">A</span><span class="cm">,</span> <span class="q">&#39;&gt;&gt;&amp;=B&#39;</span><span class="s">)</span></code>
 the filehandles will share
the same file descriptor.</p>
<p>Note that if you are using Perls older than 5.8.0, Perl will be using
the standard C libraries&#39; fdopen() to implement the &quot;=&quot; functionality.
On many UNIX systems fdopen() fails when file descriptors exceed a
certain value, typically 255.  For Perls 5.8.0 and later, PerlIO is
most often the default.</p>
<p>You can see whether Perl has been compiled with PerlIO or not by
running <code class="inline"><span class="w">perl</span> -<span class="w">V</span></code>
 and looking for <code class="inline"><span class="w">useperlio</span>=</code>
 line.  If <code class="inline"><span class="w">useperlio</span></code>

is <code class="inline"><span class="w">define</span></code>
, you have PerlIO, otherwise you don&#39;t.</p>
<p>If you open a pipe on the command <code class="inline"><span class="q">&#39;-&#39;</span></code>
, i.e., either <code class="inline"><span class="q">&#39;|-&#39;</span></code>
 or <code class="inline"><span class="q">&#39;-|&#39;</span></code>

with 2-arguments (or 1-argument) form of open(), then
there is an implicit fork done, and the return value of open is the pid
of the child within the parent process, and <code class="inline"><span class="n">0</span></code>
 within the child
process.  (Use <code class="inline"><a class="l_k" href="../functions/defined.html">defined($pid)</a></code> to determine whether the open was successful.)
The filehandle behaves normally for the parent, but i/o to that
filehandle is piped from/to the STDOUT/STDIN of the child process.
In the child process the filehandle isn&#39;t opened--i/o happens from/to
the new STDOUT or STDIN.  Typically this is used like the normal
piped open when you want to exercise more control over just how the
pipe command gets executed, such as when you are running setuid, and
don&#39;t want to have to scan shell commands for metacharacters.
The following triples are more or less equivalent:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&quot;|tr &#39;[a-z]&#39; &#39;[A-Z]&#39;&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;|-&#39;</span><span class="cm">,</span> <span class="q">&quot;tr &#39;[a-z]&#39; &#39;[A-Z]&#39;&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;|-&#39;</span><span class="s">)</span> || <a class="l_k" href="../functions/exec.html">exec</a> <span class="q">&#39;tr&#39;</span><span class="cm">,</span> <span class="q">&#39;[a-z]&#39;</span><span class="cm">,</span> <span class="q">&#39;[A-Z]&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;|-&#39;</span><span class="cm">,</span> <span class="q">&quot;tr&quot;</span><span class="cm">,</span> <span class="q">&#39;[a-z]&#39;</span><span class="cm">,</span> <span class="q">&#39;[A-Z]&#39;</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&quot;cat -n &#39;$file&#39;|&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;-|&#39;</span><span class="cm">,</span> <span class="q">&quot;cat -n &#39;$file&#39;&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;-|&#39;</span><span class="s">)</span> || <a class="l_k" href="../functions/exec.html">exec</a> <span class="q">&#39;cat&#39;</span><span class="cm">,</span> <span class="q">&#39;-n&#39;</span><span class="cm">,</span> <span class="i">$file</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;-|&#39;</span><span class="cm">,</span> <span class="q">&quot;cat&quot;</span><span class="cm">,</span> <span class="q">&#39;-n&#39;</span><span class="cm">,</span> <span class="i">$file</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The last example in each block shows the pipe as &quot;list form&quot;, which is
not yet supported on all platforms.  A good rule of thumb is that if
your platform has true <code class="inline"><a class="l_k" href="../functions/fork.html">fork()</a></code> (in other words, if your platform is
UNIX) you can use the list form.</p>
<p>See <a href="../perlipc.html#Safe-Pipe-Opens">"Safe Pipe Opens" in perlipc</a> for more examples of this.</p>
<p>Beginning with v5.6.0, Perl will attempt to flush all files opened for
output before any operation that may do a fork, but this may not be
supported on some platforms (see <a href="../perlport.html">perlport</a>).  To be safe, you may need
to set <code class="inline"><span class="i">$|</span></code>
 ($AUTOFLUSH in English) or call the <code class="inline"><span class="i">autoflush</span><span class="s">(</span><span class="s">)</span></code>
 method
of <code class="inline"><span class="w">IO::Handle</span></code>
 on any open handles.</p>
<p>On systems that support a close-on-exec flag on files, the flag will
be set for the newly opened file descriptor as determined by the value
of $^F.  See <a href="../perlvar.html#%24%5eF">"$^F" in perlvar</a>.</p>
<p>Closing any piped filehandle causes the parent process to wait for the
child to finish, and returns the status value in <code class="inline"><span class="i">$?</span></code>
 and
<code class="inline"><span class="i">$</span>{<span class="w">^CHILD_ERROR_NATIVE</span>}</code>
.</p>
<p>The filename passed to 2-argument (or 1-argument) form of open() will
have leading and trailing whitespace deleted, and the normal
redirection characters honored.  This property, known as &quot;magic open&quot;,
can often be used to good effect.  A user could specify a filename of
<i>&quot;rsh cat file |&quot;</i>, or you could change certain filenames as needed:</p>
<pre class="verbatim"><ol><li>    <span class="i">$filename</span> =~ <span class="q">s/(.*\.gz)\s*$/gzip -dc &lt; $1|/</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FH</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t open $filename: $!&quot;</span><span class="sc">;</span></li></ol></pre><p>Use 3-argument form to open a file with arbitrary weird characters in it,</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&#39;&lt;&#39;</span><span class="cm">,</span> <span class="i">$file</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>otherwise it&#39;s necessary to protect any leading and trailing whitespace:</p>
<pre class="verbatim"><ol><li>    <span class="i">$file</span> =~ <span class="q">s#^(\s)#./$1#</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span> <span class="q">&quot;&lt; $file\0&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>(this may not work on some bizarre filesystems).  One should
conscientiously choose between the <i>magic</i> and 3-arguments form
of open():</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">IN</span><span class="cm">,</span> <span class="i">$ARGV</span>[<span class="n">0</span>]<span class="sc">;</span></li></ol></pre><p>will allow the user to specify an argument of the form <code class="inline"><span class="q">&quot;rsh cat file |&quot;</span></code>
,
but will not work on a filename which happens to have a trailing space, while</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a> <span class="w">IN</span><span class="cm">,</span> <span class="q">&#39;&lt;&#39;</span><span class="cm">,</span> <span class="i">$ARGV</span>[<span class="n">0</span>]<span class="sc">;</span></li></ol></pre><p>will have exactly the opposite restrictions.</p>
<p>If you want a &quot;real&quot; C <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> (see <code class="inline"><a class="l_k" href="../functions/open.html">open(2)</a></code> on your system), then you
should use the <code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code> function, which involves no such magic (but
may use subtly different filemodes than Perl open(), which is mapped
to C fopen()).  This is
another way to protect your filenames from interpretation.  For example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">IO::Handle</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/sysopen.html">sysopen</a><span class="s">(</span><span class="w">HANDLE</span><span class="cm">,</span> <span class="i">$path</span><span class="cm">,</span> <span class="w">O_RDWR</span>|<span class="w">O_CREAT</span>|<span class="w">O_EXCL</span><span class="s">)</span></li><li>	or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;sysopen $path: $!&quot;</span><span class="sc">;</span></li><li>    <span class="i">$oldfh</span> = <a class="l_k" href="../functions/select.html">select</a><span class="s">(</span><span class="w">HANDLE</span><span class="s">)</span><span class="sc">;</span> <span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span> <a class="l_k" href="../functions/select.html">select</a><span class="s">(</span><span class="i">$oldfh</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">HANDLE</span> <span class="q">&quot;stuff $$\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/seek.html">seek</a><span class="s">(</span><span class="w">HANDLE</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;File contains: &quot;</span><span class="cm">,</span> <span class="q">&lt;HANDLE&gt;</span><span class="sc">;</span></li></ol></pre><p>Using the constructor from the <code class="inline"><span class="w">IO::Handle</span></code>
 package (or one of its
subclasses, such as <code class="inline"><span class="w">IO::File</span></code>
 or <code class="inline"><span class="w">IO::Socket</span></code>
), you can generate anonymous
filehandles that have the scope of whatever variables hold references to
them, and automatically close whenever and however you leave that scope:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">IO::File</span><span class="sc">;</span></li><li>    <span class="c">#...</span></li><li><a name="read_myfile_munged"></a>    sub <span class="m">read_myfile_munged</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="i">$ALL</span> = <a class="l_k" href="../functions/shift.html">shift</a><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="i">$handle</span> = <span class="w">IO::File</span><span class="w">-&gt;new</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="i">$handle</span><span class="cm">,</span> <span class="q">&quot;myfile&quot;</span><span class="s">)</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;myfile: $!&quot;</span><span class="sc">;</span></li><li>	<span class="i">$first</span> = <span class="q">&lt;$handle&gt;</span></li><li>	    or <a class="l_k" href="../functions/return.html">return</a> <span class="s">(</span><span class="s">)</span><span class="sc">;</span>     <span class="c"># Automatically closed here.</span></li><li>	<span class="w">mung</span> <span class="i">$first</span> or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;mung failed&quot;</span><span class="sc">;</span>	<span class="c"># Or here.</span></li><li>	<a class="l_k" href="../functions/return.html">return</a> <span class="i">$first</span><span class="cm">,</span> <span class="q">&lt;$handle&gt;</span> if <span class="i">$ALL</span><span class="sc">;</span>	<span class="c"># Or here.</span></li><li>	<span class="i">$first</span><span class="sc">;</span>					<span class="c"># Or here.</span></li><li>    <span class="s">}</span></li></ol></pre><p>See <a href="../functions/seek.html">"seek"</a> for some details about mixing reading and writing.
-----

function opendir
dirhandle|
expr|
Opens a directory named EXPR for processing by <code class="inline"><a class="l_k" href="../functions/readdir.html">readdir</a></code>, <code class="inline"><a class="l_k" href="../functions/telldir.html">telldir</a></code>,
<code class="inline"><a class="l_k" href="../functions/seekdir.html">seekdir</a></code>, <code class="inline"><a class="l_k" href="../functions/rewinddir.html">rewinddir</a></code>, and <code class="inline"><a class="l_k" href="../functions/closedir.html">closedir</a></code>.  Returns true if successful.
DIRHANDLE may be an expression whose value can be used as an indirect
dirhandle, usually the real dirhandle name.  If DIRHANDLE is an undefined
scalar variable (or array or hash element), the variable is assigned a
reference to a new anonymous dirhandle.
DIRHANDLEs have their own namespace separate from FILEHANDLEs.</p>
<p>See example at <code class="inline"><a class="l_k" href="../functions/readdir.html">readdir</a></code>.
-----

function ord
expr|
Returns the numeric (the native 8-bit encoding, like ASCII or EBCDIC,
or Unicode) value of the first character of EXPR.  If EXPR is omitted,
uses <code class="inline"><span class="i">$_</span></code>
.</p>
<p>For the reverse, see <a href="../functions/chr.html">"chr"</a>.
See <a href="../perlunicode.html">perlunicode</a> for more about Unicode.
-----

function ord
Returns the numeric (the native 8-bit encoding, like ASCII or EBCDIC,
or Unicode) value of the first character of EXPR.  If EXPR is omitted,
uses <code class="inline"><span class="i">$_</span></code>
.</p>
<p>For the reverse, see <a href="../functions/chr.html">"chr"</a>.
See <a href="../perlunicode.html">perlunicode</a> for more about Unicode.
-----

function our
expr|
<code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> associates a simple name with a package variable in the current
package for use within the current scope.  When <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">strict</span> <span class="q">&#39;vars&#39;</span></code>
 is in
effect, <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> lets you use declared global variables without qualifying
them with package names, within the lexical scope of the <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declaration.
In this way <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> differs from <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">vars</span></code>
, which is package scoped.</p>
<p>Unlike <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>, which both allocates storage for a variable and associates
a simple name with that storage for use within the current scope, <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code>
associates a simple name with a package variable in the current package,
for use within the current scope.  In other words, <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> has the same
scoping rules as <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>, but does not necessarily create a
variable.</p>
<p>If more than one value is listed, the list must be placed
in parentheses.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$foo</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/our.html">our</a><span class="s">(</span><span class="i">$bar</span><span class="cm">,</span> <span class="i">$baz</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>An <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declaration declares a global variable that will be visible
across its entire lexical scope, even across package boundaries.  The
package in which the variable is entered is determined at the point
of the declaration, not at the point of use.  This means the following
behavior holds:</p>
<pre class="verbatim"><ol><li><a name="package-Foo"></a>    package <span class="i">Foo</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># declares $Foo::bar for rest of lexical scope</span></li><li>    <span class="i">$bar</span> = <span class="n">20</span><span class="sc">;</span></li><li></li><li><a name="package-Bar"></a>    package <span class="i">Bar</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># prints 20, as it refers to $Foo::bar</span></li></ol></pre><p>Multiple <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declarations with the same name in the same lexical
scope are allowed if they are in different packages.  If they happen
to be in the same package, Perl will emit warnings if you have asked
for them, just like multiple <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> declarations.  Unlike a second
<code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> declaration, which will bind the name to a fresh variable, a
second <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declaration in the same package, in the same scope, is
merely redundant.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li><a name="package-Foo"></a>    package <span class="i">Foo</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># declares $Foo::bar for rest of lexical scope</span></li><li>    <span class="i">$bar</span> = <span class="n">20</span><span class="sc">;</span></li><li></li><li><a name="package-Bar"></a>    package <span class="i">Bar</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$bar</span> = <span class="n">30</span><span class="sc">;</span>	<span class="c"># declares $Bar::bar for rest of lexical scope</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># prints 30</span></li><li></li><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># emits warning but has no other effect</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># still prints 30</span></li></ol></pre><p>An <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declaration may also have a list of attributes associated
with it.</p>
<p>The exact semantics and interface of TYPE and ATTRS are still
evolving.  TYPE is currently bound to the use of <code class="inline"><span class="w">fields</span></code>
 pragma,
and attributes are handled using the <code class="inline"><span class="w">attributes</span></code>
 pragma, or starting
from Perl 5.8.0 also via the <code class="inline"><span class="w">Attribute::Handlers</span></code>
 module.  See
<a href="../perlsub.html#Private-Variables-via-my()">"Private Variables via my()" in perlsub</a> for details, and <a href="../fields.html">fields</a>,
<a href="../attributes.html">attributes</a>, and <a href="../Attribute/Handlers.html">Attribute::Handlers</a>.
-----

function our
type expr|
<code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> associates a simple name with a package variable in the current
package for use within the current scope.  When <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">strict</span> <span class="q">&#39;vars&#39;</span></code>
 is in
effect, <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> lets you use declared global variables without qualifying
them with package names, within the lexical scope of the <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declaration.
In this way <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> differs from <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">vars</span></code>
, which is package scoped.</p>
<p>Unlike <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>, which both allocates storage for a variable and associates
a simple name with that storage for use within the current scope, <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code>
associates a simple name with a package variable in the current package,
for use within the current scope.  In other words, <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> has the same
scoping rules as <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>, but does not necessarily create a
variable.</p>
<p>If more than one value is listed, the list must be placed
in parentheses.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$foo</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/our.html">our</a><span class="s">(</span><span class="i">$bar</span><span class="cm">,</span> <span class="i">$baz</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>An <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declaration declares a global variable that will be visible
across its entire lexical scope, even across package boundaries.  The
package in which the variable is entered is determined at the point
of the declaration, not at the point of use.  This means the following
behavior holds:</p>
<pre class="verbatim"><ol><li><a name="package-Foo"></a>    package <span class="i">Foo</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># declares $Foo::bar for rest of lexical scope</span></li><li>    <span class="i">$bar</span> = <span class="n">20</span><span class="sc">;</span></li><li></li><li><a name="package-Bar"></a>    package <span class="i">Bar</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># prints 20, as it refers to $Foo::bar</span></li></ol></pre><p>Multiple <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declarations with the same name in the same lexical
scope are allowed if they are in different packages.  If they happen
to be in the same package, Perl will emit warnings if you have asked
for them, just like multiple <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> declarations.  Unlike a second
<code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> declaration, which will bind the name to a fresh variable, a
second <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declaration in the same package, in the same scope, is
merely redundant.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li><a name="package-Foo"></a>    package <span class="i">Foo</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># declares $Foo::bar for rest of lexical scope</span></li><li>    <span class="i">$bar</span> = <span class="n">20</span><span class="sc">;</span></li><li></li><li><a name="package-Bar"></a>    package <span class="i">Bar</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$bar</span> = <span class="n">30</span><span class="sc">;</span>	<span class="c"># declares $Bar::bar for rest of lexical scope</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># prints 30</span></li><li></li><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># emits warning but has no other effect</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># still prints 30</span></li></ol></pre><p>An <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declaration may also have a list of attributes associated
with it.</p>
<p>The exact semantics and interface of TYPE and ATTRS are still
evolving.  TYPE is currently bound to the use of <code class="inline"><span class="w">fields</span></code>
 pragma,
and attributes are handled using the <code class="inline"><span class="w">attributes</span></code>
 pragma, or starting
from Perl 5.8.0 also via the <code class="inline"><span class="w">Attribute::Handlers</span></code>
 module.  See
<a href="../perlsub.html#Private-Variables-via-my()">"Private Variables via my()" in perlsub</a> for details, and <a href="../fields.html">fields</a>,
<a href="../attributes.html">attributes</a>, and <a href="../Attribute/Handlers.html">Attribute::Handlers</a>.
-----

function our
expr : attrs|
<code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> associates a simple name with a package variable in the current
package for use within the current scope.  When <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">strict</span> <span class="q">&#39;vars&#39;</span></code>
 is in
effect, <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> lets you use declared global variables without qualifying
them with package names, within the lexical scope of the <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declaration.
In this way <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> differs from <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">vars</span></code>
, which is package scoped.</p>
<p>Unlike <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>, which both allocates storage for a variable and associates
a simple name with that storage for use within the current scope, <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code>
associates a simple name with a package variable in the current package,
for use within the current scope.  In other words, <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> has the same
scoping rules as <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>, but does not necessarily create a
variable.</p>
<p>If more than one value is listed, the list must be placed
in parentheses.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$foo</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/our.html">our</a><span class="s">(</span><span class="i">$bar</span><span class="cm">,</span> <span class="i">$baz</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>An <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declaration declares a global variable that will be visible
across its entire lexical scope, even across package boundaries.  The
package in which the variable is entered is determined at the point
of the declaration, not at the point of use.  This means the following
behavior holds:</p>
<pre class="verbatim"><ol><li><a name="package-Foo"></a>    package <span class="i">Foo</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># declares $Foo::bar for rest of lexical scope</span></li><li>    <span class="i">$bar</span> = <span class="n">20</span><span class="sc">;</span></li><li></li><li><a name="package-Bar"></a>    package <span class="i">Bar</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># prints 20, as it refers to $Foo::bar</span></li></ol></pre><p>Multiple <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declarations with the same name in the same lexical
scope are allowed if they are in different packages.  If they happen
to be in the same package, Perl will emit warnings if you have asked
for them, just like multiple <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> declarations.  Unlike a second
<code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> declaration, which will bind the name to a fresh variable, a
second <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declaration in the same package, in the same scope, is
merely redundant.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li><a name="package-Foo"></a>    package <span class="i">Foo</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># declares $Foo::bar for rest of lexical scope</span></li><li>    <span class="i">$bar</span> = <span class="n">20</span><span class="sc">;</span></li><li></li><li><a name="package-Bar"></a>    package <span class="i">Bar</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$bar</span> = <span class="n">30</span><span class="sc">;</span>	<span class="c"># declares $Bar::bar for rest of lexical scope</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># prints 30</span></li><li></li><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># emits warning but has no other effect</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># still prints 30</span></li></ol></pre><p>An <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declaration may also have a list of attributes associated
with it.</p>
<p>The exact semantics and interface of TYPE and ATTRS are still
evolving.  TYPE is currently bound to the use of <code class="inline"><span class="w">fields</span></code>
 pragma,
and attributes are handled using the <code class="inline"><span class="w">attributes</span></code>
 pragma, or starting
from Perl 5.8.0 also via the <code class="inline"><span class="w">Attribute::Handlers</span></code>
 module.  See
<a href="../perlsub.html#Private-Variables-via-my()">"Private Variables via my()" in perlsub</a> for details, and <a href="../fields.html">fields</a>,
<a href="../attributes.html">attributes</a>, and <a href="../Attribute/Handlers.html">Attribute::Handlers</a>.
-----

function our
type expr : attrs|
<code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> associates a simple name with a package variable in the current
package for use within the current scope.  When <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">strict</span> <span class="q">&#39;vars&#39;</span></code>
 is in
effect, <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> lets you use declared global variables without qualifying
them with package names, within the lexical scope of the <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declaration.
In this way <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> differs from <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">vars</span></code>
, which is package scoped.</p>
<p>Unlike <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>, which both allocates storage for a variable and associates
a simple name with that storage for use within the current scope, <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code>
associates a simple name with a package variable in the current package,
for use within the current scope.  In other words, <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> has the same
scoping rules as <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>, but does not necessarily create a
variable.</p>
<p>If more than one value is listed, the list must be placed
in parentheses.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$foo</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/our.html">our</a><span class="s">(</span><span class="i">$bar</span><span class="cm">,</span> <span class="i">$baz</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>An <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declaration declares a global variable that will be visible
across its entire lexical scope, even across package boundaries.  The
package in which the variable is entered is determined at the point
of the declaration, not at the point of use.  This means the following
behavior holds:</p>
<pre class="verbatim"><ol><li><a name="package-Foo"></a>    package <span class="i">Foo</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># declares $Foo::bar for rest of lexical scope</span></li><li>    <span class="i">$bar</span> = <span class="n">20</span><span class="sc">;</span></li><li></li><li><a name="package-Bar"></a>    package <span class="i">Bar</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># prints 20, as it refers to $Foo::bar</span></li></ol></pre><p>Multiple <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declarations with the same name in the same lexical
scope are allowed if they are in different packages.  If they happen
to be in the same package, Perl will emit warnings if you have asked
for them, just like multiple <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> declarations.  Unlike a second
<code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> declaration, which will bind the name to a fresh variable, a
second <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declaration in the same package, in the same scope, is
merely redundant.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li><a name="package-Foo"></a>    package <span class="i">Foo</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># declares $Foo::bar for rest of lexical scope</span></li><li>    <span class="i">$bar</span> = <span class="n">20</span><span class="sc">;</span></li><li></li><li><a name="package-Bar"></a>    package <span class="i">Bar</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$bar</span> = <span class="n">30</span><span class="sc">;</span>	<span class="c"># declares $Bar::bar for rest of lexical scope</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># prints 30</span></li><li></li><li>    <a class="l_k" href="../functions/our.html">our</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># emits warning but has no other effect</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="i">$bar</span><span class="sc">;</span>		<span class="c"># still prints 30</span></li></ol></pre><p>An <code class="inline"><a class="l_k" href="../functions/our.html">our</a></code> declaration may also have a list of attributes associated
with it.</p>
<p>The exact semantics and interface of TYPE and ATTRS are still
evolving.  TYPE is currently bound to the use of <code class="inline"><span class="w">fields</span></code>
 pragma,
and attributes are handled using the <code class="inline"><span class="w">attributes</span></code>
 pragma, or starting
from Perl 5.8.0 also via the <code class="inline"><span class="w">Attribute::Handlers</span></code>
 module.  See
<a href="../perlsub.html#Private-Variables-via-my()">"Private Variables via my()" in perlsub</a> for details, and <a href="../fields.html">fields</a>,
<a href="../attributes.html">attributes</a>, and <a href="../Attribute/Handlers.html">Attribute::Handlers</a>.
-----

function pack
template|
list|
Takes a LIST of values and converts it into a string using the rules
given by the TEMPLATE.  The resulting string is the concatenation of
the converted values.  Typically, each converted value looks
like its machine-level representation.  For example, on 32-bit machines
an integer may be represented by a sequence of 4 bytes that will be 
converted to a sequence of 4 characters.</p>
<p>The TEMPLATE is a sequence of characters that give the order and type
of values, as follows:</p>
<pre class="verbatim"><ol><li>    a	A string with arbitrary binary data, will be null padded.</li><li>    A	A text (ASCII) string, will be space padded.</li><li>    Z	A null terminated (ASCIZ) string, will be null padded.</li><li></li><li>    b	A bit string (ascending bit order inside each byte, like vec()).</li><li>    B	A bit string (descending bit order inside each byte).</li><li>    h	A hex string (low nybble first).</li><li>    H	A hex string (high nybble first).</li><li></li><li>    c	A signed char (8-bit) value.</li><li>    C	An unsigned char (octet) value.</li><li>    W   An unsigned char value (can be greater than 255).</li><li></li><li>    s	A signed short (16-bit) value.</li><li>    S	An unsigned short value.</li><li></li><li>    l	A signed long (32-bit) value.</li><li>    L	An unsigned long value.</li><li></li><li>    q	A signed quad (64-bit) value.</li><li>    Q	An unsigned quad value.</li><li>	  (Quads are available only if your system supports 64-bit</li><li>	   integer values _and_ if Perl has been compiled to support those.</li><li>           Causes a fatal error otherwise.)</li><li></li><li>    i	A signed integer value.</li><li>    I	A unsigned integer value.</li><li>	  (This 'integer' is _at_least_ 32 bits wide.  Its exact</li><li>           size depends on what a local C compiler calls 'int'.)</li><li></li><li>    n	An unsigned short (16-bit) in "network" (big-endian) order.</li><li>    N	An unsigned long (32-bit) in "network" (big-endian) order.</li><li>    v	An unsigned short (16-bit) in "VAX" (little-endian) order.</li><li>    V	An unsigned long (32-bit) in "VAX" (little-endian) order.</li><li></li><li>    j   A Perl internal signed integer value (IV).</li><li>    J   A Perl internal unsigned integer value (UV).</li><li></li><li>    f	A single-precision float in the native format.</li><li>    d	A double-precision float in the native format.</li><li></li><li>    F	A Perl internal floating point value (NV) in the native format</li><li>    D	A long double-precision float in the native format.</li><li>	  (Long doubles are available only if your system supports long</li><li>	   double values _and_ if Perl has been compiled to support those.</li><li>           Causes a fatal error otherwise.)</li><li></li><li>    p	A pointer to a null-terminated string.</li><li>    P	A pointer to a structure (fixed-length string).</li><li></li><li>    u	A uuencoded string.</li><li>    U	A Unicode character number.  Encodes to a character in character mode</li><li>        and UTF-8 (or UTF-EBCDIC in EBCDIC platforms) in byte mode.</li><li></li><li>    w	A BER compressed integer (not an ASN.1 BER, see perlpacktut for</li><li>	details).  Its bytes represent an unsigned integer in base 128,</li><li>	most significant digit first, with as few digits as possible.  Bit</li><li>	eight (the high bit) is set on each byte except the last.</li><li></li><li>    x	A null byte.</li><li>    X	Back up a byte.</li><li>    @	Null fill or truncate to absolute position, counted from the</li><li>        start of the innermost ()-group.</li><li>    .   Null fill or truncate to absolute position specified by value.</li><li>    (	Start of a ()-group.</li></ol></pre><p>One or more of the modifiers below may optionally follow some letters in the
TEMPLATE (the second column lists the letters for which the modifier is
valid):</p>
<pre class="verbatim"><ol><li>    !   sSlLiI     Forces native (short, long, int) sizes instead</li><li>                   of fixed (16-/32-bit) sizes.</li><li></li><li>        xX         Make x and X act as alignment commands.</li><li></li><li>        nNvV       Treat integers as signed instead of unsigned.</li><li></li><li>        @.         Specify position as byte offset in the internal</li><li>                   representation of the packed string. Efficient but</li><li>                   dangerous.</li><li></li><li>    &gt;   sSiIlLqQ   Force big-endian byte-order on the type.</li><li>        jJfFdDpP   (The "big end" touches the construct.)</li><li></li><li>    &lt;   sSiIlLqQ   Force little-endian byte-order on the type.</li><li>        jJfFdDpP   (The "little end" touches the construct.)</li></ol></pre><p>The <code class="inline">&gt;</code> and <code class="inline">&lt;</code>
 modifiers can also be used on <code class="inline"><span class="s">(</span><span class="s">)</span></code>
-groups,
in which case they force a certain byte-order on all components of
that group, including subgroups.</p>
<p>The following rules apply:</p>
<ul>
<li>
<p>Each letter may optionally be followed by a number giving a repeat
count.  With all types except <code class="inline"><span class="w">a</span></code>
, <code class="inline"><span class="w">A</span></code>
, <code class="inline"><span class="w">Z</span></code>
, <code class="inline"><span class="w">b</span></code>
, <code class="inline"><span class="w">B</span></code>
, <code class="inline"><span class="w">h</span></code>
,
<code class="inline"><span class="w">H</span></code>
, <code class="inline"><span class="i">@</span></code>
, <code class="inline">.</code>, <code class="inline"><span class="w">x</span></code>
, <code class="inline"><span class="w">X</span></code>
 and <code class="inline"><span class="w">P</span></code>
 the pack function will gobble up
that many values from the LIST.  A <code class="inline"><span class="i">*</span></code>
 for the repeat count means to
use however many items are left, except for <code class="inline"><span class="i">@</span></code>
, <code class="inline"><span class="w">x</span></code>
, <code class="inline"><span class="w">X</span></code>
, where it
is equivalent to <code class="inline"><span class="n">0</span></code>
, for &lt;.&gt; where it means relative to string start
and <code class="inline"><span class="w">u</span></code>
, where it is equivalent to 1 (or 45, which is the same).
A numeric repeat count may optionally be enclosed in brackets, as in
<code class="inline"><a class="l_k" href="../functions/pack.html">pack</a> <span class="q">&#39;C[80]&#39;</span><span class="cm">,</span> <span class="i">@arr</span></code>
.</p>
<p>One can replace the numeric repeat count by a template enclosed in brackets;
then the packed length of this template in bytes is used as a count.
For example, <code class="inline"><span class="w">x</span><span class="s">[</span><span class="w">L</span><span class="s">]</span></code>
 skips a long (it skips the number of bytes in a long);
the template <code class="inline">$t X[$t] $t</code> unpack()s twice what $t unpacks.
If the template in brackets contains alignment commands (such as <code class="inline"><span class="w">x</span>!<span class="s">[</span><span class="w">d</span><span class="s">]</span></code>
),
its packed length is calculated as if the start of the template has the maximal
possible alignment.</p>
<p>When used with <code class="inline"><span class="w">Z</span></code>
, <code class="inline"><span class="i">*</span></code>
 results in the addition of a trailing null
byte (so the packed result will be one longer than the byte <code class="inline"><a class="l_k" href="../functions/length.html">length</a></code>
of the item).</p>
<p>When used with <code class="inline"><span class="i">@</span></code>
, the repeat count represents an offset from the start
of the innermost () group.</p>
<p>When used with <code class="inline">.</code>, the repeat count is used to determine the starting
position from where the value offset is calculated. If the repeat count
is 0, it&#39;s relative to the current position. If the repeat count is <code class="inline"><span class="i">*</span></code>
,
the offset is relative to the start of the packed string. And if its an
integer <code class="inline"><span class="w">n</span></code>
 the offset is relative to the start of the n-th innermost
() group (or the start of the string if <code class="inline"><span class="w">n</span></code>
 is bigger then the group
level).</p>
<p>The repeat count for <code class="inline"><span class="w">u</span></code>
 is interpreted as the maximal number of bytes
to encode per line of output, with 0, 1 and 2 replaced by 45. The repeat 
count should not be more than 65.</p>
</li>
<li>
<p>The <code class="inline"><span class="w">a</span></code>
, <code class="inline"><span class="w">A</span></code>
, and <code class="inline"><span class="w">Z</span></code>
 types gobble just one value, but pack it as a
string of length count, padding with nulls or spaces as necessary.  When
unpacking, <code class="inline"><span class="w">A</span></code>
 strips trailing whitespace and nulls, <code class="inline"><span class="w">Z</span></code>
 strips everything
after the first null, and <code class="inline"><span class="w">a</span></code>
 returns data verbatim.</p>
<p>If the value-to-pack is too long, it is truncated.  If too long and an
explicit count is provided, <code class="inline"><span class="w">Z</span></code>
 packs only <code class="inline"><span class="i">$count</span>-<span class="n">1</span></code>
 bytes, followed
by a null byte.  Thus <code class="inline"><span class="w">Z</span></code>
 always packs a trailing null (except when the
count is 0).</p>
</li>
<li>
<p>Likewise, the <code class="inline"><span class="w">b</span></code>
 and <code class="inline"><span class="w">B</span></code>
 fields pack a string that many bits long.
Each character of the input field of pack() generates 1 bit of the result.
Each result bit is based on the least-significant bit of the corresponding
input character, i.e., on <code class="inline"><a class="l_k" href="../functions/ord.html">ord($char)%2</a></code>.  In particular, characters <code class="inline"><span class="q">&quot;0&quot;</span></code>

and <code class="inline"><span class="q">&quot;1&quot;</span></code>
 generate bits 0 and 1, as do characters <code class="inline"><span class="q">&quot;\0&quot;</span></code>
 and <code class="inline"><span class="q">&quot;\1&quot;</span></code>
.</p>
<p>Starting from the beginning of the input string of pack(), each 8-tuple
of characters is converted to 1 character of output.  With format <code class="inline"><span class="w">b</span></code>

the first character of the 8-tuple determines the least-significant bit of a
character, and with format <code class="inline"><span class="w">B</span></code>
 it determines the most-significant bit of
a character.</p>
<p>If the length of the input string is not exactly divisible by 8, the
remainder is packed as if the input string were padded by null characters
at the end.  Similarly, during unpack()ing the &quot;extra&quot; bits are ignored.</p>
<p>If the input string of pack() is longer than needed, extra characters are 
ignored. A <code class="inline"><span class="i">*</span></code>
 for the repeat count of pack() means to use all the 
characters of the input field.  On unpack()ing the bits are converted to a 
string of <code class="inline"><span class="q">&quot;0&quot;</span></code>
s and <code class="inline"><span class="q">&quot;1&quot;</span></code>
s.</p>
</li>
<li>
<p>The <code class="inline"><span class="w">h</span></code>
 and <code class="inline"><span class="w">H</span></code>
 fields pack a string that many nybbles (4-bit groups,
representable as hexadecimal digits, 0-9a-f) long.</p>
<p>Each character of the input field of pack() generates 4 bits of the result.
For non-alphabetical characters the result is based on the 4 least-significant
bits of the input character, i.e., on <code class="inline"><a class="l_k" href="../functions/ord.html">ord($char)%16</a></code>.  In particular,
characters <code class="inline"><span class="q">&quot;0&quot;</span></code>
 and <code class="inline"><span class="q">&quot;1&quot;</span></code>
 generate nybbles 0 and 1, as do bytes
<code class="inline"><span class="q">&quot;\0&quot;</span></code>
 and <code class="inline"><span class="q">&quot;\1&quot;</span></code>
.  For characters <code class="inline"><span class="q">&quot;a&quot;</span>..<span class="q">&quot;f&quot;</span></code>
 and <code class="inline"><span class="q">&quot;A&quot;</span>..<span class="q">&quot;F&quot;</span></code>
 the result
is compatible with the usual hexadecimal digits, so that <code class="inline"><span class="q">&quot;a&quot;</span></code>
 and
<code class="inline"><span class="q">&quot;A&quot;</span></code>
 both generate the nybble <code class="inline"><span class="n">0xa</span>==<span class="n">10</span></code>
.  The result for characters
<code class="inline"><span class="q">&quot;g&quot;</span>..<span class="q">&quot;z&quot;</span></code>
 and <code class="inline"><span class="q">&quot;G&quot;</span>..<span class="q">&quot;Z&quot;</span></code>
 is not well-defined.</p>
<p>Starting from the beginning of the input string of pack(), each pair
of characters is converted to 1 character of output.  With format <code class="inline"><span class="w">h</span></code>
 the
first character of the pair determines the least-significant nybble of the
output character, and with format <code class="inline"><span class="w">H</span></code>
 it determines the most-significant
nybble.</p>
<p>If the length of the input string is not even, it behaves as if padded
by a null character at the end.  Similarly, during unpack()ing the &quot;extra&quot;
nybbles are ignored.</p>
<p>If the input string of pack() is longer than needed, extra characters are
ignored.
A <code class="inline"><span class="i">*</span></code>
 for the repeat count of pack() means to use all the characters of
the input field.  On unpack()ing the nybbles are converted to a string
of hexadecimal digits.</p>
</li>
<li>
<p>The <code class="inline"><span class="w">p</span></code>
 type packs a pointer to a null-terminated string.  You are
responsible for ensuring the string is not a temporary value (which can
potentially get deallocated before you get around to using the packed result).
The <code class="inline"><span class="w">P</span></code>
 type packs a pointer to a structure of the size indicated by the
length.  A NULL pointer is created if the corresponding value for <code class="inline"><span class="w">p</span></code>
 or
<code class="inline"><span class="w">P</span></code>
 is <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>, similarly for unpack().</p>
<p>If your system has a strange pointer size (i.e. a pointer is neither as
big as an int nor as big as a long), it may not be possible to pack or
unpack pointers in big- or little-endian byte order.  Attempting to do
so will result in a fatal error.</p>
</li>
<li>
<p>The <code class="inline">/</code> template character allows packing and unpacking of a sequence of
items where the packed structure contains a packed item count followed by 
the packed items themselves.</p>
<p>For <code class="inline"><a class="l_k" href="../functions/pack.html">pack</a></code> you write <i>length-item</i><code class="inline">/</code><i>sequence-item</i> and the
<i>length-item</i> describes how the length value is packed. The ones likely
to be of most use are integer-packing ones like <code class="inline"><span class="w">n</span></code>
 (for Java strings),
<code class="inline"><span class="w">w</span></code>
 (for ASN.1 or SNMP) and <code class="inline"><span class="w">N</span></code>
 (for Sun XDR).</p>
<p>For <code class="inline"><a class="l_k" href="../functions/pack.html">pack</a></code>, the <i>sequence-item</i> may have a repeat count, in which case
the minimum of that and the number of available items is used as argument
for the <i>length-item</i>. If it has no repeat count or uses a &#39;*&#39;, the number
of available items is used.</p>
<p>For <code class="inline"><a class="l_k" href="../functions/unpack.html">unpack</a></code> an internal stack of integer arguments unpacked so far is
used. You write <code class="inline">/</code><i>sequence-item</i> and the repeat count is obtained by
popping off the last element from the stack. The <i>sequence-item</i> must not
have a repeat count.</p>
<p>If the <i>sequence-item</i> refers to a string type (<code class="inline"><span class="q">&quot;A&quot;</span></code>
, <code class="inline"><span class="q">&quot;a&quot;</span></code>
 or <code class="inline"><span class="q">&quot;Z&quot;</span></code>
),
the <i>length-item</i> is a string length, not a number of strings. If there is
an explicit repeat count for pack, the packed string will be adjusted to that
given length.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/unpack.html">unpack</a> <span class="q">'W/a'</span><span class="cm">,</span> <span class="q">&quot;\04Gurusamy&quot;</span><span class="sc">;</span>            <span class="w">gives</span> <span class="s">(</span><span class="q">'Guru'</span><span class="s">)</span></li><li>    <a class="l_k" href="../functions/unpack.html">unpack</a> <span class="q">'a3/A A*'</span><span class="cm">,</span> <span class="q">'007 Bond  J '</span><span class="sc">;</span>       <span class="w">gives</span> <span class="s">(</span><span class="q">' Bond'</span><span class="cm">,</span> <span class="q">'J'</span><span class="s">)</span></li><li>    <a class="l_k" href="../functions/unpack.html">unpack</a> <span class="q">'a3 x2 /A A*'</span><span class="cm">,</span> <span class="q">'007: Bond, J.'</span><span class="sc">;</span>  <span class="w">gives</span> <span class="s">(</span><span class="q">'Bond, J'</span><span class="cm">,</span> <span class="q">'.'</span><span class="s">)</span></li><li>    <a class="l_k" href="../functions/pack.html">pack</a> <span class="q">'n/a* w/a'</span><span class="cm">,</span><span class="q">'hello,'</span><span class="cm">,</span><span class="q">'world'</span><span class="sc">;</span>       <span class="w">gives</span> <span class="q">&quot;\000\006hello,\005world&quot;</span></li><li>    <a class="l_k" href="../functions/pack.html">pack</a> <span class="q">'a/W2'</span><span class="cm">,</span> <a class="l_k" href="../functions/ord.html">ord</a><span class="s">(</span><span class="q">'a'</span><span class="s">)</span> .. <a class="l_k" href="../functions/ord.html">ord</a><span class="s">(</span><span class="q">'z'</span><span class="s">)</span><span class="sc">;</span>      <span class="w">gives</span> <span class="q">'2ab'</span></li></ol></pre><p>The <i>length-item</i> is not returned explicitly from <code class="inline"><a class="l_k" href="../functions/unpack.html">unpack</a></code>.</p>
<p>Adding a count to the <i>length-item</i> letter is unlikely to do anything
useful, unless that letter is <code class="inline"><span class="w">A</span></code>
, <code class="inline"><span class="w">a</span></code>
 or <code class="inline"><span class="w">Z</span></code>
.  Packing with a
<i>length-item</i> of <code class="inline"><span class="w">a</span></code>
 or <code class="inline"><span class="w">Z</span></code>
 may introduce <code class="inline"><span class="q">&quot;\000&quot;</span></code>
 characters,
which Perl does not regard as legal in numeric strings.</p>
</li>
<li>
<p>The integer types <code class="inline"><a class="l_k" href="../functions/s.html">s</a></code>, <code class="inline"><span class="w">S</span></code>
, <code class="inline"><span class="w">l</span></code>
, and <code class="inline"><span class="w">L</span></code>
 may be
followed by a <code class="inline">!</code>
 modifier to signify native shorts or
longs--as you can see from above for example a bare <code class="inline"><span class="w">l</span></code>
 does mean
exactly 32 bits, the native <code class="inline"><span class="w">long</span></code>
 (as seen by the local C compiler)
may be larger.  This is an issue mainly in 64-bit platforms.  You can
see whether using <code class="inline">!</code>
 makes any difference by</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/length.html">length</a><span class="s">(</span><a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">&quot;s&quot;</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot; &quot;</span><span class="cm">,</span> <a class="l_k" href="../functions/length.html">length</a><span class="s">(</span><a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">&quot;s!&quot;</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/length.html">length</a><span class="s">(</span><a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">&quot;l&quot;</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot; &quot;</span><span class="cm">,</span> <a class="l_k" href="../functions/length.html">length</a><span class="s">(</span><a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">&quot;l!&quot;</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p><code class="inline"><span class="w">i</span>!</code>
 and <code class="inline"><span class="w">I</span>!</code>
 also work but only because of completeness;
they are identical to <code class="inline"><span class="w">i</span></code>
 and <code class="inline"><span class="w">I</span></code>
.</p>
<p>The actual sizes (in bytes) of native shorts, ints, longs, and long
longs on the platform where Perl was built are also available via
<a href="../Config.html">Config</a>:</p>
<pre class="verbatim"><ol><li>       <a class="l_k" href="../functions/use.html">use</a> <span class="w">Config</span><span class="sc">;</span></li><li>       <a class="l_k" href="../functions/print.html">print</a> <span class="i">$Config</span>{<span class="w">shortsize</span>}<span class="cm">,</span>    <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>       <a class="l_k" href="../functions/print.html">print</a> <span class="i">$Config</span>{<span class="w">intsize</span>}<span class="cm">,</span>      <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>       <a class="l_k" href="../functions/print.html">print</a> <span class="i">$Config</span>{<span class="w">longsize</span>}<span class="cm">,</span>     <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>       <a class="l_k" href="../functions/print.html">print</a> <span class="i">$Config</span>{<span class="w">longlongsize</span>}<span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>(The <code class="inline"><span class="i">$Config</span>{<span class="w">longlongsize</span>}</code>
 will be undefined if your system does
not support long longs.)</p>
</li>
<li>
<p>The integer formats <code class="inline"><a class="l_k" href="../functions/s.html">s</a></code>, <code class="inline"><span class="w">S</span></code>
, <code class="inline"><span class="w">i</span></code>
, <code class="inline"><span class="w">I</span></code>
, <code class="inline"><span class="w">l</span></code>
, <code class="inline"><span class="w">L</span></code>
, <code class="inline"><span class="w">j</span></code>
, and <code class="inline"><span class="w">J</span></code>

are inherently non-portable between processors and operating systems
because they obey the native byteorder and endianness.  For example a
4-byte integer 0x12345678 (305419896 decimal) would be ordered natively
(arranged in and handled by the CPU registers) into bytes as</p>
<pre class="verbatim"><ol><li> 	0x12 0x34 0x56 0x78	# big-endian</li><li> 	0x78 0x56 0x34 0x12	# little-endian</li></ol></pre><p>Basically, the Intel and VAX CPUs are little-endian, while everybody
else, for example Motorola m68k/88k, PPC, Sparc, HP PA, Power, and
Cray are big-endian.  Alpha and MIPS can be either: Digital/Compaq
used/uses them in little-endian mode; SGI/Cray uses them in big-endian
mode.</p>
<p>The names `big-endian&#39; and `little-endian&#39; are comic references to
the classic &quot;Gulliver&#39;s Travels&quot; (via the paper &quot;On Holy Wars and a
Plea for Peace&quot; by Danny Cohen, USC/ISI IEN 137, April 1, 1980) and
the egg-eating habits of the Lilliputians.</p>
<p>Some systems may have even weirder byte orders such as</p>
<pre class="verbatim"><ol><li> 	0x56 0x78 0x12 0x34</li><li> 	0x34 0x12 0x78 0x56</li></ol></pre><p>You can see your system&#39;s preference with</p>
<pre class="verbatim"><ol><li> 	<a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">&quot; &quot;</span><span class="cm">,</span> <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span> <a class="l_k" href="../functions/sprintf.html">sprintf</a> <span class="q">&quot;%#02x&quot;</span><span class="cm">,</span> <span class="i">$_</span> <span class="s">}</span></li><li>                            <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;W*&quot;</span><span class="cm">,</span><a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">&quot;L&quot;</span><span class="cm">,</span><span class="n">0x12345678</span><span class="s">)</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>The byteorder on the platform where Perl was built is also available
via <a href="../Config.html">Config</a>:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="../functions/use.html">use</a> <span class="w">Config</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="i">$Config</span>{<span class="w">byteorder</span>}<span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>Byteorders <code class="inline"><span class="q">&#39;1234&#39;</span></code>
 and <code class="inline"><span class="q">&#39;12345678&#39;</span></code>
 are little-endian, <code class="inline"><span class="q">&#39;4321&#39;</span></code>

and <code class="inline"><span class="q">&#39;87654321&#39;</span></code>
 are big-endian.</p>
<p>If you want portable packed integers you can either use the formats
<code class="inline"><span class="w">n</span></code>
, <code class="inline"><span class="w">N</span></code>
, <code class="inline"><span class="w">v</span></code>
, and <code class="inline"><span class="w">V</span></code>
, or you can use the <code class="inline">&gt;</code> and <code class="inline">&lt;</code>

modifiers.  These modifiers are only available as of perl 5.9.2.
See also <a href="../perlport.html">perlport</a>.</p>
</li>
<li>
<p>All integer and floating point formats as well as <code class="inline"><span class="w">p</span></code>
 and <code class="inline"><span class="w">P</span></code>
 and
<code class="inline"><span class="s">(</span><span class="s">)</span></code>
-groups may be followed by the <code class="inline">&gt;</code> or <code class="inline">&lt;</code>
 modifiers
to force big- or little- endian byte-order, respectively.
This is especially useful, since <code class="inline"><span class="w">n</span></code>
, <code class="inline"><span class="w">N</span></code>
, <code class="inline"><span class="w">v</span></code>
 and <code class="inline"><span class="w">V</span></code>
 don&#39;t cover
signed integers, 64-bit integers and floating point values.  However,
there are some things to keep in mind.</p>
<p>Exchanging signed integers between different platforms only works
if all platforms store them in the same format.  Most platforms store
signed integers in two&#39;s complement, so usually this is not an issue.</p>
<p>The <code class="inline">&gt;</code> or <code class="inline">&lt;</code>
 modifiers can only be used on floating point
formats on big- or little-endian machines.  Otherwise, attempting to
do so will result in a fatal error.</p>
<p>Forcing big- or little-endian byte-order on floating point values for
data exchange can only work if all platforms are using the same
binary representation (e.g. IEEE floating point format).  Even if all
platforms are using IEEE, there may be subtle differences.  Being able
to use <code class="inline">&gt;</code> or <code class="inline">&lt;</code>
 on floating point values can be very useful,
but also very dangerous if you don&#39;t know exactly what you&#39;re doing.
It is definitely not a general way to portably store floating point
values.</p>
<p>When using <code class="inline">&gt;</code> or <code class="inline">&lt;</code>
 on an <code class="inline"><span class="s">(</span><span class="s">)</span></code>
-group, this will affect
all types inside the group that accept the byte-order modifiers,
including all subgroups.  It will silently be ignored for all other
types.  You are not allowed to override the byte-order within a group
that already has a byte-order modifier suffix.</p>
</li>
<li>
<p>Real numbers (floats and doubles) are in the native machine format only;
due to the multiplicity of floating formats around, and the lack of a
standard &quot;network&quot; representation, no facility for interchange has been
made.  This means that packed floating point data written on one machine
may not be readable on another - even if both use IEEE floating point
arithmetic (as the endian-ness of the memory representation is not part
of the IEEE spec).  See also <a href="../perlport.html">perlport</a>.</p>
<p>If you know exactly what you&#39;re doing, you can use the <code class="inline">&gt;</code> or <code class="inline">&lt;</code>

modifiers to force big- or little-endian byte-order on floating point values.</p>
<p>Note that Perl uses doubles (or long doubles, if configured) internally for
all numeric calculation, and converting from double into float and thence back
to double again will lose precision (i.e., <code class="inline">unpack("f", pack("f", $foo)</code>)
will not in general equal $foo).</p>
</li>
<li>
<p>Pack and unpack can operate in two modes, character mode (<code class="inline"><span class="w">C0</span></code>
 mode) where
the packed string is processed per character and UTF-8 mode (<code class="inline"><span class="w">U0</span></code>
 mode)
where the packed string is processed in its UTF-8-encoded Unicode form on
a byte by byte basis. Character mode is the default unless the format string 
starts with an <code class="inline"><span class="w">U</span></code>
. You can switch mode at any moment with an explicit 
<code class="inline"><span class="w">C0</span></code>
 or <code class="inline"><span class="w">U0</span></code>
 in the format. A mode is in effect until the next mode switch 
or until the end of the ()-group in which it was entered.</p>
</li>
<li>
<p>You must yourself do any alignment or padding by inserting for example
enough <code class="inline"><span class="q">&#39;x&#39;</span></code>
es while packing.  There is no way to pack() and unpack()
could know where the characters are going to or coming from.  Therefore
<code class="inline"><a class="l_k" href="../functions/pack.html">pack</a></code> (and <code class="inline"><a class="l_k" href="../functions/unpack.html">unpack</a></code>) handle their output and input as flat
sequences of characters.</p>
</li>
<li>
<p>A ()-group is a sub-TEMPLATE enclosed in parentheses.  A group may
take a repeat count, both as postfix, and for unpack() also via the <code class="inline">/</code>
template character. Within each repetition of a group, positioning with
<code class="inline"><span class="i">@</span></code>
 starts again at 0. Therefore, the result of</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span> <span class="q">&#39;@1A((@2A)@3A)&#39;</span><span class="cm">,</span> <span class="q">&#39;a&#39;</span><span class="cm">,</span> <span class="q">&#39;b&#39;</span><span class="cm">,</span> <span class="q">&#39;c&#39;</span> <span class="s">)</span></li></ol></pre><p>is the string &quot;\0a\0\0bc&quot;.</p>
</li>
<li>
<p><code class="inline"><span class="w">x</span></code>
 and <code class="inline"><span class="w">X</span></code>
 accept <code class="inline">!</code>
 modifier.  In this case they act as
alignment commands: they jump forward/back to the closest position
aligned at a multiple of <code class="inline"><span class="w">count</span></code>
 characters. For example, to pack() or
unpack() C&#39;s <code class="inline"><span class="i">struct</span> <span class="s">{</span><span class="w">char</span> <span class="w">c</span><span class="sc">;</span> <span class="w">double</span> <span class="w">d</span><span class="sc">;</span> <span class="w">char</span> <span class="w">cc</span><span class="s">[</span><span class="n">2</span><span class="s">]</span><span class="s">}</span></code>
 one may need to
use the template <code class="inline"><span class="w">W</span> <span class="w">x</span>!<span class="s">[</span><span class="w">d</span><span class="s">]</span> <span class="w">d</span> <span class="w">W</span><span class="s">[</span><span class="n">2</span><span class="s">]</span></code>
; this assumes that doubles must be
aligned on the double&#39;s size.</p>
<p>For alignment commands <code class="inline"><span class="w">count</span></code>
 of 0 is equivalent to <code class="inline"><span class="w">count</span></code>
 of 1;
both result in no-ops.</p>
</li>
<li>
<p><code class="inline"><span class="w">n</span></code>
, <code class="inline"><span class="w">N</span></code>
, <code class="inline"><span class="w">v</span></code>
 and <code class="inline"><span class="w">V</span></code>
 accept the <code class="inline">!</code>
 modifier. In this case they
will represent signed 16-/32-bit integers in big-/little-endian order.
This is only portable if all platforms sharing the packed data use the
same binary representation for signed integers (e.g. all platforms are
using two&#39;s complement representation).</p>
</li>
<li>
<p>A comment in a TEMPLATE starts with <code class="inline"><span class="c">#</span></code>
 and goes to the end of line.
White space may be used to separate pack codes from each other, but
modifiers and a repeat count must follow immediately.</p>
</li>
<li>
<p>If TEMPLATE requires more arguments to pack() than actually given, pack()
assumes additional <code class="inline"><span class="q">&quot;&quot;</span></code>
 arguments.  If TEMPLATE requires fewer arguments
to pack() than actually given, extra arguments are ignored.
-----

function package
namespace|
Declares the compilation unit as being in the given namespace.  The scope
of the package declaration is from the declaration itself through the end
of the enclosing block, file, or eval (the same as the <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> operator).
All further unqualified dynamic identifiers will be in this namespace.
A package statement affects only dynamic variables--including those
you&#39;ve used <code class="inline"><a class="l_k" href="../functions/local.html">local</a></code> on--but <i>not</i> lexical variables, which are created
with <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>.  Typically it would be the first declaration in a file to
be included by the <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> or <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code> operator.  You can switch into a
package in more than one place; it merely influences which symbol table
is used by the compiler for the rest of that block.  You can refer to
variables and filehandles in other packages by prefixing the identifier
with the package name and a double colon:  <code class="inline"><span class="i">$Package::Variable</span></code>
.
If the package name is null, the <code class="inline"><span class="w">main</span></code>
 package as assumed.  That is,
<code class="inline"><span class="i">$::sail</span></code>
 is equivalent to <code class="inline"><span class="i">$main::sail</span></code>
 (as well as to <code class="inline"><span class="i">$main&#39;sail</span></code>
,
still seen in older code).</p>
<p>See <a href="../perlmod.html#Packages">"Packages" in perlmod</a> for more information about packages, modules,
and classes.  See <a href="../perlsub.html">perlsub</a> for other scoping issues.
-----

function package
Declares the compilation unit as being in the given namespace.  The scope
of the package declaration is from the declaration itself through the end
of the enclosing block, file, or eval (the same as the <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> operator).
All further unqualified dynamic identifiers will be in this namespace.
A package statement affects only dynamic variables--including those
you&#39;ve used <code class="inline"><a class="l_k" href="../functions/local.html">local</a></code> on--but <i>not</i> lexical variables, which are created
with <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code>.  Typically it would be the first declaration in a file to
be included by the <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> or <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code> operator.  You can switch into a
package in more than one place; it merely influences which symbol table
is used by the compiler for the rest of that block.  You can refer to
variables and filehandles in other packages by prefixing the identifier
with the package name and a double colon:  <code class="inline"><span class="i">$Package::Variable</span></code>
.
If the package name is null, the <code class="inline"><span class="w">main</span></code>
 package as assumed.  That is,
<code class="inline"><span class="i">$::sail</span></code>
 is equivalent to <code class="inline"><span class="i">$main::sail</span></code>
 (as well as to <code class="inline"><span class="i">$main&#39;sail</span></code>
,
still seen in older code).</p>
<p>See <a href="../perlmod.html#Packages">"Packages" in perlmod</a> for more information about packages, modules,
and classes.  See <a href="../perlsub.html">perlsub</a> for other scoping issues.
-----

function pipe
readhandle|
writehandle|
Opens a pair of connected pipes like the corresponding system call.
Note that if you set up a loop of piped processes, deadlock can occur
unless you are very careful.  In addition, note that Perl&#39;s pipes use
IO buffering, so you may need to set <code class="inline"><span class="i">$|</span></code>
 to flush your WRITEHANDLE
after each command, depending on the application.</p>
<p>See <a href="../IPC/Open2.html">IPC::Open2</a>, <a href="../IPC/Open3.html">IPC::Open3</a>, and <a href="../perlipc.html#Bidirectional-Communication">"Bidirectional Communication" in perlipc</a>
for examples of such things.</p>
<p>On systems that support a close-on-exec flag on files, the flag will be set
for the newly opened file descriptors as determined by the value of $^F.
See <a href="../perlvar.html#%24%5eF">"$^F" in perlvar</a>.
-----

function pop
array|
Pops and returns the last value of the array, shortening the array by
one element.</p>
<p>If there are no elements in the array, returns the undefined value
(although this may happen at other times as well).  If ARRAY is
omitted, pops the <code class="inline"><span class="i">@ARGV</span></code>
 array in the main program, and the <code class="inline"><span class="i">@_</span></code>

array in subroutines, just like <code class="inline"><a class="l_k" href="../functions/shift.html">shift</a></code>.
-----

function pop
Pops and returns the last value of the array, shortening the array by
one element.</p>
<p>If there are no elements in the array, returns the undefined value
(although this may happen at other times as well).  If ARRAY is
omitted, pops the <code class="inline"><span class="i">@ARGV</span></code>
 array in the main program, and the <code class="inline"><span class="i">@_</span></code>

array in subroutines, just like <code class="inline"><a class="l_k" href="../functions/shift.html">shift</a></code>.
-----

function pos
scalar|
Returns the offset of where the last <code class="inline"><a class="l_k" href="../functions/m.html">m//g</a></code> search left off for the variable
in question (<code class="inline"><span class="i">$_</span></code>
 is used when the variable is not specified).  Note that
0 is a valid match offset.  <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> indicates that the search position
is reset (usually due to match failure, but can also be because no match has
yet been performed on the scalar). <code class="inline"><a class="l_k" href="../functions/pos.html">pos</a></code> directly accesses the location used
by the regexp engine to store the offset, so assigning to <code class="inline"><a class="l_k" href="../functions/pos.html">pos</a></code> will change
that offset, and so will also influence the <code class="inline">\<span class="w">G</span></code>
 zero-width assertion in
regular expressions. Because a failed <code class="inline"><a class="l_k" href="../functions/m.html">m//gc</a></code> match doesn&#39;t reset the offset,
the return from <code class="inline"><a class="l_k" href="../functions/pos.html">pos</a></code> won&#39;t change either in this case.  See <a href="../perlre.html">perlre</a> and
<a href="../perlop.html">perlop</a>.
-----

function pos
Returns the offset of where the last <code class="inline"><a class="l_k" href="../functions/m.html">m//g</a></code> search left off for the variable
in question (<code class="inline"><span class="i">$_</span></code>
 is used when the variable is not specified).  Note that
0 is a valid match offset.  <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> indicates that the search position
is reset (usually due to match failure, but can also be because no match has
yet been performed on the scalar). <code class="inline"><a class="l_k" href="../functions/pos.html">pos</a></code> directly accesses the location used
by the regexp engine to store the offset, so assigning to <code class="inline"><a class="l_k" href="../functions/pos.html">pos</a></code> will change
that offset, and so will also influence the <code class="inline">\<span class="w">G</span></code>
 zero-width assertion in
regular expressions. Because a failed <code class="inline"><a class="l_k" href="../functions/m.html">m//gc</a></code> match doesn&#39;t reset the offset,
the return from <code class="inline"><a class="l_k" href="../functions/pos.html">pos</a></code> won&#39;t change either in this case.  See <a href="../perlre.html">perlre</a> and
<a href="../perlop.html">perlop</a>.
-----

function print
filehandle list|
Prints a string or a list of strings.  Returns true if successful.
FILEHANDLE may be a scalar variable name, in which case the variable
contains the name of or a reference to the filehandle, thus introducing
one level of indirection.  (NOTE: If FILEHANDLE is a variable and
the next token is a term, it may be misinterpreted as an operator
unless you interpose a <code class="inline">+</code>
 or put parentheses around the arguments.)
If FILEHANDLE is omitted, prints by default to standard output (or
to the last selected output channel--see <a href="../functions/select.html">"select"</a>).  If LIST is
also omitted, prints <code class="inline"><span class="i">$_</span></code>
 to the currently selected output channel.
To set the default output channel to something other than STDOUT
use the select operation.  The current value of <code class="inline"><span class="i">$,</span></code>
 (if any) is
printed between each LIST item.  The current value of <code class="inline"><span class="i">$\</span></code>
 (if
any) is printed after the entire LIST has been printed.  Because
print takes a LIST, anything in the LIST is evaluated in list
context, and any subroutine that you call will have one or more of
its expressions evaluated in list context.  Also be careful not to
follow the print keyword with a left parenthesis unless you want
the corresponding right parenthesis to terminate the arguments to
the print--interpose a <code class="inline">+</code>
 or put parentheses around all the
arguments.</p>
<p>Note that if you&#39;re storing FILEHANDLEs in an array, or if you&#39;re using
any other expression more complex than a scalar variable to retrieve it,
you will have to use a block returning the filehandle value instead:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="s">{</span> <span class="i">$files</span>[<span class="i">$i</span>] <span class="s">}</span> <span class="q">&quot;stuff\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="s">{</span> <span class="i">$OK</span> ? <span class="w">STDOUT</span> <span class="co">:</span> <span class="w">STDERR</span> <span class="s">}</span> <span class="q">&quot;stuff\n&quot;</span><span class="sc">;</span></li></ol><
-----

function print
list|
Prints a string or a list of strings.  Returns true if successful.
FILEHANDLE may be a scalar variable name, in which case the variable
contains the name of or a reference to the filehandle, thus introducing
one level of indirection.  (NOTE: If FILEHANDLE is a variable and
the next token is a term, it may be misinterpreted as an operator
unless you interpose a <code class="inline">+</code>
 or put parentheses around the arguments.)
If FILEHANDLE is omitted, prints by default to standard output (or
to the last selected output channel--see <a href="../functions/select.html">"select"</a>).  If LIST is
also omitted, prints <code class="inline"><span class="i">$_</span></code>
 to the currently selected output channel.
To set the default output channel to something other than STDOUT
use the select operation.  The current value of <code class="inline"><span class="i">$,</span></code>
 (if any) is
printed between each LIST item.  The current value of <code class="inline"><span class="i">$\</span></code>
 (if
any) is printed after the entire LIST has been printed.  Because
print takes a LIST, anything in the LIST is evaluated in list
context, and any subroutine that you call will have one or more of
its expressions evaluated in list context.  Also be careful not to
follow the print keyword with a left parenthesis unless you want
the corresponding right parenthesis to terminate the arguments to
the print--interpose a <code class="inline">+</code>
 or put parentheses around all the
arguments.</p>
<p>Note that if you&#39;re storing FILEHANDLEs in an array, or if you&#39;re using
any other expression more complex than a scalar variable to retrieve it,
you will have to use a block returning the filehandle value instead:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="s">{</span> <span class="i">$files</span>[<span class="i">$i</span>] <span class="s">}</span> <span class="q">&quot;stuff\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="s">{</span> <span class="i">$OK</span> ? <span class="w">STDOUT</span> <span class="co">:</span> <span class="w">STDERR</span> <span class="s">}</span> <span class="q">&quot;stuff\n&quot;</span><span class="sc">;</span></li></ol><
-----

function print
Prints a string or a list of strings.  Returns true if successful.
FILEHANDLE may be a scalar variable name, in which case the variable
contains the name of or a reference to the filehandle, thus introducing
one level of indirection.  (NOTE: If FILEHANDLE is a variable and
the next token is a term, it may be misinterpreted as an operator
unless you interpose a <code class="inline">+</code>
 or put parentheses around the arguments.)
If FILEHANDLE is omitted, prints by default to standard output (or
to the last selected output channel--see <a href="../functions/select.html">"select"</a>).  If LIST is
also omitted, prints <code class="inline"><span class="i">$_</span></code>
 to the currently selected output channel.
To set the default output channel to something other than STDOUT
use the select operation.  The current value of <code class="inline"><span class="i">$,</span></code>
 (if any) is
printed between each LIST item.  The current value of <code class="inline"><span class="i">$\</span></code>
 (if
any) is printed after the entire LIST has been printed.  Because
print takes a LIST, anything in the LIST is evaluated in list
context, and any subroutine that you call will have one or more of
its expressions evaluated in list context.  Also be careful not to
follow the print keyword with a left parenthesis unless you want
the corresponding right parenthesis to terminate the arguments to
the print--interpose a <code class="inline">+</code>
 or put parentheses around all the
arguments.</p>
<p>Note that if you&#39;re storing FILEHANDLEs in an array, or if you&#39;re using
any other expression more complex than a scalar variable to retrieve it,
you will have to use a block returning the filehandle value instead:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="s">{</span> <span class="i">$files</span>[<span class="i">$i</span>] <span class="s">}</span> <span class="q">&quot;stuff\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="s">{</span> <span class="i">$OK</span> ? <span class="w">STDOUT</span> <span class="co">:</span> <span class="w">STDERR</span> <span class="s">}</span> <span class="q">&quot;stuff\n&quot;</span><span class="sc">;</span></li></ol><
-----

function printf
filehandle format|
list|
Equivalent to <code class="inline"><a class="l_k" href="../functions/print.html">print</a> <span class="i">FILEHANDLE</span> <a class="l_k" href="../functions/sprintf.html">sprintf</a><span class="s">(</span><span class="w">FORMAT</span><span class="cm">,</span> <span class="w">LIST</span><span class="s">)</span></code>
, except that <code class="inline"><span class="i">$\</span></code>

(the output record separator) is not appended.  The first argument
of the list will be interpreted as the <code class="inline"><a class="l_k" href="../functions/printf.html">printf</a></code> format. See <code class="inline"><a class="l_k" href="../functions/sprintf.html">sprintf</a></code>
for an explanation of the format argument.  If <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">locale</span></code>
 is in effect,
and POSIX::setlocale() has been called, the character used for the decimal
separator in formatted floating point numbers is affected by the LC_NUMERIC
locale.  See <a href="../perllocale.html">perllocale</a> and <a href="../POSIX.html">POSIX</a>.</p>
<p>Don&#39;t fall into the trap of using a <code class="inline"><a class="l_k" href="../functions/printf.html">printf</a></code> when a simple
<code class="inline"><a class="l_k" href="../functions/print.html">print</a></code> would do.  The <code class="inline"><a class="l_k" href="../functions/print.html">print</a></code> is more efficient and less
error prone.
-----

function printf
format|
list|
Equivalent to <code class="inline"><a class="l_k" href="../functions/print.html">print</a> <span class="i">FILEHANDLE</span> <a class="l_k" href="../functions/sprintf.html">sprintf</a><span class="s">(</span><span class="w">FORMAT</span><span class="cm">,</span> <span class="w">LIST</span><span class="s">)</span></code>
, except that <code class="inline"><span class="i">$\</span></code>

(the output record separator) is not appended.  The first argument
of the list will be interpreted as the <code class="inline"><a class="l_k" href="../functions/printf.html">printf</a></code> format. See <code class="inline"><a class="l_k" href="../functions/sprintf.html">sprintf</a></code>
for an explanation of the format argument.  If <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">locale</span></code>
 is in effect,
and POSIX::setlocale() has been called, the character used for the decimal
separator in formatted floating point numbers is affected by the LC_NUMERIC
locale.  See <a href="../perllocale.html">perllocale</a> and <a href="../POSIX.html">POSIX</a>.</p>
<p>Don&#39;t fall into the trap of using a <code class="inline"><a class="l_k" href="../functions/printf.html">printf</a></code> when a simple
<code class="inline"><a class="l_k" href="../functions/print.html">print</a></code> would do.  The <code class="inline"><a class="l_k" href="../functions/print.html">print</a></code> is more efficient and less
error prone.
-----

function prototype
function|
Returns the prototype of a function as a string (or <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> if the
function has no prototype).  FUNCTION is a reference to, or the name of,
the function whose prototype you want to retrieve.</p>
<p>If FUNCTION is a string starting with <code class="inline"><span class="w">CORE::</span></code>
, the rest is taken as a
name for Perl builtin.  If the builtin is not <i>overridable</i> (such as
<code class="inline"><a class="l_k" href="../functions/qw.html">qw//</a></code>) or if its arguments cannot be adequately expressed by a prototype
(such as <code class="inline"><a class="l_k" href="../functions/system.html">system</a></code>), prototype() returns <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>, because the builtin
does not really behave like a Perl function.  Otherwise, the string
describing the equivalent prototype is returned.
-----

function push
array|
list|
Treats ARRAY as a stack, and pushes the values of LIST
onto the end of ARRAY.  The length of ARRAY increases by the length of
LIST.  Has the same effect as</p>
<pre class="verbatim"><ol><li>    for <span class="i">$value</span> <span class="s">(</span><span class="w">LIST</span><span class="s">)</span> <span class="s">{</span></li><li>	<span class="i">$ARRAY</span>[++<span class="i">$#ARRAY</span>] = <span class="i">$value</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>but is more efficient.  Returns the number of elements in the array following
the completed <code class="inline"><a class="l_k" href="../functions/push.html">push</a></code>.
-----

function q
/string/|
Generalized quotes.  See <a href="../perlop.html#Quote-Like-Operators">"Quote-Like Operators" in perlop</a>.
-----

function q
q/string/|
Generalized quotes.  See <a href="../perlop.html#Quote-Like-Operators">"Quote-Like Operators" in perlop</a>.
-----

function q
x/string/|
Generalized quotes.  See <a href="../perlop.html#Quote-Like-Operators">"Quote-Like Operators" in perlop</a>.
-----

function q
w/string/|
Generalized quotes.  See <a href="../perlop.html#Quote-Like-Operators">"Quote-Like Operators" in perlop</a>.
-----

function qq
/string/|
Generalized quotes.  See <a href="../perlop.html#Quote-Like-Operators">"Quote-Like Operators" in perlop</a>.
-----

function qr
/string/|
Regexp-like quote.  See <a href="../perlop.html#Regexp-Quote-Like-Operators">"Regexp Quote-Like Operators" in perlop</a>.
-----

function quotemeta
expr|
Returns the value of EXPR with all non-&quot;word&quot;
characters backslashed.  (That is, all characters not matching
<code class="inline"><span class="q">/[A-Za-z_0-9]/</span></code>
 will be preceded by a backslash in the
returned string, regardless of any locale settings.)
This is the internal function implementing
the <code class="inline">\<span class="w">Q</span></code>
 escape in double-quoted strings.</p>
<p>If EXPR is omitted, uses <code class="inline"><span class="i">$_</span></code>
.
-----

function quotemeta
Returns the value of EXPR with all non-&quot;word&quot;
characters backslashed.  (That is, all characters not matching
<code class="inline"><span class="q">/[A-Za-z_0-9]/</span></code>
 will be preceded by a backslash in the
returned string, regardless of any locale settings.)
This is the internal function implementing
the <code class="inline">\<span class="w">Q</span></code>
 escape in double-quoted strings.</p>
<p>If EXPR is omitted, uses <code class="inline"><span class="i">$_</span></code>
.
-----

function qw
/string/|
Generalized quotes.  See <a href="../perlop.html#Quote-Like-Operators">"Quote-Like Operators" in perlop</a>.
-----

function qx
/string/|
Generalized quotes.  See <a href="../perlop.html#Quote-Like-Operators">"Quote-Like Operators" in perlop</a>.
-----

function rand
expr|
Returns a random fractional number greater than or equal to <code class="inline"><span class="n">0</span></code>
 and less
than the value of EXPR.  (EXPR should be positive.)  If EXPR is
omitted, the value <code class="inline"><span class="n">1</span></code>
 is used.  Currently EXPR with the value <code class="inline"><span class="n">0</span></code>
 is
also special-cased as <code class="inline"><span class="n">1</span></code>
 - this has not been documented before perl 5.8.0
and is subject to change in future versions of perl.  Automatically calls
<code class="inline"><a class="l_k" href="../functions/srand.html">srand</a></code> unless <code class="inline"><a class="l_k" href="../functions/srand.html">srand</a></code> has already been called.  See also <code class="inline"><a class="l_k" href="../functions/srand.html">srand</a></code>.</p>
<p>Apply <code class="inline"><a class="l_k" href="../functions/int.html">int()</a></code> to the value returned by <code class="inline"><a class="l_k" href="../functions/rand.html">rand()</a></code> if you want random
integers instead of random fractional numbers.  For example,</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/int.html">int</a><span class="s">(</span><a class="l_k" href="../functions/rand.html">rand</a><span class="s">(</span><span class="n">10</span><span class="s">)</span><span class="s">)</span></li></ol></pre><p>returns a random integer between <code class="inline"><span class="n">0</span></code>
 and <code class="inline"><span class="n">9</span></code>
, inclusive.</p>
<p>(Note: If your rand function consistently returns numbers that are too
large or too small, then your version of Perl was probably compiled
with the wrong number of RANDBITS.)
-----

function rand
Returns a random fractional number greater than or equal to <code class="inline"><span class="n">0</span></code>
 and less
than the value of EXPR.  (EXPR should be positive.)  If EXPR is
omitted, the value <code class="inline"><span class="n">1</span></code>
 is used.  Currently EXPR with the value <code class="inline"><span class="n">0</span></code>
 is
also special-cased as <code class="inline"><span class="n">1</span></code>
 - this has not been documented before perl 5.8.0
and is subject to change in future versions of perl.  Automatically calls
<code class="inline"><a class="l_k" href="../functions/srand.html">srand</a></code> unless <code class="inline"><a class="l_k" href="../functions/srand.html">srand</a></code> has already been called.  See also <code class="inline"><a class="l_k" href="../functions/srand.html">srand</a></code>.</p>
<p>Apply <code class="inline"><a class="l_k" href="../functions/int.html">int()</a></code> to the value returned by <code class="inline"><a class="l_k" href="../functions/rand.html">rand()</a></code> if you want random
integers instead of random fractional numbers.  For example,</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/int.html">int</a><span class="s">(</span><a class="l_k" href="../functions/rand.html">rand</a><span class="s">(</span><span class="n">10</span><span class="s">)</span><span class="s">)</span></li></ol></pre><p>returns a random integer between <code class="inline"><span class="n">0</span></code>
 and <code class="inline"><span class="n">9</span></code>
, inclusive.</p>
<p>(Note: If your rand function consistently returns numbers that are too
large or too small, then your version of Perl was probably compiled
with the wrong number of RANDBITS.)
-----

function read
filehandle|
scalar|
length|
offset|
Attempts to read LENGTH <i>characters</i> of data into variable SCALAR
from the specified FILEHANDLE.  Returns the number of characters
actually read, <code class="inline"><span class="n">0</span></code>
 at end of file, or undef if there was an error (in
the latter case <code class="inline"><span class="i">$!</span></code>
 is also set).  SCALAR will be grown or shrunk 
so that the last character actually read is the last character of the
scalar after the read.</p>
<p>An OFFSET may be specified to place the read data at some place in the
string other than the beginning.  A negative OFFSET specifies
placement at that many characters counting backwards from the end of
the string.  A positive OFFSET greater than the length of SCALAR
results in the string being padded to the required size with <code class="inline"><span class="q">&quot;\0&quot;</span></code>

bytes before the result of the read is appended.</p>
<p>The call is actually implemented in terms of either Perl&#39;s or system&#39;s
fread() call.  To get a true read(2) system call, see <code class="inline"><a class="l_k" href="../functions/sysread.html">sysread</a></code>.</p>
<p>Note the <i>characters</i>: depending on the status of the filehandle,
either (8-bit) bytes or characters are read.  By default all
filehandles operate on bytes, but for example if the filehandle has
been opened with the <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
 I/O layer (see <a href="../functions/open.html">"open"</a>, and the <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code>
pragma, <a href="../open.html">open</a>), the I/O will operate on UTF-8 encoded Unicode
characters, not bytes.  Similarly for the <code class="inline"><span class="j">:</span><span class="w">encoding</span></code>
 pragma:
in that case pretty much any characters can be read.
-----

function read
filehandle|
scalar|
length|
Attempts to read LENGTH <i>characters</i> of data into variable SCALAR
from the specified FILEHANDLE.  Returns the number of characters
actually read, <code class="inline"><span class="n">0</span></code>
 at end of file, or undef if there was an error (in
the latter case <code class="inline"><span class="i">$!</span></code>
 is also set).  SCALAR will be grown or shrunk 
so that the last character actually read is the last character of the
scalar after the read.</p>
<p>An OFFSET may be specified to place the read data at some place in the
string other than the beginning.  A negative OFFSET specifies
placement at that many characters counting backwards from the end of
the string.  A positive OFFSET greater than the length of SCALAR
results in the string being padded to the required size with <code class="inline"><span class="q">&quot;\0&quot;</span></code>

bytes before the result of the read is appended.</p>
<p>The call is actually implemented in terms of either Perl&#39;s or system&#39;s
fread() call.  To get a true read(2) system call, see <code class="inline"><a class="l_k" href="../functions/sysread.html">sysread</a></code>.</p>
<p>Note the <i>characters</i>: depending on the status of the filehandle,
either (8-bit) bytes or characters are read.  By default all
filehandles operate on bytes, but for example if the filehandle has
been opened with the <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
 I/O layer (see <a href="../functions/open.html">"open"</a>, and the <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code>
pragma, <a href="../open.html">open</a>), the I/O will operate on UTF-8 encoded Unicode
characters, not bytes.  Similarly for the <code class="inline"><span class="j">:</span><span class="w">encoding</span></code>
 pragma:
in that case pretty much any characters can be read.
-----

function readdir
dirhandle|
Returns the next directory entry for a directory opened by <code class="inline"><a class="l_k" href="../functions/opendir.html">opendir</a></code>.
If used in list context, returns all the rest of the entries in the
directory.  If there are no more entries, returns an undefined value in
scalar context or a null list in list context.</p>
<p>If you&#39;re planning to filetest the return values out of a <code class="inline"><a class="l_k" href="../functions/readdir.html">readdir</a></code>, you&#39;d
better prepend the directory in question.  Otherwise, because we didn&#39;t
<code class="inline"><a class="l_k" href="../functions/chdir.html">chdir</a></code> there, it would have been testing the wrong file.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/opendir.html">opendir</a><span class="s">(</span><a class="l_k" href="../functions/my.html">my</a> <span class="i">$dh</span><span class="cm">,</span> <span class="i">$some_dir</span><span class="s">)</span> || <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;can't opendir $some_dir: $!&quot;</span><span class="sc">;</span></li><li>    <span class="i">@dots</span> = <a class="l_k" href="../functions/grep.html">grep</a> <span class="s">{</span> <span class="q">/^\./</span> &amp;&amp; -f <span class="q">&quot;$some_dir/$_&quot;</span> <span class="s">}</span> <a class="l_k" href="../functions/readdir.html">readdir</a><span class="s">(</span><span class="i">$dh</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/closedir.html">closedir</a> <span class="i">$dh</span><span class="sc">;</span></li></ol><
-----

function readline
expr|
Reads from the filehandle whose typeglob is contained in EXPR (or from
*ARGV if EXPR is not provided).  In scalar context, each call reads and
returns the next line, until end-of-file is reached, whereupon the
subsequent call returns undef.  In list context, reads until end-of-file
is reached and returns a list of lines.  Note that the notion of &quot;line&quot;
used here is however you may have defined it with <code class="inline"><span class="i">$/</span></code>
 or
<code class="inline"><span class="i">$INPUT_RECORD_SEPARATOR</span></code>
).  See <a href="../perlvar.html#%24%2f">"$/" in perlvar</a>.</p>
<p>When <code class="inline"><span class="i">$/</span></code>
 is set to <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>, when readline() is in scalar
context (i.e. file slurp mode), and when an empty file is read, it
returns <code class="inline"><span class="q">&#39;&#39;</span></code>
 the first time, followed by <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> subsequently.</p>
<p>This is the internal function implementing the <code class="inline"><span class="q">&lt;EXPR&gt;</span></code>

operator, but you can use it directly.  The <code class="inline"><span class="q">&lt;EXPR&gt;</span></code>

operator is discussed in more detail in <a href="../perlop.html#I%2fO-Operators">"I/O Operators" in perlop</a>.</p>
<pre class="verbatim"><ol><li>    <span class="i">$line</span> = <span class="q">&lt;STDIN&gt;</span><span class="sc">;</span></li><li>    <span class="i">$line</span> = <a class="l_k" href="../functions/readline.html">readline</a><span class="s">(</span><span class="i">*STDIN</span><span class="s">)</span><span class="sc">;</span>		<span class="c"># same thing</span></li></ol></pre><p>If readline encounters an operating system error, <code class="inline"><span class="i">$!</span></code>
 will be set with the
corresponding error message.  It can be helpful to check <code class="inline"><span class="i">$!</span></code>
 when you are
reading from filehandles you don&#39;t trust, such as a tty or a socket.  The
following example uses the operator form of <code class="inline"><a class="l_k" href="../functions/readline.html">readline</a></code>, and takes the necessary
steps to ensure that <code class="inline"><a class="l_k" href="../functions/readline.html">readline</a></code> was successful.</p>
<pre class="verbatim"><ol><li>    for <span class="s">(</span><span class="sc">;</span><span class="sc">;</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">$!</span><span class="sc">;</span></li><li>        unless <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a><span class="s">(</span> <span class="i">$line</span> = &lt;&gt; <span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="../functions/last.html">last</a> if <a class="l_k" href="../functions/eof.html">eof</a><span class="sc">;</span></li><li>            <a class="l_k" href="../functions/die.html">die</a> <span class="i">$!</span> if <span class="i">$!</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>        <span class="c"># ...</span></li><li>    <span class="s">}</span></li></ol><
-----

function readline
Reads from the filehandle whose typeglob is contained in EXPR (or from
*ARGV if EXPR is not provided).  In scalar context, each call reads and
returns the next line, until end-of-file is reached, whereupon the
subsequent call returns undef.  In list context, reads until end-of-file
is reached and returns a list of lines.  Note that the notion of &quot;line&quot;
used here is however you may have defined it with <code class="inline"><span class="i">$/</span></code>
 or
<code class="inline"><span class="i">$INPUT_RECORD_SEPARATOR</span></code>
).  See <a href="../perlvar.html#%24%2f">"$/" in perlvar</a>.</p>
<p>When <code class="inline"><span class="i">$/</span></code>
 is set to <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>, when readline() is in scalar
context (i.e. file slurp mode), and when an empty file is read, it
returns <code class="inline"><span class="q">&#39;&#39;</span></code>
 the first time, followed by <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> subsequently.</p>
<p>This is the internal function implementing the <code class="inline"><span class="q">&lt;EXPR&gt;</span></code>

operator, but you can use it directly.  The <code class="inline"><span class="q">&lt;EXPR&gt;</span></code>

operator is discussed in more detail in <a href="../perlop.html#I%2fO-Operators">"I/O Operators" in perlop</a>.</p>
<pre class="verbatim"><ol><li>    <span class="i">$line</span> = <span class="q">&lt;STDIN&gt;</span><span class="sc">;</span></li><li>    <span class="i">$line</span> = <a class="l_k" href="../functions/readline.html">readline</a><span class="s">(</span><span class="i">*STDIN</span><span class="s">)</span><span class="sc">;</span>		<span class="c"># same thing</span></li></ol></pre><p>If readline encounters an operating system error, <code class="inline"><span class="i">$!</span></code>
 will be set with the
corresponding error message.  It can be helpful to check <code class="inline"><span class="i">$!</span></code>
 when you are
reading from filehandles you don&#39;t trust, such as a tty or a socket.  The
following example uses the operator form of <code class="inline"><a class="l_k" href="../functions/readline.html">readline</a></code>, and takes the necessary
steps to ensure that <code class="inline"><a class="l_k" href="../functions/readline.html">readline</a></code> was successful.</p>
<pre class="verbatim"><ol><li>    for <span class="s">(</span><span class="sc">;</span><span class="sc">;</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">$!</span><span class="sc">;</span></li><li>        unless <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a><span class="s">(</span> <span class="i">$line</span> = &lt;&gt; <span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>            <a class="l_k" href="../functions/last.html">last</a> if <a class="l_k" href="../functions/eof.html">eof</a><span class="sc">;</span></li><li>            <a class="l_k" href="../functions/die.html">die</a> <span class="i">$!</span> if <span class="i">$!</span><span class="sc">;</span></li><li>        <span class="s">}</span></li><li>        <span class="c"># ...</span></li><li>    <span class="s">}</span></li></ol><
-----

function readlink
expr|
Returns the value of a symbolic link, if symbolic links are
implemented.  If not, gives a fatal error.  If there is some system
error, returns the undefined value and sets <code class="inline"><span class="i">$!</span></code>
 (errno).  If EXPR is
omitted, uses <code class="inline"><span class="i">$_</span></code>
.
-----

function readlink
Returns the value of a symbolic link, if symbolic links are
implemented.  If not, gives a fatal error.  If there is some system
error, returns the undefined value and sets <code class="inline"><span class="i">$!</span></code>
 (errno).  If EXPR is
omitted, uses <code class="inline"><span class="i">$_</span></code>
.
-----

function readpipe
expr|
EXPR is executed as a system command.
The collected standard output of the command is returned.
In scalar context, it comes back as a single (potentially
multi-line) string.  In list context, returns a list of lines
(however you&#39;ve defined lines with <code class="inline"><span class="i">$/</span></code>
 or <code class="inline"><span class="i">$INPUT_RECORD_SEPARATOR</span></code>
).
This is the internal function implementing the <code class="inline"><a class="l_k" href="../functions/qx.html">qx/EXPR/</a></code>
operator, but you can use it directly.  The <code class="inline"><a class="l_k" href="../functions/qx.html">qx/EXPR/</a></code>
operator is discussed in more detail in <a href="../perlop.html#I%2fO-Operators">"I/O Operators" in perlop</a>.
If EXPR is omitted, uses <code class="inline"><span class="i">$_</span></code>
.
-----

function readpipe
EXPR is executed as a system command.
The collected standard output of the command is returned.
In scalar context, it comes back as a single (potentially
multi-line) string.  In list context, returns a list of lines
(however you&#39;ve defined lines with <code class="inline"><span class="i">$/</span></code>
 or <code class="inline"><span class="i">$INPUT_RECORD_SEPARATOR</span></code>
).
This is the internal function implementing the <code class="inline"><a class="l_k" href="../functions/qx.html">qx/EXPR/</a></code>
operator, but you can use it directly.  The <code class="inline"><a class="l_k" href="../functions/qx.html">qx/EXPR/</a></code>
operator is discussed in more detail in <a href="../perlop.html#I%2fO-Operators">"I/O Operators" in perlop</a>.
If EXPR is omitted, uses <code class="inline"><span class="i">$_</span></code>
.
-----

function recv
socket|
scalar|
length|
flags|
Receives a message on a socket.  Attempts to receive LENGTH characters
of data into variable SCALAR from the specified SOCKET filehandle.
SCALAR will be grown or shrunk to the length actually read.  Takes the
same flags as the system call of the same name.  Returns the address
of the sender if SOCKET&#39;s protocol supports this; returns an empty
string otherwise.  If there&#39;s an error, returns the undefined value.
This call is actually implemented in terms of recvfrom(2) system call.
See <a href="../perlipc.html#UDP%3a-Message-Passing">"UDP: Message Passing" in perlipc</a> for examples.</p>
<p>Note the <i>characters</i>: depending on the status of the socket, either
(8-bit) bytes or characters are received.  By default all sockets
operate on bytes, but for example if the socket has been changed using
binmode() to operate with the <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span><span class="w">utf8</span><span class="s">)</span></code>
 I/O layer (see the
<code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> pragma, <a href="../open.html">open</a>), the I/O will operate on UTF-8 encoded Unicode
characters, not bytes.  Similarly for the <code class="inline"><span class="j">:</span><span class="w">encoding</span></code>
 pragma: in that
case pretty much any characters can be read.
-----

function redo
label|
The <code class="inline"><a class="l_k" href="../functions/redo.html">redo</a></code> command restarts the loop block without evaluating the
conditional again.  The <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code> block, if any, is not executed.  If
the LABEL is omitted, the command refers to the innermost enclosing
loop.  Programs that want to lie to themselves about what was just input 
normally use this command:</p>
<pre class="verbatim"><ol><li>    <span class="c"># a simpleminded Pascal comment stripper</span></li><li>    <span class="c"># (warning: assumes no { or } in strings)</span></li><li>    <span class="j">LINE:</span> while <span class="s">(</span><span class="q">&lt;STDIN&gt;</span><span class="s">)</span> <span class="s">{</span></li><li>	while <span class="s">(</span><span class="q">s|({.*}.*){.*}|$1 |</span><span class="s">)</span> <span class="s">{</span><span class="s">}</span></li><li>	<span class="q">s|{.*}| |</span><span class="sc">;</span></li><li>	if <span class="s">(</span><span class="q">s|{.*| |</span><span class="s">)</span> <span class="s">{</span></li><li>	    <span class="i">$front</span> = <span class="i">$_</span><span class="sc">;</span></li><li>	    while <span class="s">(</span><span class="q">&lt;STDIN&gt;</span><span class="s">)</span> <span class="s">{</span></li><li>		if <span class="s">(</span><span class="q">/}/</span><span class="s">)</span> <span class="s">{</span>	<span class="c"># end of comment?</span></li><li>		    <span class="q">s|^|$front\{|</span><span class="sc">;</span></li><li>		    <a class="l_k" href="../functions/redo.html">redo</a> <span class="j">LINE</span><span class="sc">;</span></li><li>		<span class="s">}</span></li><li>	    <span class="s">}</span></li><li>	<span class="s">}</span></li><li>	<a class="l_k" href="../functions/print.html">print</a><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p><code class="inline"><a class="l_k" href="../functions/redo.html">redo</a></code> cannot be used to retry a block which returns a value such as
<code class="inline"><a class="l_k" href="../functions/eval.html">eval</a> <span class="s">{</span><span class="s">}</span></code>
, <code class="inline"><a class="l_k" href="../functions/sub.html">sub</a> <span class="s">{</span><span class="s">}</span></code>
 or <code class="inline"><a class="l_k" href="../functions/do.html">do</a> <span class="s">{</span><span class="s">}</span></code>
, and should not be used to exit
a grep() or map() operation.</p>
<p>Note that a block by itself is semantically identical to a loop
that executes once.  Thus <code class="inline"><a class="l_k" href="../functions/redo.html">redo</a></code> inside such a block will effectively
turn it into a looping construct.</p>
<p>See also <a href="../functions/continue.html">"continue"</a> for an illustration of how <code class="inline"><a class="l_k" href="../functions/last.html">last</a></code>, <code class="inline"><a class="l_k" href="../functions/next.html">next</a></code>, and
<code class="inline"><a class="l_k" href="../functions/redo.html">redo</a></code> work.
-----

function redo
The <code class="inline"><a class="l_k" href="../functions/redo.html">redo</a></code> command restarts the loop block without evaluating the
conditional again.  The <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code> block, if any, is not executed.  If
the LABEL is omitted, the command refers to the innermost enclosing
loop.  Programs that want to lie to themselves about what was just input 
normally use this command:</p>
<pre class="verbatim"><ol><li>    <span class="c"># a simpleminded Pascal comment stripper</span></li><li>    <span class="c"># (warning: assumes no { or } in strings)</span></li><li>    <span class="j">LINE:</span> while <span class="s">(</span><span class="q">&lt;STDIN&gt;</span><span class="s">)</span> <span class="s">{</span></li><li>	while <span class="s">(</span><span class="q">s|({.*}.*){.*}|$1 |</span><span class="s">)</span> <span class="s">{</span><span class="s">}</span></li><li>	<span class="q">s|{.*}| |</span><span class="sc">;</span></li><li>	if <span class="s">(</span><span class="q">s|{.*| |</span><span class="s">)</span> <span class="s">{</span></li><li>	    <span class="i">$front</span> = <span class="i">$_</span><span class="sc">;</span></li><li>	    while <span class="s">(</span><span class="q">&lt;STDIN&gt;</span><span class="s">)</span> <span class="s">{</span></li><li>		if <span class="s">(</span><span class="q">/}/</span><span class="s">)</span> <span class="s">{</span>	<span class="c"># end of comment?</span></li><li>		    <span class="q">s|^|$front\{|</span><span class="sc">;</span></li><li>		    <a class="l_k" href="../functions/redo.html">redo</a> <span class="j">LINE</span><span class="sc">;</span></li><li>		<span class="s">}</span></li><li>	    <span class="s">}</span></li><li>	<span class="s">}</span></li><li>	<a class="l_k" href="../functions/print.html">print</a><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p><code class="inline"><a class="l_k" href="../functions/redo.html">redo</a></code> cannot be used to retry a block which returns a value such as
<code class="inline"><a class="l_k" href="../functions/eval.html">eval</a> <span class="s">{</span><span class="s">}</span></code>
, <code class="inline"><a class="l_k" href="../functions/sub.html">sub</a> <span class="s">{</span><span class="s">}</span></code>
 or <code class="inline"><a class="l_k" href="../functions/do.html">do</a> <span class="s">{</span><span class="s">}</span></code>
, and should not be used to exit
a grep() or map() operation.</p>
<p>Note that a block by itself is semantically identical to a loop
that executes once.  Thus <code class="inline"><a class="l_k" href="../functions/redo.html">redo</a></code> inside such a block will effectively
turn it into a looping construct.</p>
<p>See also <a href="../functions/continue.html">"continue"</a> for an illustration of how <code class="inline"><a class="l_k" href="../functions/last.html">last</a></code>, <code class="inline"><a class="l_k" href="../functions/next.html">next</a></code>, and
<code class="inline"><a class="l_k" href="../functions/redo.html">redo</a></code> work.
-----

function ref
expr|
Returns a non-empty string if EXPR is a reference, the empty
string otherwise. If EXPR
is not specified, <code class="inline"><span class="i">$_</span></code>
 will be used.  The value returned depends on the
type of thing the reference is a reference to.
Builtin types include:</p>
<pre class="verbatim"><ol><li>    <span class="w">SCALAR</span></li><li>    <span class="w">ARRAY</span></li><li>    <span class="w">HASH</span></li><li>    <span class="w">CODE</span></li><li>    <span class="w">REF</span></li><li>    <span class="w">GLOB</span></li><li>    <span class="w">LVALUE</span></li><li>    <span class="w">FORMAT</span></li><li>    <span class="w">IO</span></li><li>    <span class="w">VSTRING</span></li><li>    <span class="w">Regexp</span></li></ol></pre><p>If the referenced object has been blessed into a package, then that package
name is returned instead.  You can think of <code class="inline"><a class="l_k" href="../functions/ref.html">ref</a></code> as a <code class="inline"><span class="w">typeof</span></code>
 operator.</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span><a class="l_k" href="../functions/ref.html">ref</a><span class="s">(</span><span class="i">$r</span><span class="s">)</span> eq <span class="q">&quot;HASH&quot;</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;r is a reference to a hash.\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    unless <span class="s">(</span><a class="l_k" href="../functions/ref.html">ref</a><span class="s">(</span><span class="i">$r</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;r is not a reference at all.\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>The return value <code class="inline"><span class="w">LVALUE</span></code>
 indicates a reference to an lvalue that is not
a variable. You get this from taking the reference of function calls like
<code class="inline"><a class="l_k" href="../functions/pos.html">pos()</a></code> or <code class="inline"><a class="l_k" href="../functions/substr.html">substr()</a></code>. <code class="inline"><span class="w">VSTRING</span></code>
 is returned if the reference points
to a <a href="../perldata.html#Version-Strings">version string</a>.</p>
<p>The result <code class="inline"><span class="w">Regexp</span></code>
 indicates that the argument is a regular expression
resulting from <code class="inline"><a class="l_k" href="../functions/qr.html">qr//</a></code>.</p>
<p>See also <a href="../perlref.html">perlref</a>.
-----

function ref
Returns a non-empty string if EXPR is a reference, the empty
string otherwise. If EXPR
is not specified, <code class="inline"><span class="i">$_</span></code>
 will be used.  The value returned depends on the
type of thing the reference is a reference to.
Builtin types include:</p>
<pre class="verbatim"><ol><li>    <span class="w">SCALAR</span></li><li>    <span class="w">ARRAY</span></li><li>    <span class="w">HASH</span></li><li>    <span class="w">CODE</span></li><li>    <span class="w">REF</span></li><li>    <span class="w">GLOB</span></li><li>    <span class="w">LVALUE</span></li><li>    <span class="w">FORMAT</span></li><li>    <span class="w">IO</span></li><li>    <span class="w">VSTRING</span></li><li>    <span class="w">Regexp</span></li></ol></pre><p>If the referenced object has been blessed into a package, then that package
name is returned instead.  You can think of <code class="inline"><a class="l_k" href="../functions/ref.html">ref</a></code> as a <code class="inline"><span class="w">typeof</span></code>
 operator.</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span><a class="l_k" href="../functions/ref.html">ref</a><span class="s">(</span><span class="i">$r</span><span class="s">)</span> eq <span class="q">&quot;HASH&quot;</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;r is a reference to a hash.\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    unless <span class="s">(</span><a class="l_k" href="../functions/ref.html">ref</a><span class="s">(</span><span class="i">$r</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;r is not a reference at all.\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>The return value <code class="inline"><span class="w">LVALUE</span></code>
 indicates a reference to an lvalue that is not
a variable. You get this from taking the reference of function calls like
<code class="inline"><a class="l_k" href="../functions/pos.html">pos()</a></code> or <code class="inline"><a class="l_k" href="../functions/substr.html">substr()</a></code>. <code class="inline"><span class="w">VSTRING</span></code>
 is returned if the reference points
to a <a href="../perldata.html#Version-Strings">version string</a>.</p>
<p>The result <code class="inline"><span class="w">Regexp</span></code>
 indicates that the argument is a regular expression
resulting from <code class="inline"><a class="l_k" href="../functions/qr.html">qr//</a></code>.</p>
<p>See also <a href="../perlref.html">perlref</a>.
-----

function rename
oldname|
newname|
Changes the name of a file; an existing file NEWNAME will be
clobbered.  Returns true for success, false otherwise.</p>
<p>Behavior of this function varies wildly depending on your system
implementation.  For example, it will usually not work across file system
boundaries, even though the system <i>mv</i> command sometimes compensates
for this.  Other restrictions include whether it works on directories,
open files, or pre-existing files.  Check <a href="../perlport.html">perlport</a> and either the
rename(2) manpage or equivalent system documentation for details.</p>
<p>For a platform independent <code class="inline"><span class="w">move</span></code>
 function look at the <a href="../File/Copy.html">File::Copy</a>
module.
-----

function require
version|
Demands a version of Perl specified by VERSION, or demands some semantics
specified by EXPR or by <code class="inline"><span class="i">$_</span></code>
 if EXPR is not supplied.</p>
<p>VERSION may be either a numeric argument such as 5.006, which will be
compared to <code class="inline"><span class="i">$]</span></code>
, or a literal of the form v5.6.1, which will be compared
to <code class="inline"><span class="i">$^V</span></code>
 (aka $PERL_VERSION).  A fatal error is produced at run time if
VERSION is greater than the version of the current Perl interpreter.
Compare with <a href="../functions/use.html">"use"</a>, which can do a similar check at compile time.</p>
<p>Specifying VERSION as a literal of the form v5.6.1 should generally be
avoided, because it leads to misleading error messages under earlier
versions of Perl that do not support this syntax.  The equivalent numeric
version should be used instead.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/require.html">require</a> <span class="v">v5.6.1</span><span class="sc">;</span>	<span class="c"># run time version check</span></li><li>    <a class="l_k" href="../functions/require.html">require</a> <span class="v">5.6.1</span><span class="sc">;</span>	<span class="c"># ditto</span></li><li>    <a class="l_k" href="../functions/require.html">require</a> <span class="n">5.006_001</span><span class="sc">;</span>	<span class="c"># ditto; preferred for backwards compatibility</span></li></ol></pre><p>Otherwise, <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> demands that a library file be included if it
hasn&#39;t already been included.  The file is included via the do-FILE
mechanism, which is essentially just a variety of <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code> with the
caveat that lexical variables in the invoking script will be invisible
to the included code.  Has semantics similar to the following subroutine:</p>
<pre class="verbatim"><ol><li><a name="require"></a>    sub <span class="m">require</span> <span class="s">{</span></li><li>       <a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span><span class="i">$filename</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>       if <span class="s">(</span><a class="l_k" href="../functions/exists.html">exists</a> <span class="i">$INC</span>{<span class="i">$filename</span>}<span class="s">)</span> <span class="s">{</span></li><li>           <a class="l_k" href="../functions/return.html">return</a> <span class="n">1</span> if <span class="i">$INC</span>{<span class="i">$filename</span>}<span class="sc">;</span></li><li>           <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Compilation failed in require&quot;</span><span class="sc">;</span></li><li>       <span class="s">}</span></li><li>       <a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span><span class="i">$realfilename</span><span class="cm">,</span><span class="i">$result</span><span class="s">)</span><span class="sc">;</span></li><li>       <span class="j">ITER:</span> <span class="s">{</span></li><li>           foreach <span class="i">$prefix</span> <span class="s">(</span><span class="i">@INC</span><span class="s">)</span> <span class="s">{</span></li><li>               <span class="i">$realfilename</span> = <span class="q">&quot;$prefix/$filename&quot;</span><span class="sc">;</span></li><li>               if <span class="s">(</span>-f <span class="i">$realfilename</span><span class="s">)</span> <span class="s">{</span></li><li>                   <span class="i">$INC</span>{<span class="i">$filename</span>} = <span class="i">$realfilename</span><span class="sc">;</span></li><li>                   <span class="i">$result</span> = <a class="l_k" href="../functions/do.html">do</a> <span class="i">$realfilename</span><span class="sc">;</span></li><li>                   <a class="l_k" href="../functions/last.html">last</a> <span class="j">ITER</span><span class="sc">;</span></li><li>               <span class="s">}</span></li><li>           <span class="s">}</span></li><li>           <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t find $filename in \@INC&quot;</span><span class="sc">;</span></li><li>       <span class="s">}</span></li><li>       if <span class="s">(</span><span class="i">$@</span><span class="s">)</span> <span class="s">{</span></li><li>           <span class="i">$INC</span>{<span class="i">$filename</span>} = <a class="l_k" href="../functions/undef.html">undef</a><span class="sc">;</span></li><li>           <a class="l_k" href="../functions/die.html">die</a> <span class="i">$@</span><span class="sc">;</span></li><li>       <span class="s">}</span> elsif <span class="s">(</span>!<span class="i">$result</span><span class="s">)</span> <span class="s">{</span></li><li>           <a class="l_k" href="../functions/delete.html">delete</a> <span class="i">$INC</span>{<span class="i">$filename</span>}<span class="sc">;</span></li><li>           <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;$filename did not return true value&quot;</span><span class="sc">;</span></li><li>       <span class="s">}</span> else <span class="s">{</span></li><li>           <a class="l_k" href="../functions/return.html">return</a> <span class="i">$result</span><span class="sc">;</span></li><li>       <span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre><p>Note that the file will not be included twice under the same specified
name.</p>
<p>The file must return true as the last statement to indicate
successful execution of any initialization code, so it&#39;s customary to
end such a file with <code class="inline"><span class="n">1</span><span class="sc">;</span></code>
 unless you&#39;re sure it&#39;ll return true
otherwise.  But it&#39;s better just to put the <code class="inline"><span class="n">1</span><span class="sc">;</span></code>
, in case you add more
statements.</p>
<p>If EXPR is a bareword, the require assumes a &quot;<i>.pm</i>&quot; extension and
replaces &quot;<i>::</i>&quot; with &quot;<i>/</i>&quot; in the filename for you,
to make it easy to load standard modules.  This form of loading of
modules does not risk altering your namespace.</p>
<p>In other words, if you try this:</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="../functions/require.html">require</a> <span class="w">Foo::Bar</span><span class="sc">;</span>    <span class="c"># a splendid bareword</span></li></ol></pre><p>The require function will actually look for the &quot;<i>Foo/Bar.pm</i>&quot; file in the
directories specified in the <code class="inline"><span class="i">@INC</span></code>
 array.</p>
<p>But if you try this:</p>
<pre class="verbatim"><ol><li>        <span class="i">$class</span> = <span class="q">&#39;Foo::Bar&#39;</span><span class="sc">;</span></li><li>        <a class="l_k" href="../functions/require.html">require</a> <span class="i">$class</span><span class="sc">;</span>	     <span class="c"># $class is not a bareword</span></li><li>    <span class="c">#or</span></li><li>        <a class="l_k" href="../functions/require.html">require</a> <span class="q">&quot;Foo::Bar&quot;</span><span class="sc">;</span>  <span class="c"># not a bareword because of the &quot;&quot;</span></li></ol></pre><p>The require function will look for the &quot;<i>Foo::Bar</i>&quot; file in the @INC array and
will complain about not finding &quot;<i>Foo::Bar</i>&quot; there.  In this case you can do:</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="../functions/eval.html">eval</a> <span class="q">&quot;require $class&quot;</span><span class="sc">;</span></li></ol></pre><p>Now that you understand how <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> looks for files in the case of a
bareword argument, there is a little extra functionality going on behind
the scenes.  Before <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> looks for a &quot;<i>.pm</i>&quot; extension, it will
first look for a similar filename with a &quot;<i>.pmc</i>&quot; extension. If this file
is found, it will be loaded in place of any file ending in a &quot;<i>.pm</i>&quot;
extension.</p>
<p>You can also insert hooks into the import facility, by putting directly
Perl code into the @INC array.  There are three forms of hooks: subroutine
references, array references and blessed objects.</p>
<p>Subroutine references are the simplest case.  When the inclusion system
walks through @INC and encounters a subroutine, this subroutine gets
called with two parameters, the first being a reference to itself, and the
second the name of the file to be included (e.g. &quot;<i>Foo/Bar.pm</i>&quot;).  The
subroutine should return nothing, or a list of up to three values in the
following order:</p>
<ol>
<li>
<p>A filehandle, from which the file will be read.</p>
</li>
<li>
<p>A reference to a subroutine. If there is no filehandle (previous item),
then this subroutine is expected to generate one line of source code per
call, writing the line into <code class="inline"><span class="i">$_</span></code>
 and returning 1, then returning 0 at
&quot;end of file&quot;. If there is a filehandle, then the subroutine will be
called to act as a simple source filter, with the line as read in <code class="inline"><span class="i">$_</span></code>
.
Again, return 1 for each valid line, and 0 after all lines have been
returned.</p>
</li>
<li>
<p>Optional state for the subroutine. The state is passed in as <code class="inline"><span class="i">$_</span>[<span class="n">1</span>]</code>
. A
reference to the subroutine itself is passed in as <code class="inline"><span class="i">$_</span>[<span class="n">0</span>]</code>
.</p>
</li>
</ol>
<p>If an empty list, <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>, or nothing that matches the first 3 values above
is returned then <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> will look at the remaining elements of @INC.
Note that this file handle must be a real file handle (strictly a typeglob,
or reference to a typeglob, blessed or unblessed) - tied file handles will be
ignored and return value processing will stop there.</p>
<p>If the hook is an array reference, its first element must be a subroutine
reference.  This subroutine is called as above, but the first parameter is
the array reference.  This enables to pass indirectly some arguments to
the subroutine.</p>
<p>In other words, you can write:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/push.html">push</a> <span class="i">@INC</span><span class="cm">,</span> \<span class="i">&amp;my_sub</span><span class="sc">;</span></li><li><a name="my_sub"></a>    sub <span class="m">my_sub</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span><span class="i">$coderef</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span>	<span class="c"># $coderef is \&amp;my_sub</span></li><li>	...</li><li>    <span class="s">}</span></li></ol></pre><p>or:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/push.html">push</a> <span class="i">@INC</span><span class="cm">,</span> <span class="s">[</span> \<span class="i">&amp;my_sub</span><span class="cm">,</span> <span class="i">$x</span><span class="cm">,</span> <span class="i">$y</span><span class="cm">,</span> ... <span class="s">]</span><span class="sc">;</span></li><li><a name="my_sub"></a>    sub <span class="m">my_sub</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span><span class="i">$arrayref</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>	<span class="c"># Retrieve $x, $y, ...</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="i">@parameters</span> = <span class="i">@$arrayref</span>[<span class="n">1</span>..<span class="i">$#$arrayref</span>]<span class="sc">;</span></li><li>	...</li><li>    <span class="s">}</span></li></ol></pre><p>If the hook is an object, it must provide an INC method that will be
called as above, the first parameter being the object itself.  (Note that
you must fully qualify the sub&#39;s name, as unqualified <code class="inline"><span class="w">INC</span></code>
 is always forced
into package <code class="inline"><span class="w">main</span></code>
.)  Here is a typical code layout:</p>
<pre class="verbatim"><ol><li>    <span class="c"># In Foo.pm</span></li><li><a name="package-Foo"></a>    package <span class="i">Foo</span><span class="sc">;</span></li><li><a name="new"></a>    sub <span class="m">new</span> <span class="s">{</span> ... <span class="s">}</span></li><li><a name="Foo::INC"></a>    sub <span class="m">Foo::INC</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span><span class="i">$self</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>	...</li><li>    <span class="s">}</span></li><li></li><li>    <span class="c"># In the main program</span></li><li>    <a class="l_k" href="../functions/push.html">push</a> <span class="i">@INC</span><span class="cm">,</span> <span class="w">Foo</span><span class="w">-&gt;new</span><span class="s">(</span>...<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Note that these hooks are also permitted to set the %INC entry
corresponding to the files they have loaded. See <a href="../perlvar.html#%25INC">"%INC" in perlvar</a>.</p>
<p>For a yet-more-powerful import facility, see <a href="../functions/use.html">"use"</a> and <a href="../perlmod.html">perlmod</a>.
-----

function require
expr|
Demands a version of Perl specified by VERSION, or demands some semantics
specified by EXPR or by <code class="inline"><span class="i">$_</span></code>
 if EXPR is not supplied.</p>
<p>VERSION may be either a numeric argument such as 5.006, which will be
compared to <code class="inline"><span class="i">$]</span></code>
, or a literal of the form v5.6.1, which will be compared
to <code class="inline"><span class="i">$^V</span></code>
 (aka $PERL_VERSION).  A fatal error is produced at run time if
VERSION is greater than the version of the current Perl interpreter.
Compare with <a href="../functions/use.html">"use"</a>, which can do a similar check at compile time.</p>
<p>Specifying VERSION as a literal of the form v5.6.1 should generally be
avoided, because it leads to misleading error messages under earlier
versions of Perl that do not support this syntax.  The equivalent numeric
version should be used instead.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/require.html">require</a> <span class="v">v5.6.1</span><span class="sc">;</span>	<span class="c"># run time version check</span></li><li>    <a class="l_k" href="../functions/require.html">require</a> <span class="v">5.6.1</span><span class="sc">;</span>	<span class="c"># ditto</span></li><li>    <a class="l_k" href="../functions/require.html">require</a> <span class="n">5.006_001</span><span class="sc">;</span>	<span class="c"># ditto; preferred for backwards compatibility</span></li></ol></pre><p>Otherwise, <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> demands that a library file be included if it
hasn&#39;t already been included.  The file is included via the do-FILE
mechanism, which is essentially just a variety of <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code> with the
caveat that lexical variables in the invoking script will be invisible
to the included code.  Has semantics similar to the following subroutine:</p>
<pre class="verbatim"><ol><li><a name="require"></a>    sub <span class="m">require</span> <span class="s">{</span></li><li>       <a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span><span class="i">$filename</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>       if <span class="s">(</span><a class="l_k" href="../functions/exists.html">exists</a> <span class="i">$INC</span>{<span class="i">$filename</span>}<span class="s">)</span> <span class="s">{</span></li><li>           <a class="l_k" href="../functions/return.html">return</a> <span class="n">1</span> if <span class="i">$INC</span>{<span class="i">$filename</span>}<span class="sc">;</span></li><li>           <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Compilation failed in require&quot;</span><span class="sc">;</span></li><li>       <span class="s">}</span></li><li>       <a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span><span class="i">$realfilename</span><span class="cm">,</span><span class="i">$result</span><span class="s">)</span><span class="sc">;</span></li><li>       <span class="j">ITER:</span> <span class="s">{</span></li><li>           foreach <span class="i">$prefix</span> <span class="s">(</span><span class="i">@INC</span><span class="s">)</span> <span class="s">{</span></li><li>               <span class="i">$realfilename</span> = <span class="q">&quot;$prefix/$filename&quot;</span><span class="sc">;</span></li><li>               if <span class="s">(</span>-f <span class="i">$realfilename</span><span class="s">)</span> <span class="s">{</span></li><li>                   <span class="i">$INC</span>{<span class="i">$filename</span>} = <span class="i">$realfilename</span><span class="sc">;</span></li><li>                   <span class="i">$result</span> = <a class="l_k" href="../functions/do.html">do</a> <span class="i">$realfilename</span><span class="sc">;</span></li><li>                   <a class="l_k" href="../functions/last.html">last</a> <span class="j">ITER</span><span class="sc">;</span></li><li>               <span class="s">}</span></li><li>           <span class="s">}</span></li><li>           <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t find $filename in \@INC&quot;</span><span class="sc">;</span></li><li>       <span class="s">}</span></li><li>       if <span class="s">(</span><span class="i">$@</span><span class="s">)</span> <span class="s">{</span></li><li>           <span class="i">$INC</span>{<span class="i">$filename</span>} = <a class="l_k" href="../functions/undef.html">undef</a><span class="sc">;</span></li><li>           <a class="l_k" href="../functions/die.html">die</a> <span class="i">$@</span><span class="sc">;</span></li><li>       <span class="s">}</span> elsif <span class="s">(</span>!<span class="i">$result</span><span class="s">)</span> <span class="s">{</span></li><li>           <a class="l_k" href="../functions/delete.html">delete</a> <span class="i">$INC</span>{<span class="i">$filename</span>}<span class="sc">;</span></li><li>           <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;$filename did not return true value&quot;</span><span class="sc">;</span></li><li>       <span class="s">}</span> else <span class="s">{</span></li><li>           <a class="l_k" href="../functions/return.html">return</a> <span class="i">$result</span><span class="sc">;</span></li><li>       <span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre><p>Note that the file will not be included twice under the same specified
name.</p>
<p>The file must return true as the last statement to indicate
successful execution of any initialization code, so it&#39;s customary to
end such a file with <code class="inline"><span class="n">1</span><span class="sc">;</span></code>
 unless you&#39;re sure it&#39;ll return true
otherwise.  But it&#39;s better just to put the <code class="inline"><span class="n">1</span><span class="sc">;</span></code>
, in case you add more
statements.</p>
<p>If EXPR is a bareword, the require assumes a &quot;<i>.pm</i>&quot; extension and
replaces &quot;<i>::</i>&quot; with &quot;<i>/</i>&quot; in the filename for you,
to make it easy to load standard modules.  This form of loading of
modules does not risk altering your namespace.</p>
<p>In other words, if you try this:</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="../functions/require.html">require</a> <span class="w">Foo::Bar</span><span class="sc">;</span>    <span class="c"># a splendid bareword</span></li></ol></pre><p>The require function will actually look for the &quot;<i>Foo/Bar.pm</i>&quot; file in the
directories specified in the <code class="inline"><span class="i">@INC</span></code>
 array.</p>
<p>But if you try this:</p>
<pre class="verbatim"><ol><li>        <span class="i">$class</span> = <span class="q">&#39;Foo::Bar&#39;</span><span class="sc">;</span></li><li>        <a class="l_k" href="../functions/require.html">require</a> <span class="i">$class</span><span class="sc">;</span>	     <span class="c"># $class is not a bareword</span></li><li>    <span class="c">#or</span></li><li>        <a class="l_k" href="../functions/require.html">require</a> <span class="q">&quot;Foo::Bar&quot;</span><span class="sc">;</span>  <span class="c"># not a bareword because of the &quot;&quot;</span></li></ol></pre><p>The require function will look for the &quot;<i>Foo::Bar</i>&quot; file in the @INC array and
will complain about not finding &quot;<i>Foo::Bar</i>&quot; there.  In this case you can do:</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="../functions/eval.html">eval</a> <span class="q">&quot;require $class&quot;</span><span class="sc">;</span></li></ol></pre><p>Now that you understand how <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> looks for files in the case of a
bareword argument, there is a little extra functionality going on behind
the scenes.  Before <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> looks for a &quot;<i>.pm</i>&quot; extension, it will
first look for a similar filename with a &quot;<i>.pmc</i>&quot; extension. If this file
is found, it will be loaded in place of any file ending in a &quot;<i>.pm</i>&quot;
extension.</p>
<p>You can also insert hooks into the import facility, by putting directly
Perl code into the @INC array.  There are three forms of hooks: subroutine
references, array references and blessed objects.</p>
<p>Subroutine references are the simplest case.  When the inclusion system
walks through @INC and encounters a subroutine, this subroutine gets
called with two parameters, the first being a reference to itself, and the
second the name of the file to be included (e.g. &quot;<i>Foo/Bar.pm</i>&quot;).  The
subroutine should return nothing, or a list of up to three values in the
following order:</p>
<ol>
<li>
<p>A filehandle, from which the file will be read.</p>
</li>
<li>
<p>A reference to a subroutine. If there is no filehandle (previous item),
then this subroutine is expected to generate one line of source code per
call, writing the line into <code class="inline"><span class="i">$_</span></code>
 and returning 1, then returning 0 at
&quot;end of file&quot;. If there is a filehandle, then the subroutine will be
called to act as a simple source filter, with the line as read in <code class="inline"><span class="i">$_</span></code>
.
Again, return 1 for each valid line, and 0 after all lines have been
returned.</p>
</li>
<li>
<p>Optional state for the subroutine. The state is passed in as <code class="inline"><span class="i">$_</span>[<span class="n">1</span>]</code>
. A
reference to the subroutine itself is passed in as <code class="inline"><span class="i">$_</span>[<span class="n">0</span>]</code>
.</p>
</li>
</ol>
<p>If an empty list, <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>, or nothing that matches the first 3 values above
is returned then <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> will look at the remaining elements of @INC.
Note that this file handle must be a real file handle (strictly a typeglob,
or reference to a typeglob, blessed or unblessed) - tied file handles will be
ignored and return value processing will stop there.</p>
<p>If the hook is an array reference, its first element must be a subroutine
reference.  This subroutine is called as above, but the first parameter is
the array reference.  This enables to pass indirectly some arguments to
the subroutine.</p>
<p>In other words, you can write:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/push.html">push</a> <span class="i">@INC</span><span class="cm">,</span> \<span class="i">&amp;my_sub</span><span class="sc">;</span></li><li><a name="my_sub"></a>    sub <span class="m">my_sub</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span><span class="i">$coderef</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span>	<span class="c"># $coderef is \&amp;my_sub</span></li><li>	...</li><li>    <span class="s">}</span></li></ol></pre><p>or:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/push.html">push</a> <span class="i">@INC</span><span class="cm">,</span> <span class="s">[</span> \<span class="i">&amp;my_sub</span><span class="cm">,</span> <span class="i">$x</span><span class="cm">,</span> <span class="i">$y</span><span class="cm">,</span> ... <span class="s">]</span><span class="sc">;</span></li><li><a name="my_sub"></a>    sub <span class="m">my_sub</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span><span class="i">$arrayref</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>	<span class="c"># Retrieve $x, $y, ...</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="i">@parameters</span> = <span class="i">@$arrayref</span>[<span class="n">1</span>..<span class="i">$#$arrayref</span>]<span class="sc">;</span></li><li>	...</li><li>    <span class="s">}</span></li></ol></pre><p>If the hook is an object, it must provide an INC method that will be
called as above, the first parameter being the object itself.  (Note that
you must fully qualify the sub&#39;s name, as unqualified <code class="inline"><span class="w">INC</span></code>
 is always forced
into package <code class="inline"><span class="w">main</span></code>
.)  Here is a typical code layout:</p>
<pre class="verbatim"><ol><li>    <span class="c"># In Foo.pm</span></li><li><a name="package-Foo"></a>    package <span class="i">Foo</span><span class="sc">;</span></li><li><a name="new"></a>    sub <span class="m">new</span> <span class="s">{</span> ... <span class="s">}</span></li><li><a name="Foo::INC"></a>    sub <span class="m">Foo::INC</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span><span class="i">$self</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>	...</li><li>    <span class="s">}</span></li><li></li><li>    <span class="c"># In the main program</span></li><li>    <a class="l_k" href="../functions/push.html">push</a> <span class="i">@INC</span><span class="cm">,</span> <span class="w">Foo</span><span class="w">-&gt;new</span><span class="s">(</span>...<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Note that these hooks are also permitted to set the %INC entry
corresponding to the files they have loaded. See <a href="../perlvar.html#%25INC">"%INC" in perlvar</a>.</p>
<p>For a yet-more-powerful import facility, see <a href="../functions/use.html">"use"</a> and <a href="../perlmod.html">perlmod</a>.
-----

function require
Demands a version of Perl specified by VERSION, or demands some semantics
specified by EXPR or by <code class="inline"><span class="i">$_</span></code>
 if EXPR is not supplied.</p>
<p>VERSION may be either a numeric argument such as 5.006, which will be
compared to <code class="inline"><span class="i">$]</span></code>
, or a literal of the form v5.6.1, which will be compared
to <code class="inline"><span class="i">$^V</span></code>
 (aka $PERL_VERSION).  A fatal error is produced at run time if
VERSION is greater than the version of the current Perl interpreter.
Compare with <a href="../functions/use.html">"use"</a>, which can do a similar check at compile time.</p>
<p>Specifying VERSION as a literal of the form v5.6.1 should generally be
avoided, because it leads to misleading error messages under earlier
versions of Perl that do not support this syntax.  The equivalent numeric
version should be used instead.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/require.html">require</a> <span class="v">v5.6.1</span><span class="sc">;</span>	<span class="c"># run time version check</span></li><li>    <a class="l_k" href="../functions/require.html">require</a> <span class="v">5.6.1</span><span class="sc">;</span>	<span class="c"># ditto</span></li><li>    <a class="l_k" href="../functions/require.html">require</a> <span class="n">5.006_001</span><span class="sc">;</span>	<span class="c"># ditto; preferred for backwards compatibility</span></li></ol></pre><p>Otherwise, <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> demands that a library file be included if it
hasn&#39;t already been included.  The file is included via the do-FILE
mechanism, which is essentially just a variety of <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code> with the
caveat that lexical variables in the invoking script will be invisible
to the included code.  Has semantics similar to the following subroutine:</p>
<pre class="verbatim"><ol><li><a name="require"></a>    sub <span class="m">require</span> <span class="s">{</span></li><li>       <a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span><span class="i">$filename</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>       if <span class="s">(</span><a class="l_k" href="../functions/exists.html">exists</a> <span class="i">$INC</span>{<span class="i">$filename</span>}<span class="s">)</span> <span class="s">{</span></li><li>           <a class="l_k" href="../functions/return.html">return</a> <span class="n">1</span> if <span class="i">$INC</span>{<span class="i">$filename</span>}<span class="sc">;</span></li><li>           <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Compilation failed in require&quot;</span><span class="sc">;</span></li><li>       <span class="s">}</span></li><li>       <a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span><span class="i">$realfilename</span><span class="cm">,</span><span class="i">$result</span><span class="s">)</span><span class="sc">;</span></li><li>       <span class="j">ITER:</span> <span class="s">{</span></li><li>           foreach <span class="i">$prefix</span> <span class="s">(</span><span class="i">@INC</span><span class="s">)</span> <span class="s">{</span></li><li>               <span class="i">$realfilename</span> = <span class="q">&quot;$prefix/$filename&quot;</span><span class="sc">;</span></li><li>               if <span class="s">(</span>-f <span class="i">$realfilename</span><span class="s">)</span> <span class="s">{</span></li><li>                   <span class="i">$INC</span>{<span class="i">$filename</span>} = <span class="i">$realfilename</span><span class="sc">;</span></li><li>                   <span class="i">$result</span> = <a class="l_k" href="../functions/do.html">do</a> <span class="i">$realfilename</span><span class="sc">;</span></li><li>                   <a class="l_k" href="../functions/last.html">last</a> <span class="j">ITER</span><span class="sc">;</span></li><li>               <span class="s">}</span></li><li>           <span class="s">}</span></li><li>           <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Can&#39;t find $filename in \@INC&quot;</span><span class="sc">;</span></li><li>       <span class="s">}</span></li><li>       if <span class="s">(</span><span class="i">$@</span><span class="s">)</span> <span class="s">{</span></li><li>           <span class="i">$INC</span>{<span class="i">$filename</span>} = <a class="l_k" href="../functions/undef.html">undef</a><span class="sc">;</span></li><li>           <a class="l_k" href="../functions/die.html">die</a> <span class="i">$@</span><span class="sc">;</span></li><li>       <span class="s">}</span> elsif <span class="s">(</span>!<span class="i">$result</span><span class="s">)</span> <span class="s">{</span></li><li>           <a class="l_k" href="../functions/delete.html">delete</a> <span class="i">$INC</span>{<span class="i">$filename</span>}<span class="sc">;</span></li><li>           <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;$filename did not return true value&quot;</span><span class="sc">;</span></li><li>       <span class="s">}</span> else <span class="s">{</span></li><li>           <a class="l_k" href="../functions/return.html">return</a> <span class="i">$result</span><span class="sc">;</span></li><li>       <span class="s">}</span></li><li>    <span class="s">}</span></li></ol></pre><p>Note that the file will not be included twice under the same specified
name.</p>
<p>The file must return true as the last statement to indicate
successful execution of any initialization code, so it&#39;s customary to
end such a file with <code class="inline"><span class="n">1</span><span class="sc">;</span></code>
 unless you&#39;re sure it&#39;ll return true
otherwise.  But it&#39;s better just to put the <code class="inline"><span class="n">1</span><span class="sc">;</span></code>
, in case you add more
statements.</p>
<p>If EXPR is a bareword, the require assumes a &quot;<i>.pm</i>&quot; extension and
replaces &quot;<i>::</i>&quot; with &quot;<i>/</i>&quot; in the filename for you,
to make it easy to load standard modules.  This form of loading of
modules does not risk altering your namespace.</p>
<p>In other words, if you try this:</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="../functions/require.html">require</a> <span class="w">Foo::Bar</span><span class="sc">;</span>    <span class="c"># a splendid bareword</span></li></ol></pre><p>The require function will actually look for the &quot;<i>Foo/Bar.pm</i>&quot; file in the
directories specified in the <code class="inline"><span class="i">@INC</span></code>
 array.</p>
<p>But if you try this:</p>
<pre class="verbatim"><ol><li>        <span class="i">$class</span> = <span class="q">&#39;Foo::Bar&#39;</span><span class="sc">;</span></li><li>        <a class="l_k" href="../functions/require.html">require</a> <span class="i">$class</span><span class="sc">;</span>	     <span class="c"># $class is not a bareword</span></li><li>    <span class="c">#or</span></li><li>        <a class="l_k" href="../functions/require.html">require</a> <span class="q">&quot;Foo::Bar&quot;</span><span class="sc">;</span>  <span class="c"># not a bareword because of the &quot;&quot;</span></li></ol></pre><p>The require function will look for the &quot;<i>Foo::Bar</i>&quot; file in the @INC array and
will complain about not finding &quot;<i>Foo::Bar</i>&quot; there.  In this case you can do:</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="../functions/eval.html">eval</a> <span class="q">&quot;require $class&quot;</span><span class="sc">;</span></li></ol></pre><p>Now that you understand how <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> looks for files in the case of a
bareword argument, there is a little extra functionality going on behind
the scenes.  Before <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> looks for a &quot;<i>.pm</i>&quot; extension, it will
first look for a similar filename with a &quot;<i>.pmc</i>&quot; extension. If this file
is found, it will be loaded in place of any file ending in a &quot;<i>.pm</i>&quot;
extension.</p>
<p>You can also insert hooks into the import facility, by putting directly
Perl code into the @INC array.  There are three forms of hooks: subroutine
references, array references and blessed objects.</p>
<p>Subroutine references are the simplest case.  When the inclusion system
walks through @INC and encounters a subroutine, this subroutine gets
called with two parameters, the first being a reference to itself, and the
second the name of the file to be included (e.g. &quot;<i>Foo/Bar.pm</i>&quot;).  The
subroutine should return nothing, or a list of up to three values in the
following order:</p>
<ol>
<li>
<p>A filehandle, from which the file will be read.</p>
</li>
<li>
<p>A reference to a subroutine. If there is no filehandle (previous item),
then this subroutine is expected to generate one line of source code per
call, writing the line into <code class="inline"><span class="i">$_</span></code>
 and returning 1, then returning 0 at
&quot;end of file&quot;. If there is a filehandle, then the subroutine will be
called to act as a simple source filter, with the line as read in <code class="inline"><span class="i">$_</span></code>
.
Again, return 1 for each valid line, and 0 after all lines have been
returned.</p>
</li>
<li>
<p>Optional state for the subroutine. The state is passed in as <code class="inline"><span class="i">$_</span>[<span class="n">1</span>]</code>
. A
reference to the subroutine itself is passed in as <code class="inline"><span class="i">$_</span>[<span class="n">0</span>]</code>
.</p>
</li>
</ol>
<p>If an empty list, <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>, or nothing that matches the first 3 values above
is returned then <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> will look at the remaining elements of @INC.
Note that this file handle must be a real file handle (strictly a typeglob,
or reference to a typeglob, blessed or unblessed) - tied file handles will be
ignored and return value processing will stop there.</p>
<p>If the hook is an array reference, its first element must be a subroutine
reference.  This subroutine is called as above, but the first parameter is
the array reference.  This enables to pass indirectly some arguments to
the subroutine.</p>
<p>In other words, you can write:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/push.html">push</a> <span class="i">@INC</span><span class="cm">,</span> \<span class="i">&amp;my_sub</span><span class="sc">;</span></li><li><a name="my_sub"></a>    sub <span class="m">my_sub</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span><span class="i">$coderef</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span>	<span class="c"># $coderef is \&amp;my_sub</span></li><li>	...</li><li>    <span class="s">}</span></li></ol></pre><p>or:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/push.html">push</a> <span class="i">@INC</span><span class="cm">,</span> <span class="s">[</span> \<span class="i">&amp;my_sub</span><span class="cm">,</span> <span class="i">$x</span><span class="cm">,</span> <span class="i">$y</span><span class="cm">,</span> ... <span class="s">]</span><span class="sc">;</span></li><li><a name="my_sub"></a>    sub <span class="m">my_sub</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span><span class="i">$arrayref</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>	<span class="c"># Retrieve $x, $y, ...</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="i">@parameters</span> = <span class="i">@$arrayref</span>[<span class="n">1</span>..<span class="i">$#$arrayref</span>]<span class="sc">;</span></li><li>	...</li><li>    <span class="s">}</span></li></ol></pre><p>If the hook is an object, it must provide an INC method that will be
called as above, the first parameter being the object itself.  (Note that
you must fully qualify the sub&#39;s name, as unqualified <code class="inline"><span class="w">INC</span></code>
 is always forced
into package <code class="inline"><span class="w">main</span></code>
.)  Here is a typical code layout:</p>
<pre class="verbatim"><ol><li>    <span class="c"># In Foo.pm</span></li><li><a name="package-Foo"></a>    package <span class="i">Foo</span><span class="sc">;</span></li><li><a name="new"></a>    sub <span class="m">new</span> <span class="s">{</span> ... <span class="s">}</span></li><li><a name="Foo::INC"></a>    sub <span class="m">Foo::INC</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a> <span class="s">(</span><span class="i">$self</span><span class="cm">,</span> <span class="i">$filename</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>	...</li><li>    <span class="s">}</span></li><li></li><li>    <span class="c"># In the main program</span></li><li>    <a class="l_k" href="../functions/push.html">push</a> <span class="i">@INC</span><span class="cm">,</span> <span class="w">Foo</span><span class="w">-&gt;new</span><span class="s">(</span>...<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Note that these hooks are also permitted to set the %INC entry
corresponding to the files they have loaded. See <a href="../perlvar.html#%25INC">"%INC" in perlvar</a>.</p>
<p>For a yet-more-powerful import facility, see <a href="../functions/use.html">"use"</a> and <a href="../perlmod.html">perlmod</a>.
-----

function reset
expr|
Generally used in a <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code> block at the end of a loop to clear
variables and reset <code class="inline"><span class="q">??</span></code>
 searches so that they work again.  The
expression is interpreted as a list of single characters (hyphens
allowed for ranges).  All variables and arrays beginning with one of
those letters are reset to their pristine state.  If the expression is
omitted, one-match searches (<code class="inline"><span class="q">?pattern?</span></code>
) are reset to match again.  Resets
only variables or searches in the current package.  Always returns
1.  Examples:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/reset.html">reset</a> <span class="q">&#39;X&#39;</span><span class="sc">;</span>		<span class="c"># reset all X variables</span></li><li>    <a class="l_k" href="../functions/reset.html">reset</a> <span class="q">&#39;a-z&#39;</span><span class="sc">;</span>	<span class="c"># reset lower case variables</span></li><li>    <a class="l_k" href="../functions/reset.html">reset</a><span class="sc">;</span>		<span class="c"># just reset ?one-time? searches</span></li></ol></pre><p>Resetting <code class="inline"><span class="q">&quot;A-Z&quot;</span></code>
 is not recommended because you&#39;ll wipe out your
<code class="inline"><span class="i">@ARGV</span></code>
 and <code class="inline"><span class="i">@INC</span></code>
 arrays and your <code class="inline"><span class="i">%ENV</span></code>
 hash.  Resets only package
variables--lexical variables are unaffected, but they clean themselves
up on scope exit anyway, so you&#39;ll probably want to use them instead.
See <a href="../functions/my.html">"my"</a>.
-----

function reset
Generally used in a <code class="inline"><a class="l_k" href="../functions/continue.html">continue</a></code> block at the end of a loop to clear
variables and reset <code class="inline"><span class="q">??</span></code>
 searches so that they work again.  The
expression is interpreted as a list of single characters (hyphens
allowed for ranges).  All variables and arrays beginning with one of
those letters are reset to their pristine state.  If the expression is
omitted, one-match searches (<code class="inline"><span class="q">?pattern?</span></code>
) are reset to match again.  Resets
only variables or searches in the current package.  Always returns
1.  Examples:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/reset.html">reset</a> <span class="q">&#39;X&#39;</span><span class="sc">;</span>		<span class="c"># reset all X variables</span></li><li>    <a class="l_k" href="../functions/reset.html">reset</a> <span class="q">&#39;a-z&#39;</span><span class="sc">;</span>	<span class="c"># reset lower case variables</span></li><li>    <a class="l_k" href="../functions/reset.html">reset</a><span class="sc">;</span>		<span class="c"># just reset ?one-time? searches</span></li></ol></pre><p>Resetting <code class="inline"><span class="q">&quot;A-Z&quot;</span></code>
 is not recommended because you&#39;ll wipe out your
<code class="inline"><span class="i">@ARGV</span></code>
 and <code class="inline"><span class="i">@INC</span></code>
 arrays and your <code class="inline"><span class="i">%ENV</span></code>
 hash.  Resets only package
variables--lexical variables are unaffected, but they clean themselves
up on scope exit anyway, so you&#39;ll probably want to use them instead.
See <a href="../functions/my.html">"my"</a>.
-----

function return
expr|
Returns from a subroutine, <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code>, or <code class="inline"><a class="l_k" href="../functions/do.html">do</a> <span class="w">FILE</span></code>
 with the value
given in EXPR.  Evaluation of EXPR may be in list, scalar, or void
context, depending on how the return value will be used, and the context
may vary from one execution to the next (see <code class="inline"><a class="l_k" href="../functions/wantarray.html">wantarray</a></code>).  If no EXPR
is given, returns an empty list in list context, the undefined value in
scalar context, and (of course) nothing at all in a void context.</p>
<p>(Note that in the absence of an explicit <code class="inline"><a class="l_k" href="../functions/return.html">return</a></code>, a subroutine, eval,
or do FILE will automatically return the value of the last expression
evaluated.)
-----

function return
Returns from a subroutine, <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code>, or <code class="inline"><a class="l_k" href="../functions/do.html">do</a> <span class="w">FILE</span></code>
 with the value
given in EXPR.  Evaluation of EXPR may be in list, scalar, or void
context, depending on how the return value will be used, and the context
may vary from one execution to the next (see <code class="inline"><a class="l_k" href="../functions/wantarray.html">wantarray</a></code>).  If no EXPR
is given, returns an empty list in list context, the undefined value in
scalar context, and (of course) nothing at all in a void context.</p>
<p>(Note that in the absence of an explicit <code class="inline"><a class="l_k" href="../functions/return.html">return</a></code>, a subroutine, eval,
or do FILE will automatically return the value of the last expression
evaluated.)
-----

function reverse
list|
In list context, returns a list value consisting of the elements
of LIST in the opposite order.  In scalar context, concatenates the
elements of LIST and returns a string value with all characters
in the opposite order.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">&quot;, &quot;</span><span class="cm">,</span> <a class="l_k" href="../functions/reverse.html">reverse</a> <span class="q">&quot;world&quot;</span><span class="cm">,</span> <span class="q">&quot;Hello&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># Hello, world</span></li><li></li><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/scalar.html">scalar</a> <a class="l_k" href="../functions/reverse.html">reverse</a> <span class="q">&quot;dlrow ,&quot;</span><span class="cm">,</span> <span class="q">&quot;olleH&quot;</span><span class="sc">;</span>    <span class="c"># Hello, world</span></li></ol></pre><p>Used without arguments in scalar context, reverse() reverses <code class="inline"><span class="i">$_</span></code>
.</p>
<pre class="verbatim"><ol><li>    <span class="i">$_</span> = <span class="q">&quot;dlrow ,olleH&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/reverse.html">reverse</a><span class="sc">;</span>                              <span class="c"># No output, list context</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/scalar.html">scalar</a> <a class="l_k" href="../functions/reverse.html">reverse</a><span class="sc">;</span>                       <span class="c"># Hello, world</span></li></ol></pre><p>This operator is also handy for inverting a hash, although there are some
caveats.  If a value is duplicated in the original hash, only one of those
can be represented as a key in the inverted hash.  Also, this has to
unwind one hash and build a whole new one, which may take some time
on a large hash, such as from a DBM file.</p>
<pre class="verbatim"><ol><li>    <span class="i">%by_name</span> = <a class="l_k" href="../functions/reverse.html">reverse</a> <span class="i">%by_address</span><span class="sc">;</span>	<span class="c"># Invert the hash</span></li></ol><
-----

function rewinddir
dirhandle|
Sets the current position to the beginning of the directory for the
<code class="inline"><a class="l_k" href="../functions/readdir.html">readdir</a></code> routine on DIRHANDLE.
-----

function rindex
str|
substr|
position|
Works just like index() except that it returns the position of the <i>last</i>
occurrence of SUBSTR in STR.  If POSITION is specified, returns the
last occurrence beginning at or before that position.
-----

function rindex
str|
substr|
Works just like index() except that it returns the position of the <i>last</i>
occurrence of SUBSTR in STR.  If POSITION is specified, returns the
last occurrence beginning at or before that position.
-----

function rmdir
filename|
Deletes the directory specified by FILENAME if that directory is
empty.  If it succeeds it returns true, otherwise it returns false and
sets <code class="inline"><span class="i">$!</span></code>
 (errno).  If FILENAME is omitted, uses <code class="inline"><span class="i">$_</span></code>
.</p>
<p>To remove a directory tree recursively (<code class="inline"><span class="w">rm</span> -<span class="w">rf</span></code>
 on unix) look at
the <code class="inline"><span class="w">rmtree</span></code>
 function of the <a href="../File/Path.html">File::Path</a> module.
-----

function rmdir
Deletes the directory specified by FILENAME if that directory is
empty.  If it succeeds it returns true, otherwise it returns false and
sets <code class="inline"><span class="i">$!</span></code>
 (errno).  If FILENAME is omitted, uses <code class="inline"><span class="i">$_</span></code>
.</p>
<p>To remove a directory tree recursively (<code class="inline"><span class="w">rm</span> -<span class="w">rf</span></code>
 on unix) look at
the <code class="inline"><span class="w">rmtree</span></code>
 function of the <a href="../File/Path.html">File::Path</a> module.
-----

function s
///|
The substitution operator.  See <a href="../perlop.html#Regexp-Quote-Like-Operators">"Regexp Quote-Like Operators" in perlop</a>.
-----

function say
filehandle list|
Just like <code class="inline"><a class="l_k" href="../functions/print.html">print</a></code>, but implicitly appends a newline.
<code class="inline"><a class="l_k" href="../functions/say.html">say</a> <span class="i">LIST</span></code>
 is simply an abbreviation for <code class="inline"><span class="s">{</span> <a class="l_k" href="../functions/local.html">local</a> <span class="i">$\</span> = <span class="q">&quot;\n&quot;</span><span class="sc">;</span> <a class="l_k" href="../functions/print.html">print</a>
<span class="i">LIST</span> <span class="s">}</span></code>
.</p>
<p>This keyword is only available when the &quot;say&quot; feature is
enabled: see <a href="../feature.html">feature</a>.
-----

function say
list|
Just like <code class="inline"><a class="l_k" href="../functions/print.html">print</a></code>, but implicitly appends a newline.
<code class="inline"><a class="l_k" href="../functions/say.html">say</a> <span class="i">LIST</span></code>
 is simply an abbreviation for <code class="inline"><span class="s">{</span> <a class="l_k" href="../functions/local.html">local</a> <span class="i">$\</span> = <span class="q">&quot;\n&quot;</span><span class="sc">;</span> <a class="l_k" href="../functions/print.html">print</a>
<span class="i">LIST</span> <span class="s">}</span></code>
.</p>
<p>This keyword is only available when the &quot;say&quot; feature is
enabled: see <a href="../feature.html">feature</a>.
-----

function say
Just like <code class="inline"><a class="l_k" href="../functions/print.html">print</a></code>, but implicitly appends a newline.
<code class="inline"><a class="l_k" href="../functions/say.html">say</a> <span class="i">LIST</span></code>
 is simply an abbreviation for <code class="inline"><span class="s">{</span> <a class="l_k" href="../functions/local.html">local</a> <span class="i">$\</span> = <span class="q">&quot;\n&quot;</span><span class="sc">;</span> <a class="l_k" href="../functions/print.html">print</a>
<span class="i">LIST</span> <span class="s">}</span></code>
.</p>
<p>This keyword is only available when the &quot;say&quot; feature is
enabled: see <a href="../feature.html">feature</a>.
-----

function scalar
expr|
Forces EXPR to be interpreted in scalar context and returns the value
of EXPR.</p>
<pre class="verbatim"><ol><li>    <span class="i">@counts</span> = <span class="s">(</span> <a class="l_k" href="../functions/scalar.html">scalar</a> <span class="i">@a</span><span class="cm">,</span> <a class="l_k" href="../functions/scalar.html">scalar</a> <span class="i">@b</span><span class="cm">,</span> <a class="l_k" href="../functions/scalar.html">scalar</a> <span class="i">@c</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>There is no equivalent operator to force an expression to
be interpolated in list context because in practice, this is never
needed.  If you really wanted to do so, however, you could use
the construction <code class="inline"><span class="i">@</span>{<span class="s">[</span> <span class="s">(</span><span class="w">some</span> <span class="w">expression</span><span class="s">)</span> <span class="s">]</span>}</code>
, but usually a simple
<code class="inline"><span class="s">(</span><span class="w">some</span> <span class="w">expression</span><span class="s">)</span></code>
 suffices.</p>
<p>Because <code class="inline"><a class="l_k" href="../functions/scalar.html">scalar</a></code> is unary operator, if you accidentally use for EXPR a
parenthesized list, this behaves as a scalar comma expression, evaluating
all but the last element in void context and returning the final element
evaluated in scalar context.  This is seldom what you want.</p>
<p>The following single statement:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><a class="l_k" href="../functions/scalar.html">scalar</a><span class="s">(</span><span class="i">&amp;foo</span><span class="cm">,</span><span class="i">$bar</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span><span class="i">$baz</span><span class="sc">;</span></li></ol></pre><p>is the moral equivalent of these two:</p>
<pre class="verbatim"><ol><li>	<span class="i">&amp;foo</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/print.html">print</a><span class="s">(</span><a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$bar</span><span class="s">)</span><span class="cm">,</span><span class="i">$baz</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>See <a href="../perlop.html">perlop</a> for more details on unary operators and the comma operator.
-----

function seek
filehandle|
position|
whence|
Sets FILEHANDLE&#39;s position, just like the <code class="inline"><span class="w">fseek</span></code>
 call of <code class="inline"><span class="w">stdio</span></code>
.
FILEHANDLE may be an expression whose value gives the name of the
filehandle.  The values for WHENCE are <code class="inline"><span class="n">0</span></code>
 to set the new position
<i>in bytes</i> to POSITION, <code class="inline"><span class="n">1</span></code>
 to set it to the current position plus
POSITION, and <code class="inline"><span class="n">2</span></code>
 to set it to EOF plus POSITION (typically
negative).  For WHENCE you may use the constants <code class="inline"><span class="w">SEEK_SET</span></code>
,
<code class="inline"><span class="w">SEEK_CUR</span></code>
, and <code class="inline"><span class="w">SEEK_END</span></code>
 (start of the file, current position, end
of the file) from the Fcntl module.  Returns <code class="inline"><span class="n">1</span></code>
 upon success, <code class="inline"><span class="n">0</span></code>

otherwise.</p>
<p>Note the <i>in bytes</i>: even if the filehandle has been set to
operate on characters (for example by using the <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span><span class="w">utf8</span><span class="s">)</span></code>
 open
layer), tell() will return byte offsets, not character offsets
(because implementing that would render seek() and tell() rather slow).</p>
<p>If you want to position file for <code class="inline"><a class="l_k" href="../functions/sysread.html">sysread</a></code> or <code class="inline"><a class="l_k" href="../functions/syswrite.html">syswrite</a></code>, don&#39;t use
<code class="inline"><a class="l_k" href="../functions/seek.html">seek</a></code>--buffering makes its effect on the file&#39;s system position
unpredictable and non-portable.  Use <code class="inline"><a class="l_k" href="../functions/sysseek.html">sysseek</a></code> instead.</p>
<p>Due to the rules and rigors of ANSI C, on some systems you have to do a
seek whenever you switch between reading and writing.  Amongst other
things, this may have the effect of calling stdio&#39;s clearerr(3).
A WHENCE of <code class="inline"><span class="n">1</span></code>
 (<code class="inline"><span class="w">SEEK_CUR</span></code>
) is useful for not moving the file position:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/seek.html">seek</a><span class="s">(</span><span class="w">TEST</span><span class="cm">,</span><span class="n">0</span><span class="cm">,</span><span class="n">1</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>This is also useful for applications emulating <code class="inline"><span class="w">tail</span> -f</code>
.  Once you hit
EOF on your read, and then sleep for a while, you might have to stick in a
seek() to reset things.  The <code class="inline"><a class="l_k" href="../functions/seek.html">seek</a></code> doesn&#39;t change the current position,
but it <i>does</i> clear the end-of-file condition on the handle, so that the
next <code class="inline"><span class="q">&lt;FILE&gt;</span></code>
 makes Perl try again to read something.  We hope.</p>
<p>If that doesn&#39;t work (some IO implementations are particularly
cantankerous), then you may need something more like this:</p>
<pre class="verbatim"><ol><li>    for <span class="s">(</span><span class="sc">;</span><span class="sc">;</span><span class="s">)</span> <span class="s">{</span></li><li>	for <span class="s">(</span><span class="i">$curpos</span> = <a class="l_k" href="../functions/tell.html">tell</a><span class="s">(</span><span class="w">FILE</span><span class="s">)</span><span class="sc">;</span> <span class="i">$_</span> = <span class="q">&lt;FILE&gt;</span><span class="sc">;</span></li><li>             <span class="i">$curpos</span> = <a class="l_k" href="../functions/tell.html">tell</a><span class="s">(</span><span class="w">FILE</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>	    <span class="c"># search for some stuff and put it into files</span></li><li>	<span class="s">}</span></li><li>	<a class="l_k" href="../functions/sleep.html">sleep</a><span class="s">(</span><span class="i">$for_a_while</span><span class="s">)</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/seek.html">seek</a><span class="s">(</span><span class="w">FILE</span><span class="cm">,</span> <span class="i">$curpos</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol><
-----

function seekdir
dirhandle|
pos|
Sets the current position for the <code class="inline"><a class="l_k" href="../functions/readdir.html">readdir</a></code> routine on DIRHANDLE.  POS
must be a value returned by <code class="inline"><a class="l_k" href="../functions/telldir.html">telldir</a></code>.  <code class="inline"><a class="l_k" href="../functions/seekdir.html">seekdir</a></code> also has the same caveats
about possible directory compaction as the corresponding system library
routine.
-----

function select
filehandle|
Returns the currently selected filehandle.  If FILEHANDLE is supplied,
sets the new current default filehandle for output.  This has two
effects: first, a <code class="inline"><a class="l_k" href="../functions/write.html">write</a></code> or a <code class="inline"><a class="l_k" href="../functions/print.html">print</a></code> without a filehandle will
default to this FILEHANDLE.  Second, references to variables related to
output will refer to this output channel.  For example, if you have to
set the top of form format for more than one output channel, you might
do the following:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/select.html">select</a><span class="s">(</span><span class="w">REPORT1</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$^</span> = <span class="q">&#39;report1_top&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/select.html">select</a><span class="s">(</span><span class="w">REPORT2</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$^</span> = <span class="q">&#39;report2_top&#39;</span><span class="sc">;</span></li></ol></pre><p>FILEHANDLE may be an expression whose value gives the name of the
actual filehandle.  Thus:</p>
<pre class="verbatim"><ol><li>    <span class="i">$oldfh</span> = <a class="l_k" href="../functions/select.html">select</a><span class="s">(</span><span class="w">STDERR</span><span class="s">)</span><span class="sc">;</span> <span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span> <a class="l_k" href="../functions/select.html">select</a><span class="s">(</span><span class="i">$oldfh</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Some programmers may prefer to think of filehandles as objects with
methods, preferring to write the last example as:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">IO::Handle</span><span class="sc">;</span></li><li>    <span class="w">STDERR</span><span class="w">-&gt;autoflush</span><span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="sc">;</span></li></ol><
-----

function select
Returns the currently selected filehandle.  If FILEHANDLE is supplied,
sets the new current default filehandle for output.  This has two
effects: first, a <code class="inline"><a class="l_k" href="../functions/write.html">write</a></code> or a <code class="inline"><a class="l_k" href="../functions/print.html">print</a></code> without a filehandle will
default to this FILEHANDLE.  Second, references to variables related to
output will refer to this output channel.  For example, if you have to
set the top of form format for more than one output channel, you might
do the following:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/select.html">select</a><span class="s">(</span><span class="w">REPORT1</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$^</span> = <span class="q">&#39;report1_top&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/select.html">select</a><span class="s">(</span><span class="w">REPORT2</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$^</span> = <span class="q">&#39;report2_top&#39;</span><span class="sc">;</span></li></ol></pre><p>FILEHANDLE may be an expression whose value gives the name of the
actual filehandle.  Thus:</p>
<pre class="verbatim"><ol><li>    <span class="i">$oldfh</span> = <a class="l_k" href="../functions/select.html">select</a><span class="s">(</span><span class="w">STDERR</span><span class="s">)</span><span class="sc">;</span> <span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span> <a class="l_k" href="../functions/select.html">select</a><span class="s">(</span><span class="i">$oldfh</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Some programmers may prefer to think of filehandles as objects with
methods, preferring to write the last example as:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">IO::Handle</span><span class="sc">;</span></li><li>    <span class="w">STDERR</span><span class="w">-&gt;autoflush</span><span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="sc">;</span></li></ol><
-----

function semctl
id|
semnum|
cmd|
arg|
Calls the System V IPC function <code class="inline"><a class="l_k" href="../functions/semctl.html">semctl</a></code>.  You&#39;ll probably have to say</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">IPC::SysV</span><span class="sc">;</span></li></ol></pre><p>first to get the correct constant definitions.  If CMD is IPC_STAT or
GETALL, then ARG must be a variable that will hold the returned
semid_ds structure or semaphore value array.  Returns like <code class="inline"><a class="l_k" href="../functions/ioctl.html">ioctl</a></code>:
the undefined value for error, &quot;<code class="inline">0 but true</code>&quot; for zero, or the actual
return value otherwise.  The ARG must consist of a vector of native
short integers, which may be created with <code class="inline"><a class="l_k" href="../functions/pack.html">pack("s!",(0)x$nsem)</a></code>.
See also <a href="../perlipc.html#SysV-IPC">"SysV IPC" in perlipc</a>, <code class="inline"><span class="w">IPC::SysV</span></code>
, <code class="inline"><span class="w">IPC::Semaphore</span></code>

documentation.
-----

function semget
key|
nsems|
flags|
Calls the System V IPC function semget.  Returns the semaphore id, or
the undefined value if there is an error.  See also
<a href="../perlipc.html#SysV-IPC">"SysV IPC" in perlipc</a>, <code class="inline"><span class="w">IPC::SysV</span></code>
, <code class="inline"><span class="w">IPC::SysV::Semaphore</span></code>

documentation.
-----

function semop
key|
opstring|
Calls the System V IPC function semop to perform semaphore operations
such as signalling and waiting.  OPSTRING must be a packed array of
semop structures.  Each semop structure can be generated with
<code class="inline"><a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">&quot;s!3&quot;</span><span class="cm">,</span> <span class="i">$semnum</span><span class="cm">,</span> <span class="i">$semop</span><span class="cm">,</span> <span class="i">$semflag</span><span class="s">)</span></code>
.  The length of OPSTRING 
implies the number of semaphore operations.  Returns true if
successful, or false if there is an error.  As an example, the
following code waits on semaphore $semnum of semaphore id $semid:</p>
<pre class="verbatim"><ol><li>    <span class="i">$semop</span> = <a class="l_k" href="../functions/pack.html">pack</a><span class="s">(</span><span class="q">&quot;s!3&quot;</span><span class="cm">,</span> <span class="i">$semnum</span><span class="cm">,</span> <span class="n">-1</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Semaphore trouble: $!\n&quot;</span> unless <a class="l_k" href="../functions/semop.html">semop</a><span class="s">(</span><span class="i">$semid</span><span class="cm">,</span> <span class="i">$semop</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>To signal the semaphore, replace <code class="inline"><span class="n">-1</span></code>
 with <code class="inline"><span class="n">1</span></code>
.  See also
<a href="../perlipc.html#SysV-IPC">"SysV IPC" in perlipc</a>, <code class="inline"><span class="w">IPC::SysV</span></code>
, and <code class="inline"><span class="w">IPC::SysV::Semaphore</span></code>

documentation.
-----

function send
socket|
msg|
flags|
to|
Sends a message on a socket.  Attempts to send the scalar MSG to the
SOCKET filehandle.  Takes the same flags as the system call of the
same name.  On unconnected sockets you must specify a destination to
send TO, in which case it does a C <code class="inline"><span class="w">sendto</span></code>
.  Returns the number of
characters sent, or the undefined value if there is an error.  The C
system call sendmsg(2) is currently unimplemented.  See
<a href="../perlipc.html#UDP%3a-Message-Passing">"UDP: Message Passing" in perlipc</a> for examples.</p>
<p>Note the <i>characters</i>: depending on the status of the socket, either
(8-bit) bytes or characters are sent.  By default all sockets operate
on bytes, but for example if the socket has been changed using
binmode() to operate with the <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span><span class="w">utf8</span><span class="s">)</span></code>
 I/O layer (see
<a href="../functions/open.html">"open"</a>, or the <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> pragma, <a href="../open.html">open</a>), the I/O will operate on UTF-8
encoded Unicode characters, not bytes.  Similarly for the <code class="inline"><span class="j">:</span><span class="w">encoding</span></code>

pragma: in that case pretty much any characters can be sent.
-----

function send
socket|
msg|
flags|
Sends a message on a socket.  Attempts to send the scalar MSG to the
SOCKET filehandle.  Takes the same flags as the system call of the
same name.  On unconnected sockets you must specify a destination to
send TO, in which case it does a C <code class="inline"><span class="w">sendto</span></code>
.  Returns the number of
characters sent, or the undefined value if there is an error.  The C
system call sendmsg(2) is currently unimplemented.  See
<a href="../perlipc.html#UDP%3a-Message-Passing">"UDP: Message Passing" in perlipc</a> for examples.</p>
<p>Note the <i>characters</i>: depending on the status of the socket, either
(8-bit) bytes or characters are sent.  By default all sockets operate
on bytes, but for example if the socket has been changed using
binmode() to operate with the <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span><span class="w">utf8</span><span class="s">)</span></code>
 I/O layer (see
<a href="../functions/open.html">"open"</a>, or the <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> pragma, <a href="../open.html">open</a>), the I/O will operate on UTF-8
encoded Unicode characters, not bytes.  Similarly for the <code class="inline"><span class="j">:</span><span class="w">encoding</span></code>

pragma: in that case pretty much any characters can be sent.
-----

function setgrent
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function sethostent
stayopen|
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function setnetent
stayopen|
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function setpgrp
pid|
pgrp|
Sets the current process group for the specified PID, <code class="inline"><span class="n">0</span></code>
 for the current
process.  Will produce a fatal error if used on a machine that doesn&#39;t
implement POSIX setpgid(2) or BSD setpgrp(2).  If the arguments are omitted,
it defaults to <code class="inline"><span class="n">0</span><span class="cm">,</span><span class="n">0</span></code>
.  Note that the BSD 4.2 version of <code class="inline"><a class="l_k" href="../functions/setpgrp.html">setpgrp</a></code> does not
accept any arguments, so only <code class="inline"><a class="l_k" href="../functions/setpgrp.html">setpgrp(0,0)</a></code> is portable.  See also
<code class="inline"><span class="i">POSIX::setsid</span><span class="s">(</span><span class="s">)</span></code>
.
-----

function setpriority
which|
who|
priority|
Sets the current priority for a process, a process group, or a user.
(See setpriority(2).)  Will produce a fatal error if used on a machine
that doesn&#39;t implement setpriority(2).
-----

function setprotoent
stayopen|
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function setpwent
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function setservent
stayopen|
These routines perform the same functions as their counterparts in the
system library.  In list context, the return values from the
various get routines are as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span></li><li>       <span class="i">$quota</span><span class="cm">,</span><span class="i">$comment</span><span class="cm">,</span><span class="i">$gcos</span><span class="cm">,</span><span class="i">$dir</span><span class="cm">,</span><span class="i">$shell</span><span class="cm">,</span><span class="i">$expire</span><span class="s">)</span> = <span class="w">getpw</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$passwd</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$members</span><span class="s">)</span> = <span class="w">getgr</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$length</span><span class="cm">,</span><span class="i">@addrs</span><span class="s">)</span> = <span class="w">gethost</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$addrtype</span><span class="cm">,</span><span class="i">$net</span><span class="s">)</span> = <span class="w">getnet</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getproto</span>*</li><li>    <span class="s">(</span><span class="i">$name</span><span class="cm">,</span><span class="i">$aliases</span><span class="cm">,</span><span class="i">$port</span><span class="cm">,</span><span class="i">$proto</span><span class="s">)</span> = <span class="w">getserv</span>*</li></ol></pre><p>(If the entry doesn&#39;t exist you get a null list.)</p>
<p>The exact meaning of the $gcos field varies but it usually contains
the real name of the user (as opposed to the login name) and other
information pertaining to the user.  Beware, however, that in many
system users are able to change this information and therefore it
cannot be trusted and therefore the $gcos is tainted (see
<a href="../perlsec.html">perlsec</a>).  The $passwd and $shell, user&#39;s encrypted password and
login shell, are also tainted, because of the same reason.</p>
<p>In scalar context, you get the name, unless the function was a
lookup by name, in which case you get the other thing, whatever it is.
(If the entry doesn&#39;t exist you get the undefined value.)  For example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$uid</span>   = <a class="l_k" href="../functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwuid.html">getpwuid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getpwent.html">getpwent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$gid</span>   = <a class="l_k" href="../functions/getgrnam.html">getgrnam</a><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrgid.html">getgrgid</a><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/getgrent.html">getgrent</a><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#etc.</span></li></ol></pre><p>In <i>getpw*()</i> the fields $quota, $comment, and $expire are special
cases in the sense that in many systems they are unsupported.  If the
$quota is unsupported, it is an empty scalar.  If it is supported, it
usually encodes the disk quota.  If the $comment field is unsupported,
it is an empty scalar.  If it is supported it usually encodes some
administrative comment about the user.  In some systems the $quota
field may be $change or $age, fields that have to do with password
aging.  In some systems the $comment field may be $class.  The $expire
field, if present, encodes the expiration period of the account or the
password.  For the availability and the exact meaning of these fields
in your system, please consult your getpwnam(3) documentation and your
<i>pwd.h</i> file.  You can also find out from within Perl what your
$quota and $comment fields mean and whether you have the $expire field
by using the <code class="inline"><span class="w">Config</span></code>
 module and the values <code class="inline"><span class="w">d_pwquota</span></code>
, <code class="inline"><span class="w">d_pwage</span></code>
,
<code class="inline"><span class="w">d_pwchange</span></code>
, <code class="inline"><span class="w">d_pwcomment</span></code>
, and <code class="inline"><span class="w">d_pwexpire</span></code>
.  Shadow password
files are only supported if your vendor has implemented them in the
intuitive fashion that calling the regular C library routines gets the
shadow versions if you&#39;re running under privilege or if there exists
the shadow(3) functions as found in System V (this includes Solaris
and Linux.)  Those systems that implement a proprietary shadow password
facility are unlikely to be supported.</p>
<p>The $members value returned by <i>getgr*()</i> is a space separated list of
the login names of the members of the group.</p>
<p>For the <i>gethost*()</i> functions, if the <code class="inline"><span class="w">h_errno</span></code>
 variable is supported in
C, it will be returned to you via <code class="inline"><span class="i">$?</span></code>
 if the function call fails.  The
<code class="inline"><span class="i">@addrs</span></code>
 value returned by a successful call is a list of the raw
addresses returned by the corresponding system library call.  In the
Internet domain, each address is four bytes long and you can unpack it
by saying something like:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span><span class="i">$b</span><span class="cm">,</span><span class="i">$c</span><span class="cm">,</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">'W4'</span><span class="cm">,</span><span class="i">$addr</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The Socket library makes this slightly easier:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$iaddr</span> = <span class="i">inet_aton</span><span class="s">(</span><span class="q">&quot;127.1&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c"># or whatever address</span></li><li>    <span class="i">$name</span>  = <a class="l_k" href="../functions/gethostbyaddr.html">gethostbyaddr</a><span class="s">(</span><span class="i">$iaddr</span><span class="cm">,</span> <span class="w">AF_INET</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># or going the other way</span></li><li>    <span class="i">$straddr</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$iaddr</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the opposite way, to resolve a hostname to the IP address
you can write this:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <span class="i">$packed_ip</span> = <a class="l_k" href="../functions/gethostbyname.html">gethostbyname</a><span class="s">(</span><span class="q">&quot;www.perl.org&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="../functions/defined.html">defined</a> <span class="i">$packed_ip</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$ip_address</span> = <span class="i">inet_ntoa</span><span class="s">(</span><span class="i">$packed_ip</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Make sure &lt;gethostbyname()&gt; is called in SCALAR context and that
its return value is checked for definedness.</p>
<p>If you get tired of remembering which element of the return list
contains which return value, by-name interfaces are provided
in standard modules: <code class="inline"><span class="w">File::stat</span></code>
, <code class="inline"><span class="w">Net::hostent</span></code>
, <code class="inline"><span class="w">Net::netent</span></code>
,
<code class="inline"><span class="w">Net::protoent</span></code>
, <code class="inline"><span class="w">Net::servent</span></code>
, <code class="inline"><span class="w">Time::gmtime</span></code>
, <code class="inline"><span class="w">Time::localtime</span></code>
,
and <code class="inline"><span class="w">User::grent</span></code>
.  These override the normal built-ins, supplying
versions that return objects with the appropriate names
for each field.  For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/use.html">use</a> <span class="w">User::pwent</span><span class="sc">;</span></li><li>   <span class="i">$is_his</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="i">-&gt;uid</span> == <span class="i">pwent</span><span class="s">(</span><span class="i">$whoever</span><span class="s">)</span><span class="i">-&gt;uid</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Even though it looks like they&#39;re the same method calls (uid),
they aren&#39;t, because a <code class="inline"><span class="w">File::stat</span></code>
 object is different from
a <code class="inline"><span class="w">User::pwent</span></code>
 object.
-----

function setsockopt
socket|
level|
optname|
optval|
Sets the socket option requested.  Returns undefined if there is an
error.  Use integer constants provided by the <code class="inline"><span class="w">Socket</span></code>
 module for
LEVEL and OPNAME.  Values for LEVEL can also be obtained from
getprotobyname.  OPTVAL might either be a packed string or an integer.
An integer OPTVAL is shorthand for pack(&quot;i&quot;, OPTVAL).</p>
<p>An example disabling the Nagle&#39;s algorithm for a socket:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span> <span class="q">qw(IPPROTO_TCP TCP_NODELAY)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/setsockopt.html">setsockopt</a><span class="s">(</span><span class="i">$socket</span><span class="cm">,</span> <span class="w">IPPROTO_TCP</span><span class="cm">,</span> <span class="w">TCP_NODELAY</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li></ol><
-----

function shift
array|
Shifts the first value of the array off and returns it, shortening the
array by 1 and moving everything down.  If there are no elements in the
array, returns the undefined value.  If ARRAY is omitted, shifts the
<code class="inline"><span class="i">@_</span></code>
 array within the lexical scope of subroutines and formats, and the
<code class="inline"><span class="i">@ARGV</span></code>
 array outside of a subroutine and also within the lexical scopes
established by the <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a> <span class="w">STRING</span></code>
, <code class="inline">BEGIN <span class="s">{</span><span class="s">}</span></code>
, <code class="inline">INIT <span class="s">{</span><span class="s">}</span></code>
, <code class="inline">CHECK <span class="s">{</span><span class="s">}</span></code>
,
<code class="inline">UNITCHECK <span class="s">{</span><span class="s">}</span></code>
 and <code class="inline">END <span class="s">{</span><span class="s">}</span></code>
 constructs.</p>
<p>See also <code class="inline"><a class="l_k" href="../functions/unshift.html">unshift</a></code>, <code class="inline"><a class="l_k" href="../functions/push.html">push</a></code>, and <code class="inline"><a class="l_k" href="../functions/pop.html">pop</a></code>.  <code class="inline"><a class="l_k" href="../functions/shift.html">shift</a></code> and <code class="inline"><a class="l_k" href="../functions/unshift.html">unshift</a></code> do the
same thing to the left end of an array that <code class="inline"><a class="l_k" href="../functions/pop.html">pop</a></code> and <code class="inline"><a class="l_k" href="../functions/push.html">push</a></code> do to the
right end.
-----

function shift
Shifts the first value of the array off and returns it, shortening the
array by 1 and moving everything down.  If there are no elements in the
array, returns the undefined value.  If ARRAY is omitted, shifts the
<code class="inline"><span class="i">@_</span></code>
 array within the lexical scope of subroutines and formats, and the
<code class="inline"><span class="i">@ARGV</span></code>
 array outside of a subroutine and also within the lexical scopes
established by the <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a> <span class="w">STRING</span></code>
, <code class="inline">BEGIN <span class="s">{</span><span class="s">}</span></code>
, <code class="inline">INIT <span class="s">{</span><span class="s">}</span></code>
, <code class="inline">CHECK <span class="s">{</span><span class="s">}</span></code>
,
<code class="inline">UNITCHECK <span class="s">{</span><span class="s">}</span></code>
 and <code class="inline">END <span class="s">{</span><span class="s">}</span></code>
 constructs.</p>
<p>See also <code class="inline"><a class="l_k" href="../functions/unshift.html">unshift</a></code>, <code class="inline"><a class="l_k" href="../functions/push.html">push</a></code>, and <code class="inline"><a class="l_k" href="../functions/pop.html">pop</a></code>.  <code class="inline"><a class="l_k" href="../functions/shift.html">shift</a></code> and <code class="inline"><a class="l_k" href="../functions/unshift.html">unshift</a></code> do the
same thing to the left end of an array that <code class="inline"><a class="l_k" href="../functions/pop.html">pop</a></code> and <code class="inline"><a class="l_k" href="../functions/push.html">push</a></code> do to the
right end.
-----

function shmctl
id|
cmd|
arg|
Calls the System V IPC function shmctl.  You&#39;ll probably have to say</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">IPC::SysV</span><span class="sc">;</span></li></ol></pre><p>first to get the correct constant definitions.  If CMD is <code class="inline"><span class="w">IPC_STAT</span></code>
,
then ARG must be a variable that will hold the returned <code class="inline"><span class="w">shmid_ds</span></code>

structure.  Returns like ioctl: the undefined value for error, &quot;<code class="inline"><span class="n">0</span></code>
 but
true&quot; for zero, or the actual return value otherwise.
See also <a href="../perlipc.html#SysV-IPC">"SysV IPC" in perlipc</a> and <code class="inline"><span class="w">IPC::SysV</span></code>
 documentation.
-----

function shmget
key|
size|
flags|
Calls the System V IPC function shmget.  Returns the shared memory
segment id, or the undefined value if there is an error.
See also <a href="../perlipc.html#SysV-IPC">"SysV IPC" in perlipc</a> and <code class="inline"><span class="w">IPC::SysV</span></code>
 documentation.
-----

function shmread
id|
var|
pos|
size|
-----

function shmwrite
id|
string|
pos|
size|
Reads or writes the System V shared memory segment ID starting at
position POS for size SIZE by attaching to it, copying in/out, and
detaching from it.  When reading, VAR must be a variable that will
hold the data read.  When writing, if STRING is too long, only SIZE
bytes are used; if STRING is too short, nulls are written to fill out
SIZE bytes.  Return true if successful, or false if there is an error.
shmread() taints the variable. See also <a href="../perlipc.html#SysV-IPC">"SysV IPC" in perlipc</a>,
<code class="inline"><span class="w">IPC::SysV</span></code>
 documentation, and the <code class="inline"><span class="w">IPC::Shareable</span></code>
 module from CPAN.
-----

function shutdown
socket|
how|
Shuts down a socket connection in the manner indicated by HOW, which
has the same interpretation as in the system call of the same name.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/shutdown.html">shutdown</a><span class="s">(</span><span class="w">SOCKET</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span>    <span class="c"># I/we have stopped reading data</span></li><li>    <a class="l_k" href="../functions/shutdown.html">shutdown</a><span class="s">(</span><span class="w">SOCKET</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span>    <span class="c"># I/we have stopped writing data</span></li><li>    <a class="l_k" href="../functions/shutdown.html">shutdown</a><span class="s">(</span><span class="w">SOCKET</span><span class="cm">,</span> <span class="n">2</span><span class="s">)</span><span class="sc">;</span>    <span class="c"># I/we have stopped using this socket</span></li></ol></pre><p>This is useful with sockets when you want to tell the other
side you&#39;re done writing but not done reading, or vice versa.
It&#39;s also a more insistent form of close because it also
disables the file descriptor in any forked copies in other
processes.</p>
<p>Returns <code class="inline"><span class="n">1</span></code>
 for success. In the case of error, returns <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> if
the first argument is not a valid filehandle, or returns <code class="inline"><span class="n">0</span></code>
 and sets
<code class="inline"><span class="i">$!</span></code>
 for any other failure.
-----

function sin
expr|
Returns the sine of EXPR (expressed in radians).  If EXPR is omitted,
returns sine of <code class="inline"><span class="i">$_</span></code>
.</p>
<p>For the inverse sine operation, you may use the <code class="inline"><span class="w">Math::Trig::asin</span></code>

function, or use this relation:</p>
<pre class="verbatim"><ol><li><a name="asin"></a>    sub <span class="m">asin</span> <span class="s">{</span> <a class="l_k" href="../functions/atan2.html">atan2</a><span class="s">(</span><span class="i">$_</span>[<span class="n">0</span>]<span class="cm">,</span> <a class="l_k" href="../functions/sqrt.html">sqrt</a><span class="s">(</span><span class="n">1</span> - <span class="i">$_</span>[<span class="n">0</span>] * <span class="i">$_</span>[<span class="n">0</span>]<span class="s">)</span><span class="s">)</span> <span class="s">}</span></li></ol><
-----

function sin
Returns the sine of EXPR (expressed in radians).  If EXPR is omitted,
returns sine of <code class="inline"><span class="i">$_</span></code>
.</p>
<p>For the inverse sine operation, you may use the <code class="inline"><span class="w">Math::Trig::asin</span></code>

function, or use this relation:</p>
<pre class="verbatim"><ol><li><a name="asin"></a>    sub <span class="m">asin</span> <span class="s">{</span> <a class="l_k" href="../functions/atan2.html">atan2</a><span class="s">(</span><span class="i">$_</span>[<span class="n">0</span>]<span class="cm">,</span> <a class="l_k" href="../functions/sqrt.html">sqrt</a><span class="s">(</span><span class="n">1</span> - <span class="i">$_</span>[<span class="n">0</span>] * <span class="i">$_</span>[<span class="n">0</span>]<span class="s">)</span><span class="s">)</span> <span class="s">}</span></li></ol><
-----

function sleep
expr|
Causes the script to sleep for EXPR seconds, or forever if no EXPR.
Returns the number of seconds actually slept.</p>
<p>May be interrupted if the process receives a signal such as <code class="inline"><span class="w">SIGALRM</span></code>
.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/eval.html">eval</a> <span class="s">{</span></li><li>        <a class="l_k" href="../functions/local.html">local</a> <span class="i">$SIG</span>{<span class="w">ALARM</span>} = <a class="l_k" href="../functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Alarm!\n&quot;</span> <span class="s">}</span><span class="sc">;</span></li><li>        <a class="l_k" href="../functions/sleep.html">sleep</a><span class="sc">;</span></li><li>    <span class="s">}</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/die.html">die</a> <span class="i">$@</span> unless <span class="i">$@</span> eq <span class="q">&quot;Alarm!\n&quot;</span><span class="sc">;</span></li></ol></pre><p>You probably cannot mix <code class="inline"><a class="l_k" href="../functions/alarm.html">alarm</a></code> and <code class="inline"><a class="l_k" href="../functions/sleep.html">sleep</a></code> calls, because <code class="inline"><a class="l_k" href="../functions/sleep.html">sleep</a></code>
is often implemented using <code class="inline"><a class="l_k" href="../functions/alarm.html">alarm</a></code>.</p>
<p>On some older systems, it may sleep up to a full second less than what
you requested, depending on how it counts seconds.  Most modern systems
always sleep the full amount.  They may appear to sleep longer than that,
however, because your process might not be scheduled right away in a
busy multitasking system.</p>
<p>For delays of finer granularity than one second, the Time::HiRes module
(from CPAN, and starting from Perl 5.8 part of the standard
distribution) provides usleep().  You may also use Perl&#39;s four-argument
version of select() leaving the first three arguments undefined, or you
might be able to use the <code class="inline"><a class="l_k" href="../functions/syscall.html">syscall</a></code> interface to access setitimer(2) if
your system supports it. See <a href="../perlfaq8.html">perlfaq8</a> for details.</p>
<p>See also the POSIX module&#39;s <code class="inline"><span class="w">pause</span></code>
 function.
-----

function sleep
Causes the script to sleep for EXPR seconds, or forever if no EXPR.
Returns the number of seconds actually slept.</p>
<p>May be interrupted if the process receives a signal such as <code class="inline"><span class="w">SIGALRM</span></code>
.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/eval.html">eval</a> <span class="s">{</span></li><li>        <a class="l_k" href="../functions/local.html">local</a> <span class="i">$SIG</span>{<span class="w">ALARM</span>} = <a class="l_k" href="../functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;Alarm!\n&quot;</span> <span class="s">}</span><span class="sc">;</span></li><li>        <a class="l_k" href="../functions/sleep.html">sleep</a><span class="sc">;</span></li><li>    <span class="s">}</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/die.html">die</a> <span class="i">$@</span> unless <span class="i">$@</span> eq <span class="q">&quot;Alarm!\n&quot;</span><span class="sc">;</span></li></ol></pre><p>You probably cannot mix <code class="inline"><a class="l_k" href="../functions/alarm.html">alarm</a></code> and <code class="inline"><a class="l_k" href="../functions/sleep.html">sleep</a></code> calls, because <code class="inline"><a class="l_k" href="../functions/sleep.html">sleep</a></code>
is often implemented using <code class="inline"><a class="l_k" href="../functions/alarm.html">alarm</a></code>.</p>
<p>On some older systems, it may sleep up to a full second less than what
you requested, depending on how it counts seconds.  Most modern systems
always sleep the full amount.  They may appear to sleep longer than that,
however, because your process might not be scheduled right away in a
busy multitasking system.</p>
<p>For delays of finer granularity than one second, the Time::HiRes module
(from CPAN, and starting from Perl 5.8 part of the standard
distribution) provides usleep().  You may also use Perl&#39;s four-argument
version of select() leaving the first three arguments undefined, or you
might be able to use the <code class="inline"><a class="l_k" href="../functions/syscall.html">syscall</a></code> interface to access setitimer(2) if
your system supports it. See <a href="../perlfaq8.html">perlfaq8</a> for details.</p>
<p>See also the POSIX module&#39;s <code class="inline"><span class="w">pause</span></code>
 function.
-----

function socket
socket|
domain|
type|
protocol|
Opens a socket of the specified kind and attaches it to filehandle
SOCKET.  DOMAIN, TYPE, and PROTOCOL are specified the same as for
the system call of the same name.  You should <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span></code>
 first
to get the proper definitions imported.  See the examples in
<a href="../perlipc.html#Sockets%3a-Client%2fServer-Communication">"Sockets: Client/Server Communication" in perlipc</a>.</p>
<p>On systems that support a close-on-exec flag on files, the flag will
be set for the newly opened file descriptor, as determined by the
value of $^F.  See <a href="../perlvar.html#%24%5eF">"$^F" in perlvar</a>.
-----

function socketpair
socket1|
socket2|
domain|
type|
protocol|
Creates an unnamed pair of sockets in the specified domain, of the
specified type.  DOMAIN, TYPE, and PROTOCOL are specified the same as
for the system call of the same name.  If unimplemented, yields a fatal
error.  Returns true if successful.</p>
<p>On systems that support a close-on-exec flag on files, the flag will
be set for the newly opened file descriptors, as determined by the value
of $^F.  See <a href="../perlvar.html#%24%5eF">"$^F" in perlvar</a>.</p>
<p>Some systems defined <code class="inline"><a class="l_k" href="../functions/pipe.html">pipe</a></code> in terms of <code class="inline"><a class="l_k" href="../functions/socketpair.html">socketpair</a></code>, in which a call
to <code class="inline"><a class="l_k" href="../functions/pipe.html">pipe</a><span class="s">(</span><span class="w">Rdr</span><span class="cm">,</span> <span class="w">Wtr</span><span class="s">)</span></code>
 is essentially:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Socket</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/socketpair.html">socketpair</a><span class="s">(</span><span class="w">Rdr</span><span class="cm">,</span> <span class="w">Wtr</span><span class="cm">,</span> <span class="w">AF_UNIX</span><span class="cm">,</span> <span class="w">SOCK_STREAM</span><span class="cm">,</span> <span class="w">PF_UNSPEC</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/shutdown.html">shutdown</a><span class="s">(</span><span class="w">Rdr</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span>        <span class="c"># no more writing for reader</span></li><li>    <a class="l_k" href="../functions/shutdown.html">shutdown</a><span class="s">(</span><span class="w">Wtr</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span>        <span class="c"># no more reading for writer</span></li></ol></pre><p>See <a href="../perlipc.html">perlipc</a> for an example of socketpair use.  Perl 5.8 and later will
emulate socketpair using IP sockets to localhost if your system implements
sockets but not socketpair.
-----

function sort
subname list|
In list context, this sorts the LIST and returns the sorted list value.
In scalar context, the behaviour of <code class="inline"><a class="l_k" href="../functions/sort.html">sort()</a></code> is undefined.</p>
<p>If SUBNAME or BLOCK is omitted, <code class="inline"><a class="l_k" href="../functions/sort.html">sort</a></code>s in standard string comparison
order.  If SUBNAME is specified, it gives the name of a subroutine
that returns an integer less than, equal to, or greater than <code class="inline"><span class="n">0</span></code>
,
depending on how the elements of the list are to be ordered.  (The <code class="inline">&lt;=&gt;</code>
 and <code class="inline">cmp</code>
 operators are extremely useful in such routines.)
SUBNAME may be a scalar variable name (unsubscripted), in which case
the value provides the name of (or a reference to) the actual
subroutine to use.  In place of a SUBNAME, you can provide a BLOCK as
an anonymous, in-line sort subroutine.</p>
<p>If the subroutine&#39;s prototype is <code class="inline"><span class="s">(</span><span class="i">$$</span><span class="s">)</span></code>
, the elements to be compared
are passed by reference in <code class="inline"><span class="i">@_</span></code>
, as for a normal subroutine.  This is
slower than unprototyped subroutines, where the elements to be
compared are passed into the subroutine
as the package global variables $a and $b (see example below).  Note that
in the latter case, it is usually counter-productive to declare $a and
$b as lexicals.</p>
<p>The values to be compared are always passed by reference and should not
be modified.</p>
<p>You also cannot exit out of the sort block or subroutine using any of the
loop control operators described in <a href="../perlsyn.html">perlsyn</a> or with <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code>.</p>
<p>When <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">locale</span></code>
 is in effect, <code class="inline"><a class="l_k" href="../functions/sort.html">sort</a> <span class="i">LIST</span></code>
 sorts LIST according to the
current collation locale.  See <a href="../perllocale.html">perllocale</a>.</p>
<p>sort() returns aliases into the original list, much as a for loop&#39;s index
variable aliases the list elements.  That is, modifying an element of a
list returned by sort() (for example, in a <code class="inline">foreach</code>
, <code class="inline"><a class="l_k" href="../functions/map.html">map</a></code> or <code class="inline"><a class="l_k" href="../functions/grep.html">grep</a></code>)
actually modifies the element in the original list.  This is usually
something to be avoided when writing clear code.</p>
<p>Perl 5.6 and earlier used a quicksort algorithm to implement sort.
That algorithm was not stable, and <i>could</i> go quadratic.  (A <i>stable</i> sort
preserves the input order of elements that compare equal.  Although
quicksort&#39;s run time is O(NlogN) when averaged over all arrays of
length N, the time can be O(N**2), <i>quadratic</i> behavior, for some
inputs.)  In 5.7, the quicksort implementation was replaced with
a stable mergesort algorithm whose worst-case behavior is O(NlogN).
But benchmarks indicated that for some inputs, on some platforms,
the original quicksort was faster.  5.8 has a sort pragma for
limited control of the sort.  Its rather blunt control of the
underlying algorithm may not persist into future Perls, but the
ability to characterize the input or output in implementation
independent ways quite probably will.  See <a href="../sort.html">the sort pragma</a>.</p>
<p>Examples:</p>
<pre class="verbatim"><ol><li>    <span class="c"># sort lexically</span></li><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">@files</span><span class="sc">;</span></li><li></li><li>    <span class="c"># same thing, but with explicit sort routine</span></li><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><span class="i">$a</span> cmp <span class="i">$b</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li><li></li><li>    <span class="c"># now case-insensitively</span></li><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$a</span><span class="s">)</span> cmp <a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$b</span><span class="s">)</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li><li></li><li>    <span class="c"># same thing in reversed order</span></li><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><span class="i">$b</span> cmp <span class="i">$a</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li><li></li><li>    <span class="c"># sort numerically ascending</span></li><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><span class="i">$a</span> &lt;=&gt; <span class="i">$b</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li><li></li><li>    <span class="c"># sort numerically descending</span></li><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><span class="i">$b</span> &lt;=&gt; <span class="i">$a</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li><li></li><li>    <span class="c"># this sorts the %age hash by value instead of key</span></li><li>    <span class="c"># using an in-line function</span></li><li>    <span class="i">@eldest</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$age</span>{<span class="i">$b</span>} &lt;=&gt; <span class="i">$age</span>{<span class="i">$a</span>} <span class="s">}</span> <a class="l_k" href="../functions/keys.html">keys</a> <span class="i">%age</span><span class="sc">;</span></li><li></li><li>    <span class="c"># sort using explicit subroutine name</span></li><li><a name="byage"></a>    sub <span class="m">byage</span> <span class="s">{</span></li><li>	<span class="i">$age</span>{<span class="i">$a</span>} &lt;=&gt; <span class="i">$age</span>{<span class="i">$b</span>}<span class="sc">;</span>	<span class="c"># presuming numeric</span></li><li>    <span class="s">}</span></li><li>    <span class="i">@sortedclass</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">byage</span> <span class="i">@class</span><span class="sc">;</span></li><li></li><li><a name="backwards"></a>    sub <span class="m">backwards</span> <span class="s">{</span> <span class="i">$b</span> cmp <span class="i">$a</span> <span class="s">}</span></li><li>    <span class="i">@harry</span>  = <span class="q">qw(dog cat x Cain Abel)</span><span class="sc">;</span></li><li>    <span class="i">@george</span> = <span class="q">qw(gone chased yz Punished Axed)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">@harry</span><span class="sc">;</span></li><li>	    <span class="c"># prints AbelCaincatdogx</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">backwards</span> <span class="i">@harry</span><span class="sc">;</span></li><li>	    <span class="c"># prints xdogcatCainAbel</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">@george</span><span class="cm">,</span> <span class="q">&#39;to&#39;</span><span class="cm">,</span> <span class="i">@harry</span><span class="sc">;</span></li><li>	    <span class="c"># prints AbelAxedCainPunishedcatchaseddoggonetoxyz</span></li><li></li><li>    <span class="c"># inefficiently sort by descending numeric compare using</span></li><li>    <span class="c"># the first integer after the first = sign, or the</span></li><li>    <span class="c"># whole record case-insensitively otherwise</span></li><li></li><li>    <span class="i">@new</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span></li><li>	<span class="s">(</span><span class="i">$b</span> =~ <span class="q">/=(\d+)/</span><span class="s">)</span>[<span class="n">0</span>] &lt;=&gt; <span class="s">(</span><span class="i">$a</span> =~ <span class="q">/=(\d+)/</span><span class="s">)</span>[<span class="n">0</span>]</li><li>			    ||</li><li>	            <a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$a</span><span class="s">)</span>  cmp  <a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$b</span><span class="s">)</span></li><li>    <span class="s">}</span> <span class="i">@old</span><span class="sc">;</span></li><li></li><li>    <span class="c"># same thing, but much more efficiently;</span></li><li>    <span class="c"># we&#39;ll build auxiliary indices instead</span></li><li>    <span class="c"># for speed</span></li><li>    <span class="i">@nums</span> = <span class="i">@caps</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    for <span class="s">(</span><span class="i">@old</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/push.html">push</a> <span class="i">@nums</span><span class="cm">,</span> <span class="q">/=(\d+)/</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/push.html">push</a> <span class="i">@caps</span><span class="cm">,</span> <a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="i">@new</span> = <span class="i">@old</span>[ <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span></li><li>			<span class="i">$nums</span>[<span class="i">$b</span>] &lt;=&gt; <span class="i">$nums</span>[<span class="i">$a</span>]</li><li>				 ||</li><li>			<span class="i">$caps</span>[<span class="i">$a</span>] cmp <span class="i">$caps</span>[<span class="i">$b</span>]</li><li>		       <span class="s">}</span> <span class="n">0</span>..<span class="i">$#old</span></li><li>	       ]<span class="sc">;</span></li><li></li><li>    <span class="c"># same thing, but without any temps</span></li><li>    <span class="i">@new</span> = <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span> <span class="i">$_</span>-&gt;[<span class="n">0</span>] <span class="s">}</span></li><li>           <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$b</span>-&gt;[<span class="n">1</span>] &lt;=&gt; <span class="i">$a</span>-&gt;[<span class="n">1</span>]</li><li>                           ||</li><li>                  <span class="i">$a</span>-&gt;[<span class="n">2</span>] cmp <span class="i">$b</span>-&gt;[<span class="n">2</span>]</li><li>           <span class="s">}</span> <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span> <span class="s">[</span><span class="i">$_</span><span class="cm">,</span> <span class="q">/=(\d+)/</span><span class="cm">,</span> <a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="s">]</span> <span class="s">}</span> <span class="i">@old</span><span class="sc">;</span></li><li></li><li>    <span class="c"># using a prototype allows you to use any comparison subroutine</span></li><li>    <span class="c"># as a sort subroutine (including other package&#39;s subroutines)</span></li><li><a name="package-other"></a>    package <span class="i">other</span><span class="sc">;</span></li><li><a name="backwards-1"></a>    sub <span class="m">backwards ($$)</span> <span class="s">{</span> <span class="i">$_</span>[<span class="n">1</span>] cmp <span class="i">$_</span>[<span class="n">0</span>]<span class="sc">;</span> <span class="s">}</span>	<span class="c"># $a and $b are not set here</span></li><li></li><li><a name="package-main"></a>    package <span class="i">main</span><span class="sc">;</span></li><li>    <span class="i">@new</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">other::backwards</span> <span class="i">@old</span><span class="sc">;</span></li><li></li><li>    <span class="c"># guarantee stability, regardless of algorithm</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <a class="l_k" href="../functions/sort.html">sort</a> <span class="q">&#39;stable&#39;</span><span class="sc">;</span></li><li>    <span class="i">@new</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$a</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="n">5</span><span class="s">)</span> cmp <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$b</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="n">5</span><span class="s">)</span> <span class="s">}</span> <span class="i">@old</span><span class="sc">;</span></li><li></li><li>    <span class="c"># force use of mergesort (not portable outside Perl 5.8)</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <a class="l_k" href="../functions/sort.html">sort</a> <span class="q">&#39;_mergesort&#39;</span><span class="sc">;</span>  <span class="c"># note discouraging _</span></li><li>    <span class="i">@new</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$a</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="n">5</span><span class="s">)</span> cmp <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$b</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="n">5</span><span class="s">)</span> <span class="s">}</span> <span class="i">@old</span><span class="sc">;</span></li></ol></pre><p>Warning: syntactical care is required when sorting the list returned from
a function. If you want to sort the list returned by the function call
<code class="inline"><span class="i">find_records</span><span class="s">(</span><span class="i">@key</span><span class="s">)</span></code>
, you can use:</p>
<pre class="verbatim"><ol><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$a</span> cmp <span class="i">$b</span> <span class="s">}</span> <span class="w">find_records</span> <span class="i">@key</span><span class="sc">;</span></li><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a> +<span class="i">find_records</span><span class="s">(</span><span class="i">@key</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">&amp;find_records</span><span class="s">(</span><span class="i">@key</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a><span class="s">(</span><span class="i">find_records</span><span class="s">(</span><span class="i">@key</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If instead you want to sort the array @key with the comparison routine
<code class="inline"><span class="i">find_records</span><span class="s">(</span><span class="s">)</span></code>
 then you can use:</p>
<pre class="verbatim"><ol><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <span class="i">find_records</span><span class="s">(</span><span class="s">)</span> <span class="s">}</span> <span class="i">@key</span><span class="sc">;</span></li><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">find_records</span><span class="s">(</span><span class="i">@key</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a><span class="s">(</span><span class="i">find_records</span> <span class="i">@key</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a><span class="s">(</span><span class="i">find_records</span> <span class="s">(</span><span class="i">@key</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If you&#39;re using strict, you <i>must not</i> declare $a
and $b as lexicals.  They are package globals.  That means
that if you&#39;re in the <code class="inline"><span class="w">main</span></code>
 package and type</p>
<pre class="verbatim"><ol><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><span class="i">$b</span> &lt;=&gt; <span class="i">$a</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li></ol></pre><p>then <code class="inline"><span class="i">$a</span></code>
 and <code class="inline"><span class="i">$b</span></code>
 are <code class="inline"><span class="i">$main::a</span></code>
 and <code class="inline"><span class="i">$main::b</span></code>
 (or <code class="inline"><span class="i">$::a</span></code>
 and <code class="inline"><span class="i">$::b</span></code>
),
but if you&#39;re in the <code class="inline"><span class="w">FooPack</span></code>
 package, it&#39;s the same as typing</p>
<pre class="verbatim"><ol><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><span class="i">$FooPack::b</span> &lt;=&gt; <span class="i">$FooPack::a</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li></ol></pre><p>The comparison function is required to behave.  If it returns
inconsistent results (sometimes saying <code class="inline"><span class="i">$x</span>[<span class="n">1</span>]</code>
 is less than <code class="inline"><span class="i">$x</span>[<span class="n">2</span>]</code>
 and
sometimes saying the opposite, for example) the results are not
well-defined.</p>
<p>Because <code class="inline">&lt;=&gt;</code>
 returns <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> when either operand is <code class="inline"><span class="w">NaN</span></code>

(not-a-number), and because <code class="inline"><a class="l_k" href="../functions/sort.html">sort</a></code> will trigger a fatal error unless the
result of a comparison is defined, when sorting with a comparison function
like <code class="inline"><span class="i">$a</span> &lt;=&gt; <span class="i">$b</span></code>
, be careful about lists that might contain a <code class="inline"><span class="w">NaN</span></code>
.
The following example takes advantage of the fact that <code class="inline"><span class="w">NaN</span> != <span class="w">NaN</span></code>
 to
eliminate any <code class="inline"><span class="w">NaN</span></code>
s from the input.</p>
<pre class="verbatim"><ol><li>    <span class="i">@result</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$a</span> &lt;=&gt; <span class="i">$b</span> <span class="s">}</span> <a class="l_k" href="../functions/grep.html">grep</a> <span class="s">{</span> <span class="i">$_</span> == <span class="i">$_</span> <span class="s">}</span> <span class="i">@input</span><span class="sc">;</span></li></ol><
-----

function sort
block list|
In list context, this sorts the LIST and returns the sorted list value.
In scalar context, the behaviour of <code class="inline"><a class="l_k" href="../functions/sort.html">sort()</a></code> is undefined.</p>
<p>If SUBNAME or BLOCK is omitted, <code class="inline"><a class="l_k" href="../functions/sort.html">sort</a></code>s in standard string comparison
order.  If SUBNAME is specified, it gives the name of a subroutine
that returns an integer less than, equal to, or greater than <code class="inline"><span class="n">0</span></code>
,
depending on how the elements of the list are to be ordered.  (The <code class="inline">&lt;=&gt;</code>
 and <code class="inline">cmp</code>
 operators are extremely useful in such routines.)
SUBNAME may be a scalar variable name (unsubscripted), in which case
the value provides the name of (or a reference to) the actual
subroutine to use.  In place of a SUBNAME, you can provide a BLOCK as
an anonymous, in-line sort subroutine.</p>
<p>If the subroutine&#39;s prototype is <code class="inline"><span class="s">(</span><span class="i">$$</span><span class="s">)</span></code>
, the elements to be compared
are passed by reference in <code class="inline"><span class="i">@_</span></code>
, as for a normal subroutine.  This is
slower than unprototyped subroutines, where the elements to be
compared are passed into the subroutine
as the package global variables $a and $b (see example below).  Note that
in the latter case, it is usually counter-productive to declare $a and
$b as lexicals.</p>
<p>The values to be compared are always passed by reference and should not
be modified.</p>
<p>You also cannot exit out of the sort block or subroutine using any of the
loop control operators described in <a href="../perlsyn.html">perlsyn</a> or with <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code>.</p>
<p>When <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">locale</span></code>
 is in effect, <code class="inline"><a class="l_k" href="../functions/sort.html">sort</a> <span class="i">LIST</span></code>
 sorts LIST according to the
current collation locale.  See <a href="../perllocale.html">perllocale</a>.</p>
<p>sort() returns aliases into the original list, much as a for loop&#39;s index
variable aliases the list elements.  That is, modifying an element of a
list returned by sort() (for example, in a <code class="inline">foreach</code>
, <code class="inline"><a class="l_k" href="../functions/map.html">map</a></code> or <code class="inline"><a class="l_k" href="../functions/grep.html">grep</a></code>)
actually modifies the element in the original list.  This is usually
something to be avoided when writing clear code.</p>
<p>Perl 5.6 and earlier used a quicksort algorithm to implement sort.
That algorithm was not stable, and <i>could</i> go quadratic.  (A <i>stable</i> sort
preserves the input order of elements that compare equal.  Although
quicksort&#39;s run time is O(NlogN) when averaged over all arrays of
length N, the time can be O(N**2), <i>quadratic</i> behavior, for some
inputs.)  In 5.7, the quicksort implementation was replaced with
a stable mergesort algorithm whose worst-case behavior is O(NlogN).
But benchmarks indicated that for some inputs, on some platforms,
the original quicksort was faster.  5.8 has a sort pragma for
limited control of the sort.  Its rather blunt control of the
underlying algorithm may not persist into future Perls, but the
ability to characterize the input or output in implementation
independent ways quite probably will.  See <a href="../sort.html">the sort pragma</a>.</p>
<p>Examples:</p>
<pre class="verbatim"><ol><li>    <span class="c"># sort lexically</span></li><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">@files</span><span class="sc">;</span></li><li></li><li>    <span class="c"># same thing, but with explicit sort routine</span></li><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><span class="i">$a</span> cmp <span class="i">$b</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li><li></li><li>    <span class="c"># now case-insensitively</span></li><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$a</span><span class="s">)</span> cmp <a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$b</span><span class="s">)</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li><li></li><li>    <span class="c"># same thing in reversed order</span></li><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><span class="i">$b</span> cmp <span class="i">$a</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li><li></li><li>    <span class="c"># sort numerically ascending</span></li><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><span class="i">$a</span> &lt;=&gt; <span class="i">$b</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li><li></li><li>    <span class="c"># sort numerically descending</span></li><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><span class="i">$b</span> &lt;=&gt; <span class="i">$a</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li><li></li><li>    <span class="c"># this sorts the %age hash by value instead of key</span></li><li>    <span class="c"># using an in-line function</span></li><li>    <span class="i">@eldest</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$age</span>{<span class="i">$b</span>} &lt;=&gt; <span class="i">$age</span>{<span class="i">$a</span>} <span class="s">}</span> <a class="l_k" href="../functions/keys.html">keys</a> <span class="i">%age</span><span class="sc">;</span></li><li></li><li>    <span class="c"># sort using explicit subroutine name</span></li><li><a name="byage"></a>    sub <span class="m">byage</span> <span class="s">{</span></li><li>	<span class="i">$age</span>{<span class="i">$a</span>} &lt;=&gt; <span class="i">$age</span>{<span class="i">$b</span>}<span class="sc">;</span>	<span class="c"># presuming numeric</span></li><li>    <span class="s">}</span></li><li>    <span class="i">@sortedclass</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">byage</span> <span class="i">@class</span><span class="sc">;</span></li><li></li><li><a name="backwards"></a>    sub <span class="m">backwards</span> <span class="s">{</span> <span class="i">$b</span> cmp <span class="i">$a</span> <span class="s">}</span></li><li>    <span class="i">@harry</span>  = <span class="q">qw(dog cat x Cain Abel)</span><span class="sc">;</span></li><li>    <span class="i">@george</span> = <span class="q">qw(gone chased yz Punished Axed)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">@harry</span><span class="sc">;</span></li><li>	    <span class="c"># prints AbelCaincatdogx</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">backwards</span> <span class="i">@harry</span><span class="sc">;</span></li><li>	    <span class="c"># prints xdogcatCainAbel</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">@george</span><span class="cm">,</span> <span class="q">&#39;to&#39;</span><span class="cm">,</span> <span class="i">@harry</span><span class="sc">;</span></li><li>	    <span class="c"># prints AbelAxedCainPunishedcatchaseddoggonetoxyz</span></li><li></li><li>    <span class="c"># inefficiently sort by descending numeric compare using</span></li><li>    <span class="c"># the first integer after the first = sign, or the</span></li><li>    <span class="c"># whole record case-insensitively otherwise</span></li><li></li><li>    <span class="i">@new</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span></li><li>	<span class="s">(</span><span class="i">$b</span> =~ <span class="q">/=(\d+)/</span><span class="s">)</span>[<span class="n">0</span>] &lt;=&gt; <span class="s">(</span><span class="i">$a</span> =~ <span class="q">/=(\d+)/</span><span class="s">)</span>[<span class="n">0</span>]</li><li>			    ||</li><li>	            <a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$a</span><span class="s">)</span>  cmp  <a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$b</span><span class="s">)</span></li><li>    <span class="s">}</span> <span class="i">@old</span><span class="sc">;</span></li><li></li><li>    <span class="c"># same thing, but much more efficiently;</span></li><li>    <span class="c"># we&#39;ll build auxiliary indices instead</span></li><li>    <span class="c"># for speed</span></li><li>    <span class="i">@nums</span> = <span class="i">@caps</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    for <span class="s">(</span><span class="i">@old</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/push.html">push</a> <span class="i">@nums</span><span class="cm">,</span> <span class="q">/=(\d+)/</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/push.html">push</a> <span class="i">@caps</span><span class="cm">,</span> <a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="i">@new</span> = <span class="i">@old</span>[ <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span></li><li>			<span class="i">$nums</span>[<span class="i">$b</span>] &lt;=&gt; <span class="i">$nums</span>[<span class="i">$a</span>]</li><li>				 ||</li><li>			<span class="i">$caps</span>[<span class="i">$a</span>] cmp <span class="i">$caps</span>[<span class="i">$b</span>]</li><li>		       <span class="s">}</span> <span class="n">0</span>..<span class="i">$#old</span></li><li>	       ]<span class="sc">;</span></li><li></li><li>    <span class="c"># same thing, but without any temps</span></li><li>    <span class="i">@new</span> = <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span> <span class="i">$_</span>-&gt;[<span class="n">0</span>] <span class="s">}</span></li><li>           <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$b</span>-&gt;[<span class="n">1</span>] &lt;=&gt; <span class="i">$a</span>-&gt;[<span class="n">1</span>]</li><li>                           ||</li><li>                  <span class="i">$a</span>-&gt;[<span class="n">2</span>] cmp <span class="i">$b</span>-&gt;[<span class="n">2</span>]</li><li>           <span class="s">}</span> <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span> <span class="s">[</span><span class="i">$_</span><span class="cm">,</span> <span class="q">/=(\d+)/</span><span class="cm">,</span> <a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="s">]</span> <span class="s">}</span> <span class="i">@old</span><span class="sc">;</span></li><li></li><li>    <span class="c"># using a prototype allows you to use any comparison subroutine</span></li><li>    <span class="c"># as a sort subroutine (including other package&#39;s subroutines)</span></li><li><a name="package-other"></a>    package <span class="i">other</span><span class="sc">;</span></li><li><a name="backwards-1"></a>    sub <span class="m">backwards ($$)</span> <span class="s">{</span> <span class="i">$_</span>[<span class="n">1</span>] cmp <span class="i">$_</span>[<span class="n">0</span>]<span class="sc">;</span> <span class="s">}</span>	<span class="c"># $a and $b are not set here</span></li><li></li><li><a name="package-main"></a>    package <span class="i">main</span><span class="sc">;</span></li><li>    <span class="i">@new</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">other::backwards</span> <span class="i">@old</span><span class="sc">;</span></li><li></li><li>    <span class="c"># guarantee stability, regardless of algorithm</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <a class="l_k" href="../functions/sort.html">sort</a> <span class="q">&#39;stable&#39;</span><span class="sc">;</span></li><li>    <span class="i">@new</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$a</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="n">5</span><span class="s">)</span> cmp <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$b</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="n">5</span><span class="s">)</span> <span class="s">}</span> <span class="i">@old</span><span class="sc">;</span></li><li></li><li>    <span class="c"># force use of mergesort (not portable outside Perl 5.8)</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <a class="l_k" href="../functions/sort.html">sort</a> <span class="q">&#39;_mergesort&#39;</span><span class="sc">;</span>  <span class="c"># note discouraging _</span></li><li>    <span class="i">@new</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$a</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="n">5</span><span class="s">)</span> cmp <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$b</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="n">5</span><span class="s">)</span> <span class="s">}</span> <span class="i">@old</span><span class="sc">;</span></li></ol></pre><p>Warning: syntactical care is required when sorting the list returned from
a function. If you want to sort the list returned by the function call
<code class="inline"><span class="i">find_records</span><span class="s">(</span><span class="i">@key</span><span class="s">)</span></code>
, you can use:</p>
<pre class="verbatim"><ol><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$a</span> cmp <span class="i">$b</span> <span class="s">}</span> <span class="w">find_records</span> <span class="i">@key</span><span class="sc">;</span></li><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a> +<span class="i">find_records</span><span class="s">(</span><span class="i">@key</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">&amp;find_records</span><span class="s">(</span><span class="i">@key</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a><span class="s">(</span><span class="i">find_records</span><span class="s">(</span><span class="i">@key</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If instead you want to sort the array @key with the comparison routine
<code class="inline"><span class="i">find_records</span><span class="s">(</span><span class="s">)</span></code>
 then you can use:</p>
<pre class="verbatim"><ol><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <span class="i">find_records</span><span class="s">(</span><span class="s">)</span> <span class="s">}</span> <span class="i">@key</span><span class="sc">;</span></li><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">find_records</span><span class="s">(</span><span class="i">@key</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a><span class="s">(</span><span class="i">find_records</span> <span class="i">@key</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a><span class="s">(</span><span class="i">find_records</span> <span class="s">(</span><span class="i">@key</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If you&#39;re using strict, you <i>must not</i> declare $a
and $b as lexicals.  They are package globals.  That means
that if you&#39;re in the <code class="inline"><span class="w">main</span></code>
 package and type</p>
<pre class="verbatim"><ol><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><span class="i">$b</span> &lt;=&gt; <span class="i">$a</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li></ol></pre><p>then <code class="inline"><span class="i">$a</span></code>
 and <code class="inline"><span class="i">$b</span></code>
 are <code class="inline"><span class="i">$main::a</span></code>
 and <code class="inline"><span class="i">$main::b</span></code>
 (or <code class="inline"><span class="i">$::a</span></code>
 and <code class="inline"><span class="i">$::b</span></code>
),
but if you&#39;re in the <code class="inline"><span class="w">FooPack</span></code>
 package, it&#39;s the same as typing</p>
<pre class="verbatim"><ol><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><span class="i">$FooPack::b</span> &lt;=&gt; <span class="i">$FooPack::a</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li></ol></pre><p>The comparison function is required to behave.  If it returns
inconsistent results (sometimes saying <code class="inline"><span class="i">$x</span>[<span class="n">1</span>]</code>
 is less than <code class="inline"><span class="i">$x</span>[<span class="n">2</span>]</code>
 and
sometimes saying the opposite, for example) the results are not
well-defined.</p>
<p>Because <code class="inline">&lt;=&gt;</code>
 returns <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> when either operand is <code class="inline"><span class="w">NaN</span></code>

(not-a-number), and because <code class="inline"><a class="l_k" href="../functions/sort.html">sort</a></code> will trigger a fatal error unless the
result of a comparison is defined, when sorting with a comparison function
like <code class="inline"><span class="i">$a</span> &lt;=&gt; <span class="i">$b</span></code>
, be careful about lists that might contain a <code class="inline"><span class="w">NaN</span></code>
.
The following example takes advantage of the fact that <code class="inline"><span class="w">NaN</span> != <span class="w">NaN</span></code>
 to
eliminate any <code class="inline"><span class="w">NaN</span></code>
s from the input.</p>
<pre class="verbatim"><ol><li>    <span class="i">@result</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$a</span> &lt;=&gt; <span class="i">$b</span> <span class="s">}</span> <a class="l_k" href="../functions/grep.html">grep</a> <span class="s">{</span> <span class="i">$_</span> == <span class="i">$_</span> <span class="s">}</span> <span class="i">@input</span><span class="sc">;</span></li></ol><
-----

function sort
list|
In list context, this sorts the LIST and returns the sorted list value.
In scalar context, the behaviour of <code class="inline"><a class="l_k" href="../functions/sort.html">sort()</a></code> is undefined.</p>
<p>If SUBNAME or BLOCK is omitted, <code class="inline"><a class="l_k" href="../functions/sort.html">sort</a></code>s in standard string comparison
order.  If SUBNAME is specified, it gives the name of a subroutine
that returns an integer less than, equal to, or greater than <code class="inline"><span class="n">0</span></code>
,
depending on how the elements of the list are to be ordered.  (The <code class="inline">&lt;=&gt;</code>
 and <code class="inline">cmp</code>
 operators are extremely useful in such routines.)
SUBNAME may be a scalar variable name (unsubscripted), in which case
the value provides the name of (or a reference to) the actual
subroutine to use.  In place of a SUBNAME, you can provide a BLOCK as
an anonymous, in-line sort subroutine.</p>
<p>If the subroutine&#39;s prototype is <code class="inline"><span class="s">(</span><span class="i">$$</span><span class="s">)</span></code>
, the elements to be compared
are passed by reference in <code class="inline"><span class="i">@_</span></code>
, as for a normal subroutine.  This is
slower than unprototyped subroutines, where the elements to be
compared are passed into the subroutine
as the package global variables $a and $b (see example below).  Note that
in the latter case, it is usually counter-productive to declare $a and
$b as lexicals.</p>
<p>The values to be compared are always passed by reference and should not
be modified.</p>
<p>You also cannot exit out of the sort block or subroutine using any of the
loop control operators described in <a href="../perlsyn.html">perlsyn</a> or with <code class="inline"><a class="l_k" href="../functions/goto.html">goto</a></code>.</p>
<p>When <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">locale</span></code>
 is in effect, <code class="inline"><a class="l_k" href="../functions/sort.html">sort</a> <span class="i">LIST</span></code>
 sorts LIST according to the
current collation locale.  See <a href="../perllocale.html">perllocale</a>.</p>
<p>sort() returns aliases into the original list, much as a for loop&#39;s index
variable aliases the list elements.  That is, modifying an element of a
list returned by sort() (for example, in a <code class="inline">foreach</code>
, <code class="inline"><a class="l_k" href="../functions/map.html">map</a></code> or <code class="inline"><a class="l_k" href="../functions/grep.html">grep</a></code>)
actually modifies the element in the original list.  This is usually
something to be avoided when writing clear code.</p>
<p>Perl 5.6 and earlier used a quicksort algorithm to implement sort.
That algorithm was not stable, and <i>could</i> go quadratic.  (A <i>stable</i> sort
preserves the input order of elements that compare equal.  Although
quicksort&#39;s run time is O(NlogN) when averaged over all arrays of
length N, the time can be O(N**2), <i>quadratic</i> behavior, for some
inputs.)  In 5.7, the quicksort implementation was replaced with
a stable mergesort algorithm whose worst-case behavior is O(NlogN).
But benchmarks indicated that for some inputs, on some platforms,
the original quicksort was faster.  5.8 has a sort pragma for
limited control of the sort.  Its rather blunt control of the
underlying algorithm may not persist into future Perls, but the
ability to characterize the input or output in implementation
independent ways quite probably will.  See <a href="../sort.html">the sort pragma</a>.</p>
<p>Examples:</p>
<pre class="verbatim"><ol><li>    <span class="c"># sort lexically</span></li><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">@files</span><span class="sc">;</span></li><li></li><li>    <span class="c"># same thing, but with explicit sort routine</span></li><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><span class="i">$a</span> cmp <span class="i">$b</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li><li></li><li>    <span class="c"># now case-insensitively</span></li><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$a</span><span class="s">)</span> cmp <a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$b</span><span class="s">)</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li><li></li><li>    <span class="c"># same thing in reversed order</span></li><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><span class="i">$b</span> cmp <span class="i">$a</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li><li></li><li>    <span class="c"># sort numerically ascending</span></li><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><span class="i">$a</span> &lt;=&gt; <span class="i">$b</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li><li></li><li>    <span class="c"># sort numerically descending</span></li><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><span class="i">$b</span> &lt;=&gt; <span class="i">$a</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li><li></li><li>    <span class="c"># this sorts the %age hash by value instead of key</span></li><li>    <span class="c"># using an in-line function</span></li><li>    <span class="i">@eldest</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$age</span>{<span class="i">$b</span>} &lt;=&gt; <span class="i">$age</span>{<span class="i">$a</span>} <span class="s">}</span> <a class="l_k" href="../functions/keys.html">keys</a> <span class="i">%age</span><span class="sc">;</span></li><li></li><li>    <span class="c"># sort using explicit subroutine name</span></li><li><a name="byage"></a>    sub <span class="m">byage</span> <span class="s">{</span></li><li>	<span class="i">$age</span>{<span class="i">$a</span>} &lt;=&gt; <span class="i">$age</span>{<span class="i">$b</span>}<span class="sc">;</span>	<span class="c"># presuming numeric</span></li><li>    <span class="s">}</span></li><li>    <span class="i">@sortedclass</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">byage</span> <span class="i">@class</span><span class="sc">;</span></li><li></li><li><a name="backwards"></a>    sub <span class="m">backwards</span> <span class="s">{</span> <span class="i">$b</span> cmp <span class="i">$a</span> <span class="s">}</span></li><li>    <span class="i">@harry</span>  = <span class="q">qw(dog cat x Cain Abel)</span><span class="sc">;</span></li><li>    <span class="i">@george</span> = <span class="q">qw(gone chased yz Punished Axed)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">@harry</span><span class="sc">;</span></li><li>	    <span class="c"># prints AbelCaincatdogx</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">backwards</span> <span class="i">@harry</span><span class="sc">;</span></li><li>	    <span class="c"># prints xdogcatCainAbel</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">@george</span><span class="cm">,</span> <span class="q">&#39;to&#39;</span><span class="cm">,</span> <span class="i">@harry</span><span class="sc">;</span></li><li>	    <span class="c"># prints AbelAxedCainPunishedcatchaseddoggonetoxyz</span></li><li></li><li>    <span class="c"># inefficiently sort by descending numeric compare using</span></li><li>    <span class="c"># the first integer after the first = sign, or the</span></li><li>    <span class="c"># whole record case-insensitively otherwise</span></li><li></li><li>    <span class="i">@new</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span></li><li>	<span class="s">(</span><span class="i">$b</span> =~ <span class="q">/=(\d+)/</span><span class="s">)</span>[<span class="n">0</span>] &lt;=&gt; <span class="s">(</span><span class="i">$a</span> =~ <span class="q">/=(\d+)/</span><span class="s">)</span>[<span class="n">0</span>]</li><li>			    ||</li><li>	            <a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$a</span><span class="s">)</span>  cmp  <a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$b</span><span class="s">)</span></li><li>    <span class="s">}</span> <span class="i">@old</span><span class="sc">;</span></li><li></li><li>    <span class="c"># same thing, but much more efficiently;</span></li><li>    <span class="c"># we&#39;ll build auxiliary indices instead</span></li><li>    <span class="c"># for speed</span></li><li>    <span class="i">@nums</span> = <span class="i">@caps</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    for <span class="s">(</span><span class="i">@old</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/push.html">push</a> <span class="i">@nums</span><span class="cm">,</span> <span class="q">/=(\d+)/</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/push.html">push</a> <span class="i">@caps</span><span class="cm">,</span> <a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="i">@new</span> = <span class="i">@old</span>[ <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span></li><li>			<span class="i">$nums</span>[<span class="i">$b</span>] &lt;=&gt; <span class="i">$nums</span>[<span class="i">$a</span>]</li><li>				 ||</li><li>			<span class="i">$caps</span>[<span class="i">$a</span>] cmp <span class="i">$caps</span>[<span class="i">$b</span>]</li><li>		       <span class="s">}</span> <span class="n">0</span>..<span class="i">$#old</span></li><li>	       ]<span class="sc">;</span></li><li></li><li>    <span class="c"># same thing, but without any temps</span></li><li>    <span class="i">@new</span> = <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span> <span class="i">$_</span>-&gt;[<span class="n">0</span>] <span class="s">}</span></li><li>           <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$b</span>-&gt;[<span class="n">1</span>] &lt;=&gt; <span class="i">$a</span>-&gt;[<span class="n">1</span>]</li><li>                           ||</li><li>                  <span class="i">$a</span>-&gt;[<span class="n">2</span>] cmp <span class="i">$b</span>-&gt;[<span class="n">2</span>]</li><li>           <span class="s">}</span> <a class="l_k" href="../functions/map.html">map</a> <span class="s">{</span> <span class="s">[</span><span class="i">$_</span><span class="cm">,</span> <span class="q">/=(\d+)/</span><span class="cm">,</span> <a class="l_k" href="../functions/uc.html">uc</a><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="s">]</span> <span class="s">}</span> <span class="i">@old</span><span class="sc">;</span></li><li></li><li>    <span class="c"># using a prototype allows you to use any comparison subroutine</span></li><li>    <span class="c"># as a sort subroutine (including other package&#39;s subroutines)</span></li><li><a name="package-other"></a>    package <span class="i">other</span><span class="sc">;</span></li><li><a name="backwards-1"></a>    sub <span class="m">backwards ($$)</span> <span class="s">{</span> <span class="i">$_</span>[<span class="n">1</span>] cmp <span class="i">$_</span>[<span class="n">0</span>]<span class="sc">;</span> <span class="s">}</span>	<span class="c"># $a and $b are not set here</span></li><li></li><li><a name="package-main"></a>    package <span class="i">main</span><span class="sc">;</span></li><li>    <span class="i">@new</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">other::backwards</span> <span class="i">@old</span><span class="sc">;</span></li><li></li><li>    <span class="c"># guarantee stability, regardless of algorithm</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <a class="l_k" href="../functions/sort.html">sort</a> <span class="q">&#39;stable&#39;</span><span class="sc">;</span></li><li>    <span class="i">@new</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$a</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="n">5</span><span class="s">)</span> cmp <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$b</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="n">5</span><span class="s">)</span> <span class="s">}</span> <span class="i">@old</span><span class="sc">;</span></li><li></li><li>    <span class="c"># force use of mergesort (not portable outside Perl 5.8)</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <a class="l_k" href="../functions/sort.html">sort</a> <span class="q">&#39;_mergesort&#39;</span><span class="sc">;</span>  <span class="c"># note discouraging _</span></li><li>    <span class="i">@new</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$a</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="n">5</span><span class="s">)</span> cmp <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$b</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="n">5</span><span class="s">)</span> <span class="s">}</span> <span class="i">@old</span><span class="sc">;</span></li></ol></pre><p>Warning: syntactical care is required when sorting the list returned from
a function. If you want to sort the list returned by the function call
<code class="inline"><span class="i">find_records</span><span class="s">(</span><span class="i">@key</span><span class="s">)</span></code>
, you can use:</p>
<pre class="verbatim"><ol><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$a</span> cmp <span class="i">$b</span> <span class="s">}</span> <span class="w">find_records</span> <span class="i">@key</span><span class="sc">;</span></li><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a> +<span class="i">find_records</span><span class="s">(</span><span class="i">@key</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">&amp;find_records</span><span class="s">(</span><span class="i">@key</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a><span class="s">(</span><span class="i">find_records</span><span class="s">(</span><span class="i">@key</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If instead you want to sort the array @key with the comparison routine
<code class="inline"><span class="i">find_records</span><span class="s">(</span><span class="s">)</span></code>
 then you can use:</p>
<pre class="verbatim"><ol><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <span class="i">find_records</span><span class="s">(</span><span class="s">)</span> <span class="s">}</span> <span class="i">@key</span><span class="sc">;</span></li><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="i">find_records</span><span class="s">(</span><span class="i">@key</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a><span class="s">(</span><span class="i">find_records</span> <span class="i">@key</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">@contact</span> = <a class="l_k" href="../functions/sort.html">sort</a><span class="s">(</span><span class="i">find_records</span> <span class="s">(</span><span class="i">@key</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If you&#39;re using strict, you <i>must not</i> declare $a
and $b as lexicals.  They are package globals.  That means
that if you&#39;re in the <code class="inline"><span class="w">main</span></code>
 package and type</p>
<pre class="verbatim"><ol><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><span class="i">$b</span> &lt;=&gt; <span class="i">$a</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li></ol></pre><p>then <code class="inline"><span class="i">$a</span></code>
 and <code class="inline"><span class="i">$b</span></code>
 are <code class="inline"><span class="i">$main::a</span></code>
 and <code class="inline"><span class="i">$main::b</span></code>
 (or <code class="inline"><span class="i">$::a</span></code>
 and <code class="inline"><span class="i">$::b</span></code>
),
but if you&#39;re in the <code class="inline"><span class="w">FooPack</span></code>
 package, it&#39;s the same as typing</p>
<pre class="verbatim"><ol><li>    <span class="i">@articles</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span><span class="i">$FooPack::b</span> &lt;=&gt; <span class="i">$FooPack::a</span><span class="s">}</span> <span class="i">@files</span><span class="sc">;</span></li></ol></pre><p>The comparison function is required to behave.  If it returns
inconsistent results (sometimes saying <code class="inline"><span class="i">$x</span>[<span class="n">1</span>]</code>
 is less than <code class="inline"><span class="i">$x</span>[<span class="n">2</span>]</code>
 and
sometimes saying the opposite, for example) the results are not
well-defined.</p>
<p>Because <code class="inline">&lt;=&gt;</code>
 returns <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> when either operand is <code class="inline"><span class="w">NaN</span></code>

(not-a-number), and because <code class="inline"><a class="l_k" href="../functions/sort.html">sort</a></code> will trigger a fatal error unless the
result of a comparison is defined, when sorting with a comparison function
like <code class="inline"><span class="i">$a</span> &lt;=&gt; <span class="i">$b</span></code>
, be careful about lists that might contain a <code class="inline"><span class="w">NaN</span></code>
.
The following example takes advantage of the fact that <code class="inline"><span class="w">NaN</span> != <span class="w">NaN</span></code>
 to
eliminate any <code class="inline"><span class="w">NaN</span></code>
s from the input.</p>
<pre class="verbatim"><ol><li>    <span class="i">@result</span> = <a class="l_k" href="../functions/sort.html">sort</a> <span class="s">{</span> <span class="i">$a</span> &lt;=&gt; <span class="i">$b</span> <span class="s">}</span> <a class="l_k" href="../functions/grep.html">grep</a> <span class="s">{</span> <span class="i">$_</span> == <span class="i">$_</span> <span class="s">}</span> <span class="i">@input</span><span class="sc">;</span></li></ol><
-----

function splice
array|
offset|
length|
list|
Removes the elements designated by OFFSET and LENGTH from an array, and
replaces them with the elements of LIST, if any.  In list context,
returns the elements removed from the array.  In scalar context,
returns the last element removed, or <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> if no elements are
removed.  The array grows or shrinks as necessary.
If OFFSET is negative then it starts that far from the end of the array.
If LENGTH is omitted, removes everything from OFFSET onward.
If LENGTH is negative, removes the elements from OFFSET onward
except for -LENGTH elements at the end of the array.
If both OFFSET and LENGTH are omitted, removes everything. If OFFSET is
past the end of the array, perl issues a warning, and splices at the
end of the array.</p>
<p>The following equivalences hold (assuming <code class="inline"><span class="i">$[</span> == <span class="n">0</span> and <span class="i">$#a</span> &gt;= <span class="i">$i</span></code>
 )</p>
<pre class="verbatim"><ol><li>    push(@a,$x,$y)	splice(@a,@a,0,$x,$y)</li><li>    pop(@a)		splice(@a,-1)</li><li>    shift(@a)		splice(@a,0,1)</li><li>    unshift(@a,$x,$y)	splice(@a,0,0,$x,$y)</li><li>    $a[$i] = $y		splice(@a,$i,1,$y)</li></ol></pre><p>Example, assuming array lengths are passed before arrays:</p>
<pre class="verbatim"><ol><li><a name="aeq"></a>    sub <span class="m">aeq</span> <span class="s">{</span>	<span class="c"># compare two list values</span></li><li>	<a class="l_k" href="../functions/my.html">my</a><span class="s">(</span><span class="i">@a</span><span class="s">)</span> = <a class="l_k" href="../functions/splice.html">splice</a><span class="s">(</span><span class="i">@_</span><span class="cm">,</span><span class="n">0</span><span class="cm">,</span><a class="l_k" href="../functions/shift.html">shift</a><span class="s">)</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/my.html">my</a><span class="s">(</span><span class="i">@b</span><span class="s">)</span> = <a class="l_k" href="../functions/splice.html">splice</a><span class="s">(</span><span class="i">@_</span><span class="cm">,</span><span class="n">0</span><span class="cm">,</span><a class="l_k" href="../functions/shift.html">shift</a><span class="s">)</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/return.html">return</a> <span class="n">0</span> unless <span class="i">@a</span> == <span class="i">@b</span><span class="sc">;</span>	<span class="c"># same len?</span></li><li>	while <span class="s">(</span><span class="i">@a</span><span class="s">)</span> <span class="s">{</span></li><li>	    <a class="l_k" href="../functions/return.html">return</a> <span class="n">0</span> if <a class="l_k" href="../functions/pop.html">pop</a><span class="s">(</span><span class="i">@a</span><span class="s">)</span> ne <a class="l_k" href="../functions/pop.html">pop</a><span class="s">(</span><span class="i">@b</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="s">}</span></li><li>	<a class="l_k" href="../functions/return.html">return</a> <span class="n">1</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    if <span class="s">(</span><span class="i">&amp;aeq</span><span class="s">(</span><span class="i">$len</span><span class="cm">,</span><span class="i">@foo</span>[<span class="n">1</span>..<span class="i">$len</span>]<span class="cm">,</span><span class="n">0</span>+<span class="i">@bar</span><span class="cm">,</span><span class="i">@bar</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li></ol><
-----

function splice
array|
offset|
length|
Removes the elements designated by OFFSET and LENGTH from an array, and
replaces them with the elements of LIST, if any.  In list context,
returns the elements removed from the array.  In scalar context,
returns the last element removed, or <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> if no elements are
removed.  The array grows or shrinks as necessary.
If OFFSET is negative then it starts that far from the end of the array.
If LENGTH is omitted, removes everything from OFFSET onward.
If LENGTH is negative, removes the elements from OFFSET onward
except for -LENGTH elements at the end of the array.
If both OFFSET and LENGTH are omitted, removes everything. If OFFSET is
past the end of the array, perl issues a warning, and splices at the
end of the array.</p>
<p>The following equivalences hold (assuming <code class="inline"><span class="i">$[</span> == <span class="n">0</span> and <span class="i">$#a</span> &gt;= <span class="i">$i</span></code>
 )</p>
<pre class="verbatim"><ol><li>    push(@a,$x,$y)	splice(@a,@a,0,$x,$y)</li><li>    pop(@a)		splice(@a,-1)</li><li>    shift(@a)		splice(@a,0,1)</li><li>    unshift(@a,$x,$y)	splice(@a,0,0,$x,$y)</li><li>    $a[$i] = $y		splice(@a,$i,1,$y)</li></ol></pre><p>Example, assuming array lengths are passed before arrays:</p>
<pre class="verbatim"><ol><li><a name="aeq"></a>    sub <span class="m">aeq</span> <span class="s">{</span>	<span class="c"># compare two list values</span></li><li>	<a class="l_k" href="../functions/my.html">my</a><span class="s">(</span><span class="i">@a</span><span class="s">)</span> = <a class="l_k" href="../functions/splice.html">splice</a><span class="s">(</span><span class="i">@_</span><span class="cm">,</span><span class="n">0</span><span class="cm">,</span><a class="l_k" href="../functions/shift.html">shift</a><span class="s">)</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/my.html">my</a><span class="s">(</span><span class="i">@b</span><span class="s">)</span> = <a class="l_k" href="../functions/splice.html">splice</a><span class="s">(</span><span class="i">@_</span><span class="cm">,</span><span class="n">0</span><span class="cm">,</span><a class="l_k" href="../functions/shift.html">shift</a><span class="s">)</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/return.html">return</a> <span class="n">0</span> unless <span class="i">@a</span> == <span class="i">@b</span><span class="sc">;</span>	<span class="c"># same len?</span></li><li>	while <span class="s">(</span><span class="i">@a</span><span class="s">)</span> <span class="s">{</span></li><li>	    <a class="l_k" href="../functions/return.html">return</a> <span class="n">0</span> if <a class="l_k" href="../functions/pop.html">pop</a><span class="s">(</span><span class="i">@a</span><span class="s">)</span> ne <a class="l_k" href="../functions/pop.html">pop</a><span class="s">(</span><span class="i">@b</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="s">}</span></li><li>	<a class="l_k" href="../functions/return.html">return</a> <span class="n">1</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    if <span class="s">(</span><span class="i">&amp;aeq</span><span class="s">(</span><span class="i">$len</span><span class="cm">,</span><span class="i">@foo</span>[<span class="n">1</span>..<span class="i">$len</span>]<span class="cm">,</span><span class="n">0</span>+<span class="i">@bar</span><span class="cm">,</span><span class="i">@bar</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li></ol><
-----

function splice
array|
offset|
Removes the elements designated by OFFSET and LENGTH from an array, and
replaces them with the elements of LIST, if any.  In list context,
returns the elements removed from the array.  In scalar context,
returns the last element removed, or <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> if no elements are
removed.  The array grows or shrinks as necessary.
If OFFSET is negative then it starts that far from the end of the array.
If LENGTH is omitted, removes everything from OFFSET onward.
If LENGTH is negative, removes the elements from OFFSET onward
except for -LENGTH elements at the end of the array.
If both OFFSET and LENGTH are omitted, removes everything. If OFFSET is
past the end of the array, perl issues a warning, and splices at the
end of the array.</p>
<p>The following equivalences hold (assuming <code class="inline"><span class="i">$[</span> == <span class="n">0</span> and <span class="i">$#a</span> &gt;= <span class="i">$i</span></code>
 )</p>
<pre class="verbatim"><ol><li>    push(@a,$x,$y)	splice(@a,@a,0,$x,$y)</li><li>    pop(@a)		splice(@a,-1)</li><li>    shift(@a)		splice(@a,0,1)</li><li>    unshift(@a,$x,$y)	splice(@a,0,0,$x,$y)</li><li>    $a[$i] = $y		splice(@a,$i,1,$y)</li></ol></pre><p>Example, assuming array lengths are passed before arrays:</p>
<pre class="verbatim"><ol><li><a name="aeq"></a>    sub <span class="m">aeq</span> <span class="s">{</span>	<span class="c"># compare two list values</span></li><li>	<a class="l_k" href="../functions/my.html">my</a><span class="s">(</span><span class="i">@a</span><span class="s">)</span> = <a class="l_k" href="../functions/splice.html">splice</a><span class="s">(</span><span class="i">@_</span><span class="cm">,</span><span class="n">0</span><span class="cm">,</span><a class="l_k" href="../functions/shift.html">shift</a><span class="s">)</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/my.html">my</a><span class="s">(</span><span class="i">@b</span><span class="s">)</span> = <a class="l_k" href="../functions/splice.html">splice</a><span class="s">(</span><span class="i">@_</span><span class="cm">,</span><span class="n">0</span><span class="cm">,</span><a class="l_k" href="../functions/shift.html">shift</a><span class="s">)</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/return.html">return</a> <span class="n">0</span> unless <span class="i">@a</span> == <span class="i">@b</span><span class="sc">;</span>	<span class="c"># same len?</span></li><li>	while <span class="s">(</span><span class="i">@a</span><span class="s">)</span> <span class="s">{</span></li><li>	    <a class="l_k" href="../functions/return.html">return</a> <span class="n">0</span> if <a class="l_k" href="../functions/pop.html">pop</a><span class="s">(</span><span class="i">@a</span><span class="s">)</span> ne <a class="l_k" href="../functions/pop.html">pop</a><span class="s">(</span><span class="i">@b</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="s">}</span></li><li>	<a class="l_k" href="../functions/return.html">return</a> <span class="n">1</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    if <span class="s">(</span><span class="i">&amp;aeq</span><span class="s">(</span><span class="i">$len</span><span class="cm">,</span><span class="i">@foo</span>[<span class="n">1</span>..<span class="i">$len</span>]<span class="cm">,</span><span class="n">0</span>+<span class="i">@bar</span><span class="cm">,</span><span class="i">@bar</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li></ol><
-----

function splice
array|
Removes the elements designated by OFFSET and LENGTH from an array, and
replaces them with the elements of LIST, if any.  In list context,
returns the elements removed from the array.  In scalar context,
returns the last element removed, or <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> if no elements are
removed.  The array grows or shrinks as necessary.
If OFFSET is negative then it starts that far from the end of the array.
If LENGTH is omitted, removes everything from OFFSET onward.
If LENGTH is negative, removes the elements from OFFSET onward
except for -LENGTH elements at the end of the array.
If both OFFSET and LENGTH are omitted, removes everything. If OFFSET is
past the end of the array, perl issues a warning, and splices at the
end of the array.</p>
<p>The following equivalences hold (assuming <code class="inline"><span class="i">$[</span> == <span class="n">0</span> and <span class="i">$#a</span> &gt;= <span class="i">$i</span></code>
 )</p>
<pre class="verbatim"><ol><li>    push(@a,$x,$y)	splice(@a,@a,0,$x,$y)</li><li>    pop(@a)		splice(@a,-1)</li><li>    shift(@a)		splice(@a,0,1)</li><li>    unshift(@a,$x,$y)	splice(@a,0,0,$x,$y)</li><li>    $a[$i] = $y		splice(@a,$i,1,$y)</li></ol></pre><p>Example, assuming array lengths are passed before arrays:</p>
<pre class="verbatim"><ol><li><a name="aeq"></a>    sub <span class="m">aeq</span> <span class="s">{</span>	<span class="c"># compare two list values</span></li><li>	<a class="l_k" href="../functions/my.html">my</a><span class="s">(</span><span class="i">@a</span><span class="s">)</span> = <a class="l_k" href="../functions/splice.html">splice</a><span class="s">(</span><span class="i">@_</span><span class="cm">,</span><span class="n">0</span><span class="cm">,</span><a class="l_k" href="../functions/shift.html">shift</a><span class="s">)</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/my.html">my</a><span class="s">(</span><span class="i">@b</span><span class="s">)</span> = <a class="l_k" href="../functions/splice.html">splice</a><span class="s">(</span><span class="i">@_</span><span class="cm">,</span><span class="n">0</span><span class="cm">,</span><a class="l_k" href="../functions/shift.html">shift</a><span class="s">)</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/return.html">return</a> <span class="n">0</span> unless <span class="i">@a</span> == <span class="i">@b</span><span class="sc">;</span>	<span class="c"># same len?</span></li><li>	while <span class="s">(</span><span class="i">@a</span><span class="s">)</span> <span class="s">{</span></li><li>	    <a class="l_k" href="../functions/return.html">return</a> <span class="n">0</span> if <a class="l_k" href="../functions/pop.html">pop</a><span class="s">(</span><span class="i">@a</span><span class="s">)</span> ne <a class="l_k" href="../functions/pop.html">pop</a><span class="s">(</span><span class="i">@b</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="s">}</span></li><li>	<a class="l_k" href="../functions/return.html">return</a> <span class="n">1</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    if <span class="s">(</span><span class="i">&amp;aeq</span><span class="s">(</span><span class="i">$len</span><span class="cm">,</span><span class="i">@foo</span>[<span class="n">1</span>..<span class="i">$len</span>]<span class="cm">,</span><span class="n">0</span>+<span class="i">@bar</span><span class="cm">,</span><span class="i">@bar</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span></li></ol><
-----

function split
/pattern/|
expr|
limit|
Splits the string EXPR into a list of strings and returns that list.  By
default, empty leading fields are preserved, and empty trailing ones are
deleted.  (If all fields are empty, they are considered to be trailing.)</p>
<p>In scalar context, returns the number of fields found. In scalar and void
context it splits into the <code class="inline"><span class="i">@_</span></code>
 array.  Use of split in scalar and void
context is deprecated, however, because it clobbers your subroutine
arguments.</p>
<p>If EXPR is omitted, splits the <code class="inline"><span class="i">$_</span></code>
 string.  If PATTERN is also omitted,
splits on whitespace (after skipping any leading whitespace).  Anything
matching PATTERN is taken to be a delimiter separating the fields.  (Note
that the delimiter may be longer than one character.)</p>
<p>If LIMIT is specified and positive, it represents the maximum number
of fields the EXPR will be split into, though the actual number of
fields returned depends on the number of times PATTERN matches within
EXPR.  If LIMIT is unspecified or zero, trailing null fields are
stripped (which potential users of <code class="inline"><a class="l_k" href="../functions/pop.html">pop</a></code> would do well to remember).
If LIMIT is negative, it is treated as if an arbitrarily large LIMIT
had been specified.  Note that splitting an EXPR that evaluates to the
empty string always returns the empty list, regardless of the LIMIT
specified.</p>
<p>A pattern matching the null string (not to be confused with
a null pattern <code class="inline"><span class="q">//</span></code>
, which is just one member of the set of patterns
matching a null string) will split the value of EXPR into separate
characters at each point it matches that way.  For example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">':'</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/ */</span><span class="cm">,</span> <span class="q">'hi there'</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>produces the output &#39;h:i:t:h:e:r:e&#39;.</p>
<p>As a special case for <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code>, using the empty pattern <code class="inline"><span class="q">//</span></code>
 specifically
matches only the null string, and is not be confused with the regular use
of <code class="inline"><span class="q">//</span></code>
 to mean &quot;the last successful pattern match&quot;.  So, for <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code>,
the following:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">':'</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">//</span><span class="cm">,</span> <span class="q">'hi there'</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>produces the output &#39;h:i: :t:h:e:r:e&#39;.</p>
<p>Empty leading fields are produced when there are positive-width matches at
the beginning of the string; a zero-width match at the beginning of
the string does not produce an empty field. For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">&#39;:&#39;</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/(?=\w)/</span><span class="cm">,</span> <span class="q">&#39;hi there!&#39;</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>produces the output &#39;h:i :t:h:e:r:e!&#39;. Empty trailing fields, on the other
hand, are produced when there is a match at the end of the string (and
when LIMIT is given and is not 0), regardless of the length of the match.
For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">':'</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">//</span><span class="cm">,</span>   <span class="q">'hi there!'</span><span class="cm">,</span> <span class="n">-1</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">':'</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/\W/</span><span class="cm">,</span> <span class="q">'hi there!'</span><span class="cm">,</span> <span class="n">-1</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>produce the output &#39;h:i: :t:h:e:r:e:!:&#39; and &#39;hi:there:&#39;, respectively,
both with an empty trailing field.</p>
<p>The LIMIT parameter can be used to split a line partially</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$login</span><span class="cm">,</span> <span class="i">$passwd</span><span class="cm">,</span> <span class="i">$remainder</span><span class="s">)</span> = <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/:/</span><span class="cm">,</span> <span class="i">$_</span><span class="cm">,</span> <span class="n">3</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>When assigning to a list, if LIMIT is omitted, or zero, Perl supplies
a LIMIT one larger than the number of variables in the list, to avoid
unnecessary work.  For the list above LIMIT would have been 4 by
default.  In time critical applications it behooves you not to split
into more fields than you really need.</p>
<p>If the PATTERN contains parentheses, additional list elements are
created from each matching substring in the delimiter.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/([,-])/</span><span class="cm">,</span> <span class="q">&quot;1-10,20&quot;</span><span class="cm">,</span> <span class="n">3</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>produces the list value</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="n">1</span><span class="cm">,</span> <span class="q">&#39;-&#39;</span><span class="cm">,</span> <span class="n">10</span><span class="cm">,</span> <span class="q">&#39;,&#39;</span><span class="cm">,</span> <span class="n">20</span><span class="s">)</span></li></ol></pre><p>If you had the entire header of a normal Unix email message in $header,
you could split it up into fields and their values this way:</p>
<pre class="verbatim"><ol><li>    <span class="i">$header</span> =~ <span class="q">s/\n(?=\s)//g</span><span class="sc">;</span>  <span class="c"># fix continuation lines</span></li><li>    <span class="i">%hdrs</span>   =  <span class="s">(</span><span class="w">UNIX_FROM</span> <span class="cm">=&gt;</span> <a class="l_k" href="../functions/split.html">split</a> <span class="q">/^(\S*?):\s*/m</span><span class="cm">,</span> <span class="i">$header</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The pattern <code class="inline"><span class="q">/PATTERN/</span></code>
 may be replaced with an expression to specify
patterns that vary at runtime.  (To do runtime compilation only once,
use <code class="inline"><span class="q">/$variable/o</span></code>
.)</p>
<p>As a special case, specifying a PATTERN of space (<code class="inline"><span class="q">&#39; &#39;</span></code>
) will split on
white space just as <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code> with no arguments does.  Thus, <code class="inline"><a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">&#39; &#39;</span><span class="s">)</span></code>
 can
be used to emulate <b>awk</b>&#39;s default behavior, whereas <code class="inline"><a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/ /</span><span class="s">)</span></code>

will give you as many null initial fields as there are leading spaces.
A <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code> on <code class="inline"><span class="q">/\s+/</span></code>
 is like a <code class="inline"><a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">&#39; &#39;</span><span class="s">)</span></code>
 except that any leading
whitespace produces a null first field.  A <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code> with no arguments
really does a <code class="inline"><a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">&#39; &#39;</span><span class="cm">,</span> <span class="i">$_</span><span class="s">)</span></code>
 internally.</p>
<p>A PATTERN of <code class="inline"><span class="q">/^/</span></code>
 is treated as if it were <code class="inline"><span class="q">/^/m</span></code>
, since it isn&#39;t
much use otherwise.</p>
<p>Example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">PASSWD</span><span class="cm">,</span> <span class="q">&#39;/etc/passwd&#39;</span><span class="s">)</span><span class="sc">;</span></li><li>    while <span class="s">(</span><span class="q">&lt;PASSWD&gt;</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="../functions/chomp.html">chomp</a><span class="sc">;</span></li><li>        <span class="s">(</span><span class="i">$login</span><span class="cm">,</span> <span class="i">$passwd</span><span class="cm">,</span> <span class="i">$uid</span><span class="cm">,</span> <span class="i">$gid</span><span class="cm">,</span></li><li>         <span class="i">$gcos</span><span class="cm">,</span> <span class="i">$home</span><span class="cm">,</span> <span class="i">$shell</span><span class="s">)</span> = <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/:/</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="c">#...</span></li><li>    <span class="s">}</span></li></ol></pre><p>As with regular pattern matching, any capturing parentheses that are not
matched in a <code class="inline"><a class="l_k" href="../functions/split.html">split()</a></code> will be set to <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> when returned:</p>
<pre class="verbatim"><ol><li>    <span class="i">@fields</span> = <a class="l_k" href="../functions/split.html">split</a> <span class="q">/(A)|B/</span><span class="cm">,</span> <span class="q">&quot;1A2B3&quot;</span><span class="sc">;</span></li><li>    <span class="c"># @fields is (1, &#39;A&#39;, 2, undef, 3)</span></li></ol><
-----

function split
/pattern/|
expr|
Splits the string EXPR into a list of strings and returns that list.  By
default, empty leading fields are preserved, and empty trailing ones are
deleted.  (If all fields are empty, they are considered to be trailing.)</p>
<p>In scalar context, returns the number of fields found. In scalar and void
context it splits into the <code class="inline"><span class="i">@_</span></code>
 array.  Use of split in scalar and void
context is deprecated, however, because it clobbers your subroutine
arguments.</p>
<p>If EXPR is omitted, splits the <code class="inline"><span class="i">$_</span></code>
 string.  If PATTERN is also omitted,
splits on whitespace (after skipping any leading whitespace).  Anything
matching PATTERN is taken to be a delimiter separating the fields.  (Note
that the delimiter may be longer than one character.)</p>
<p>If LIMIT is specified and positive, it represents the maximum number
of fields the EXPR will be split into, though the actual number of
fields returned depends on the number of times PATTERN matches within
EXPR.  If LIMIT is unspecified or zero, trailing null fields are
stripped (which potential users of <code class="inline"><a class="l_k" href="../functions/pop.html">pop</a></code> would do well to remember).
If LIMIT is negative, it is treated as if an arbitrarily large LIMIT
had been specified.  Note that splitting an EXPR that evaluates to the
empty string always returns the empty list, regardless of the LIMIT
specified.</p>
<p>A pattern matching the null string (not to be confused with
a null pattern <code class="inline"><span class="q">//</span></code>
, which is just one member of the set of patterns
matching a null string) will split the value of EXPR into separate
characters at each point it matches that way.  For example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">':'</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/ */</span><span class="cm">,</span> <span class="q">'hi there'</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>produces the output &#39;h:i:t:h:e:r:e&#39;.</p>
<p>As a special case for <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code>, using the empty pattern <code class="inline"><span class="q">//</span></code>
 specifically
matches only the null string, and is not be confused with the regular use
of <code class="inline"><span class="q">//</span></code>
 to mean &quot;the last successful pattern match&quot;.  So, for <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code>,
the following:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">':'</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">//</span><span class="cm">,</span> <span class="q">'hi there'</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>produces the output &#39;h:i: :t:h:e:r:e&#39;.</p>
<p>Empty leading fields are produced when there are positive-width matches at
the beginning of the string; a zero-width match at the beginning of
the string does not produce an empty field. For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">&#39;:&#39;</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/(?=\w)/</span><span class="cm">,</span> <span class="q">&#39;hi there!&#39;</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>produces the output &#39;h:i :t:h:e:r:e!&#39;. Empty trailing fields, on the other
hand, are produced when there is a match at the end of the string (and
when LIMIT is given and is not 0), regardless of the length of the match.
For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">':'</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">//</span><span class="cm">,</span>   <span class="q">'hi there!'</span><span class="cm">,</span> <span class="n">-1</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">':'</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/\W/</span><span class="cm">,</span> <span class="q">'hi there!'</span><span class="cm">,</span> <span class="n">-1</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>produce the output &#39;h:i: :t:h:e:r:e:!:&#39; and &#39;hi:there:&#39;, respectively,
both with an empty trailing field.</p>
<p>The LIMIT parameter can be used to split a line partially</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$login</span><span class="cm">,</span> <span class="i">$passwd</span><span class="cm">,</span> <span class="i">$remainder</span><span class="s">)</span> = <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/:/</span><span class="cm">,</span> <span class="i">$_</span><span class="cm">,</span> <span class="n">3</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>When assigning to a list, if LIMIT is omitted, or zero, Perl supplies
a LIMIT one larger than the number of variables in the list, to avoid
unnecessary work.  For the list above LIMIT would have been 4 by
default.  In time critical applications it behooves you not to split
into more fields than you really need.</p>
<p>If the PATTERN contains parentheses, additional list elements are
created from each matching substring in the delimiter.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/([,-])/</span><span class="cm">,</span> <span class="q">&quot;1-10,20&quot;</span><span class="cm">,</span> <span class="n">3</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>produces the list value</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="n">1</span><span class="cm">,</span> <span class="q">&#39;-&#39;</span><span class="cm">,</span> <span class="n">10</span><span class="cm">,</span> <span class="q">&#39;,&#39;</span><span class="cm">,</span> <span class="n">20</span><span class="s">)</span></li></ol></pre><p>If you had the entire header of a normal Unix email message in $header,
you could split it up into fields and their values this way:</p>
<pre class="verbatim"><ol><li>    <span class="i">$header</span> =~ <span class="q">s/\n(?=\s)//g</span><span class="sc">;</span>  <span class="c"># fix continuation lines</span></li><li>    <span class="i">%hdrs</span>   =  <span class="s">(</span><span class="w">UNIX_FROM</span> <span class="cm">=&gt;</span> <a class="l_k" href="../functions/split.html">split</a> <span class="q">/^(\S*?):\s*/m</span><span class="cm">,</span> <span class="i">$header</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The pattern <code class="inline"><span class="q">/PATTERN/</span></code>
 may be replaced with an expression to specify
patterns that vary at runtime.  (To do runtime compilation only once,
use <code class="inline"><span class="q">/$variable/o</span></code>
.)</p>
<p>As a special case, specifying a PATTERN of space (<code class="inline"><span class="q">&#39; &#39;</span></code>
) will split on
white space just as <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code> with no arguments does.  Thus, <code class="inline"><a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">&#39; &#39;</span><span class="s">)</span></code>
 can
be used to emulate <b>awk</b>&#39;s default behavior, whereas <code class="inline"><a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/ /</span><span class="s">)</span></code>

will give you as many null initial fields as there are leading spaces.
A <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code> on <code class="inline"><span class="q">/\s+/</span></code>
 is like a <code class="inline"><a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">&#39; &#39;</span><span class="s">)</span></code>
 except that any leading
whitespace produces a null first field.  A <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code> with no arguments
really does a <code class="inline"><a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">&#39; &#39;</span><span class="cm">,</span> <span class="i">$_</span><span class="s">)</span></code>
 internally.</p>
<p>A PATTERN of <code class="inline"><span class="q">/^/</span></code>
 is treated as if it were <code class="inline"><span class="q">/^/m</span></code>
, since it isn&#39;t
much use otherwise.</p>
<p>Example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">PASSWD</span><span class="cm">,</span> <span class="q">&#39;/etc/passwd&#39;</span><span class="s">)</span><span class="sc">;</span></li><li>    while <span class="s">(</span><span class="q">&lt;PASSWD&gt;</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="../functions/chomp.html">chomp</a><span class="sc">;</span></li><li>        <span class="s">(</span><span class="i">$login</span><span class="cm">,</span> <span class="i">$passwd</span><span class="cm">,</span> <span class="i">$uid</span><span class="cm">,</span> <span class="i">$gid</span><span class="cm">,</span></li><li>         <span class="i">$gcos</span><span class="cm">,</span> <span class="i">$home</span><span class="cm">,</span> <span class="i">$shell</span><span class="s">)</span> = <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/:/</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="c">#...</span></li><li>    <span class="s">}</span></li></ol></pre><p>As with regular pattern matching, any capturing parentheses that are not
matched in a <code class="inline"><a class="l_k" href="../functions/split.html">split()</a></code> will be set to <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> when returned:</p>
<pre class="verbatim"><ol><li>    <span class="i">@fields</span> = <a class="l_k" href="../functions/split.html">split</a> <span class="q">/(A)|B/</span><span class="cm">,</span> <span class="q">&quot;1A2B3&quot;</span><span class="sc">;</span></li><li>    <span class="c"># @fields is (1, &#39;A&#39;, 2, undef, 3)</span></li></ol><
-----

function split
/pattern/|
Splits the string EXPR into a list of strings and returns that list.  By
default, empty leading fields are preserved, and empty trailing ones are
deleted.  (If all fields are empty, they are considered to be trailing.)</p>
<p>In scalar context, returns the number of fields found. In scalar and void
context it splits into the <code class="inline"><span class="i">@_</span></code>
 array.  Use of split in scalar and void
context is deprecated, however, because it clobbers your subroutine
arguments.</p>
<p>If EXPR is omitted, splits the <code class="inline"><span class="i">$_</span></code>
 string.  If PATTERN is also omitted,
splits on whitespace (after skipping any leading whitespace).  Anything
matching PATTERN is taken to be a delimiter separating the fields.  (Note
that the delimiter may be longer than one character.)</p>
<p>If LIMIT is specified and positive, it represents the maximum number
of fields the EXPR will be split into, though the actual number of
fields returned depends on the number of times PATTERN matches within
EXPR.  If LIMIT is unspecified or zero, trailing null fields are
stripped (which potential users of <code class="inline"><a class="l_k" href="../functions/pop.html">pop</a></code> would do well to remember).
If LIMIT is negative, it is treated as if an arbitrarily large LIMIT
had been specified.  Note that splitting an EXPR that evaluates to the
empty string always returns the empty list, regardless of the LIMIT
specified.</p>
<p>A pattern matching the null string (not to be confused with
a null pattern <code class="inline"><span class="q">//</span></code>
, which is just one member of the set of patterns
matching a null string) will split the value of EXPR into separate
characters at each point it matches that way.  For example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">':'</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/ */</span><span class="cm">,</span> <span class="q">'hi there'</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>produces the output &#39;h:i:t:h:e:r:e&#39;.</p>
<p>As a special case for <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code>, using the empty pattern <code class="inline"><span class="q">//</span></code>
 specifically
matches only the null string, and is not be confused with the regular use
of <code class="inline"><span class="q">//</span></code>
 to mean &quot;the last successful pattern match&quot;.  So, for <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code>,
the following:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">':'</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">//</span><span class="cm">,</span> <span class="q">'hi there'</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>produces the output &#39;h:i: :t:h:e:r:e&#39;.</p>
<p>Empty leading fields are produced when there are positive-width matches at
the beginning of the string; a zero-width match at the beginning of
the string does not produce an empty field. For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">&#39;:&#39;</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/(?=\w)/</span><span class="cm">,</span> <span class="q">&#39;hi there!&#39;</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>produces the output &#39;h:i :t:h:e:r:e!&#39;. Empty trailing fields, on the other
hand, are produced when there is a match at the end of the string (and
when LIMIT is given and is not 0), regardless of the length of the match.
For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">':'</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">//</span><span class="cm">,</span>   <span class="q">'hi there!'</span><span class="cm">,</span> <span class="n">-1</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">':'</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/\W/</span><span class="cm">,</span> <span class="q">'hi there!'</span><span class="cm">,</span> <span class="n">-1</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>produce the output &#39;h:i: :t:h:e:r:e:!:&#39; and &#39;hi:there:&#39;, respectively,
both with an empty trailing field.</p>
<p>The LIMIT parameter can be used to split a line partially</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$login</span><span class="cm">,</span> <span class="i">$passwd</span><span class="cm">,</span> <span class="i">$remainder</span><span class="s">)</span> = <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/:/</span><span class="cm">,</span> <span class="i">$_</span><span class="cm">,</span> <span class="n">3</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>When assigning to a list, if LIMIT is omitted, or zero, Perl supplies
a LIMIT one larger than the number of variables in the list, to avoid
unnecessary work.  For the list above LIMIT would have been 4 by
default.  In time critical applications it behooves you not to split
into more fields than you really need.</p>
<p>If the PATTERN contains parentheses, additional list elements are
created from each matching substring in the delimiter.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/([,-])/</span><span class="cm">,</span> <span class="q">&quot;1-10,20&quot;</span><span class="cm">,</span> <span class="n">3</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>produces the list value</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="n">1</span><span class="cm">,</span> <span class="q">&#39;-&#39;</span><span class="cm">,</span> <span class="n">10</span><span class="cm">,</span> <span class="q">&#39;,&#39;</span><span class="cm">,</span> <span class="n">20</span><span class="s">)</span></li></ol></pre><p>If you had the entire header of a normal Unix email message in $header,
you could split it up into fields and their values this way:</p>
<pre class="verbatim"><ol><li>    <span class="i">$header</span> =~ <span class="q">s/\n(?=\s)//g</span><span class="sc">;</span>  <span class="c"># fix continuation lines</span></li><li>    <span class="i">%hdrs</span>   =  <span class="s">(</span><span class="w">UNIX_FROM</span> <span class="cm">=&gt;</span> <a class="l_k" href="../functions/split.html">split</a> <span class="q">/^(\S*?):\s*/m</span><span class="cm">,</span> <span class="i">$header</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The pattern <code class="inline"><span class="q">/PATTERN/</span></code>
 may be replaced with an expression to specify
patterns that vary at runtime.  (To do runtime compilation only once,
use <code class="inline"><span class="q">/$variable/o</span></code>
.)</p>
<p>As a special case, specifying a PATTERN of space (<code class="inline"><span class="q">&#39; &#39;</span></code>
) will split on
white space just as <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code> with no arguments does.  Thus, <code class="inline"><a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">&#39; &#39;</span><span class="s">)</span></code>
 can
be used to emulate <b>awk</b>&#39;s default behavior, whereas <code class="inline"><a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/ /</span><span class="s">)</span></code>

will give you as many null initial fields as there are leading spaces.
A <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code> on <code class="inline"><span class="q">/\s+/</span></code>
 is like a <code class="inline"><a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">&#39; &#39;</span><span class="s">)</span></code>
 except that any leading
whitespace produces a null first field.  A <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code> with no arguments
really does a <code class="inline"><a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">&#39; &#39;</span><span class="cm">,</span> <span class="i">$_</span><span class="s">)</span></code>
 internally.</p>
<p>A PATTERN of <code class="inline"><span class="q">/^/</span></code>
 is treated as if it were <code class="inline"><span class="q">/^/m</span></code>
, since it isn&#39;t
much use otherwise.</p>
<p>Example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">PASSWD</span><span class="cm">,</span> <span class="q">&#39;/etc/passwd&#39;</span><span class="s">)</span><span class="sc">;</span></li><li>    while <span class="s">(</span><span class="q">&lt;PASSWD&gt;</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="../functions/chomp.html">chomp</a><span class="sc">;</span></li><li>        <span class="s">(</span><span class="i">$login</span><span class="cm">,</span> <span class="i">$passwd</span><span class="cm">,</span> <span class="i">$uid</span><span class="cm">,</span> <span class="i">$gid</span><span class="cm">,</span></li><li>         <span class="i">$gcos</span><span class="cm">,</span> <span class="i">$home</span><span class="cm">,</span> <span class="i">$shell</span><span class="s">)</span> = <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/:/</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="c">#...</span></li><li>    <span class="s">}</span></li></ol></pre><p>As with regular pattern matching, any capturing parentheses that are not
matched in a <code class="inline"><a class="l_k" href="../functions/split.html">split()</a></code> will be set to <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> when returned:</p>
<pre class="verbatim"><ol><li>    <span class="i">@fields</span> = <a class="l_k" href="../functions/split.html">split</a> <span class="q">/(A)|B/</span><span class="cm">,</span> <span class="q">&quot;1A2B3&quot;</span><span class="sc">;</span></li><li>    <span class="c"># @fields is (1, &#39;A&#39;, 2, undef, 3)</span></li></ol><
-----

function split
Splits the string EXPR into a list of strings and returns that list.  By
default, empty leading fields are preserved, and empty trailing ones are
deleted.  (If all fields are empty, they are considered to be trailing.)</p>
<p>In scalar context, returns the number of fields found. In scalar and void
context it splits into the <code class="inline"><span class="i">@_</span></code>
 array.  Use of split in scalar and void
context is deprecated, however, because it clobbers your subroutine
arguments.</p>
<p>If EXPR is omitted, splits the <code class="inline"><span class="i">$_</span></code>
 string.  If PATTERN is also omitted,
splits on whitespace (after skipping any leading whitespace).  Anything
matching PATTERN is taken to be a delimiter separating the fields.  (Note
that the delimiter may be longer than one character.)</p>
<p>If LIMIT is specified and positive, it represents the maximum number
of fields the EXPR will be split into, though the actual number of
fields returned depends on the number of times PATTERN matches within
EXPR.  If LIMIT is unspecified or zero, trailing null fields are
stripped (which potential users of <code class="inline"><a class="l_k" href="../functions/pop.html">pop</a></code> would do well to remember).
If LIMIT is negative, it is treated as if an arbitrarily large LIMIT
had been specified.  Note that splitting an EXPR that evaluates to the
empty string always returns the empty list, regardless of the LIMIT
specified.</p>
<p>A pattern matching the null string (not to be confused with
a null pattern <code class="inline"><span class="q">//</span></code>
, which is just one member of the set of patterns
matching a null string) will split the value of EXPR into separate
characters at each point it matches that way.  For example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">':'</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/ */</span><span class="cm">,</span> <span class="q">'hi there'</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>produces the output &#39;h:i:t:h:e:r:e&#39;.</p>
<p>As a special case for <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code>, using the empty pattern <code class="inline"><span class="q">//</span></code>
 specifically
matches only the null string, and is not be confused with the regular use
of <code class="inline"><span class="q">//</span></code>
 to mean &quot;the last successful pattern match&quot;.  So, for <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code>,
the following:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">':'</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">//</span><span class="cm">,</span> <span class="q">'hi there'</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>produces the output &#39;h:i: :t:h:e:r:e&#39;.</p>
<p>Empty leading fields are produced when there are positive-width matches at
the beginning of the string; a zero-width match at the beginning of
the string does not produce an empty field. For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">&#39;:&#39;</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/(?=\w)/</span><span class="cm">,</span> <span class="q">&#39;hi there!&#39;</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>produces the output &#39;h:i :t:h:e:r:e!&#39;. Empty trailing fields, on the other
hand, are produced when there is a match at the end of the string (and
when LIMIT is given and is not 0), regardless of the length of the match.
For example:</p>
<pre class="verbatim"><ol><li>   <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">':'</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">//</span><span class="cm">,</span>   <span class="q">'hi there!'</span><span class="cm">,</span> <span class="n">-1</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>   <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/join.html">join</a><span class="s">(</span><span class="q">':'</span><span class="cm">,</span> <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/\W/</span><span class="cm">,</span> <span class="q">'hi there!'</span><span class="cm">,</span> <span class="n">-1</span><span class="s">)</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>produce the output &#39;h:i: :t:h:e:r:e:!:&#39; and &#39;hi:there:&#39;, respectively,
both with an empty trailing field.</p>
<p>The LIMIT parameter can be used to split a line partially</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$login</span><span class="cm">,</span> <span class="i">$passwd</span><span class="cm">,</span> <span class="i">$remainder</span><span class="s">)</span> = <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/:/</span><span class="cm">,</span> <span class="i">$_</span><span class="cm">,</span> <span class="n">3</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>When assigning to a list, if LIMIT is omitted, or zero, Perl supplies
a LIMIT one larger than the number of variables in the list, to avoid
unnecessary work.  For the list above LIMIT would have been 4 by
default.  In time critical applications it behooves you not to split
into more fields than you really need.</p>
<p>If the PATTERN contains parentheses, additional list elements are
created from each matching substring in the delimiter.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/([,-])/</span><span class="cm">,</span> <span class="q">&quot;1-10,20&quot;</span><span class="cm">,</span> <span class="n">3</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>produces the list value</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="n">1</span><span class="cm">,</span> <span class="q">&#39;-&#39;</span><span class="cm">,</span> <span class="n">10</span><span class="cm">,</span> <span class="q">&#39;,&#39;</span><span class="cm">,</span> <span class="n">20</span><span class="s">)</span></li></ol></pre><p>If you had the entire header of a normal Unix email message in $header,
you could split it up into fields and their values this way:</p>
<pre class="verbatim"><ol><li>    <span class="i">$header</span> =~ <span class="q">s/\n(?=\s)//g</span><span class="sc">;</span>  <span class="c"># fix continuation lines</span></li><li>    <span class="i">%hdrs</span>   =  <span class="s">(</span><span class="w">UNIX_FROM</span> <span class="cm">=&gt;</span> <a class="l_k" href="../functions/split.html">split</a> <span class="q">/^(\S*?):\s*/m</span><span class="cm">,</span> <span class="i">$header</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The pattern <code class="inline"><span class="q">/PATTERN/</span></code>
 may be replaced with an expression to specify
patterns that vary at runtime.  (To do runtime compilation only once,
use <code class="inline"><span class="q">/$variable/o</span></code>
.)</p>
<p>As a special case, specifying a PATTERN of space (<code class="inline"><span class="q">&#39; &#39;</span></code>
) will split on
white space just as <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code> with no arguments does.  Thus, <code class="inline"><a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">&#39; &#39;</span><span class="s">)</span></code>
 can
be used to emulate <b>awk</b>&#39;s default behavior, whereas <code class="inline"><a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/ /</span><span class="s">)</span></code>

will give you as many null initial fields as there are leading spaces.
A <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code> on <code class="inline"><span class="q">/\s+/</span></code>
 is like a <code class="inline"><a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">&#39; &#39;</span><span class="s">)</span></code>
 except that any leading
whitespace produces a null first field.  A <code class="inline"><a class="l_k" href="../functions/split.html">split</a></code> with no arguments
really does a <code class="inline"><a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">&#39; &#39;</span><span class="cm">,</span> <span class="i">$_</span><span class="s">)</span></code>
 internally.</p>
<p>A PATTERN of <code class="inline"><span class="q">/^/</span></code>
 is treated as if it were <code class="inline"><span class="q">/^/m</span></code>
, since it isn&#39;t
much use otherwise.</p>
<p>Example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/open.html">open</a><span class="s">(</span><span class="w">PASSWD</span><span class="cm">,</span> <span class="q">&#39;/etc/passwd&#39;</span><span class="s">)</span><span class="sc">;</span></li><li>    while <span class="s">(</span><span class="q">&lt;PASSWD&gt;</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="../functions/chomp.html">chomp</a><span class="sc">;</span></li><li>        <span class="s">(</span><span class="i">$login</span><span class="cm">,</span> <span class="i">$passwd</span><span class="cm">,</span> <span class="i">$uid</span><span class="cm">,</span> <span class="i">$gid</span><span class="cm">,</span></li><li>         <span class="i">$gcos</span><span class="cm">,</span> <span class="i">$home</span><span class="cm">,</span> <span class="i">$shell</span><span class="s">)</span> = <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">/:/</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="c">#...</span></li><li>    <span class="s">}</span></li></ol></pre><p>As with regular pattern matching, any capturing parentheses that are not
matched in a <code class="inline"><a class="l_k" href="../functions/split.html">split()</a></code> will be set to <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> when returned:</p>
<pre class="verbatim"><ol><li>    <span class="i">@fields</span> = <a class="l_k" href="../functions/split.html">split</a> <span class="q">/(A)|B/</span><span class="cm">,</span> <span class="q">&quot;1A2B3&quot;</span><span class="sc">;</span></li><li>    <span class="c"># @fields is (1, &#39;A&#39;, 2, undef, 3)</span></li></ol><
-----

function sprintf
format|
list|
Returns a string formatted by the usual <code class="inline"><a class="l_k" href="../functions/printf.html">printf</a></code> conventions of the C
library function <code class="inline"><a class="l_k" href="../functions/sprintf.html">sprintf</a></code>.  See below for more details
and see <code class="inline"><a class="l_k" href="../functions/sprintf.html">sprintf(3)</a></code> or <code class="inline"><a class="l_k" href="../functions/printf.html">printf(3)</a></code> on your system for an explanation of
the general principles.</p>
<p>For example:</p>
<pre class="verbatim"><ol><li>        <span class="c"># Format number with up to 8 leading zeroes</span></li><li>        <span class="i">$result</span> = <a class="l_k" href="../functions/sprintf.html">sprintf</a><span class="s">(</span><span class="q">&quot;%08d&quot;</span><span class="cm">,</span> <span class="i">$number</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>        <span class="c"># Round number to 3 digits after decimal point</span></li><li>        <span class="i">$rounded</span> = <a class="l_k" href="../functions/sprintf.html">sprintf</a><span class="s">(</span><span class="q">&quot;%.3f&quot;</span><span class="cm">,</span> <span class="i">$number</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Perl does its own <code class="inline"><a class="l_k" href="../functions/sprintf.html">sprintf</a></code> formatting--it emulates the C
function <code class="inline"><a class="l_k" href="../functions/sprintf.html">sprintf</a></code>, but it doesn&#39;t use it (except for floating-point
numbers, and even then only the standard modifiers are allowed).  As a
result, any non-standard extensions in your local <code class="inline"><a class="l_k" href="../functions/sprintf.html">sprintf</a></code> are not
available from Perl.</p>
<p>Unlike <code class="inline"><a class="l_k" href="../functions/printf.html">printf</a></code>, <code class="inline"><a class="l_k" href="../functions/sprintf.html">sprintf</a></code> does not do what you probably mean when you
pass it an array as your first argument. The array is given scalar context,
and instead of using the 0th element of the array as the format, Perl will
use the count of elements in the array as the format, which is almost never
useful.</p>
<p>Perl&#39;s <code class="inline"><a class="l_k" href="../functions/sprintf.html">sprintf</a></code> permits the following universally-known conversions:</p>
<pre class="verbatim"><ol><li>   %%	a percent sign</li><li>   %c	a character with the given number</li><li>   %s	a string</li><li>   %d	a signed integer, in decimal</li><li>   %u	an unsigned integer, in decimal</li><li>   %o	an unsigned integer, in octal</li><li>   %x	an unsigned integer, in hexadecimal</li><li>   %e	a floating-point number, in scientific notation</li><li>   %f	a floating-point number, in fixed decimal notation</li><li>   %g	a floating-point number, in %e or %f notation</li></ol></pre><p>In addition, Perl permits the following widely-supported conversions:</p>
<pre class="verbatim"><ol><li>   %X	like %x, but using upper-case letters</li><li>   %E	like %e, but using an upper-case "E"</li><li>   %G	like %g, but with an upper-case "E" (if applicable)</li><li>   %b	an unsigned integer, in binary</li><li>   %B	like %b, but using an upper-case "B" with the # flag</li><li>   %p	a pointer (outputs the Perl value's address in hexadecimal)</li><li>   %n	special: *stores* the number of characters output so far</li><li>        into the next variable in the parameter list</li></ol></pre><p>Finally, for backward (and we do mean &quot;backward&quot;) compatibility, Perl
permits these unnecessary but widely-supported conversions:</p>
<pre class="verbatim"><ol><li>   %i	a synonym for %d</li><li>   %D	a synonym for %ld</li><li>   %U	a synonym for %lu</li><li>   %O	a synonym for %lo</li><li>   %F	a synonym for %f</li></ol></pre><p>Note that the number of exponent digits in the scientific notation produced
by <code class="inline"><span class="i">%e</span></code>
, <code class="inline"><span class="i">%E</span></code>
, <code class="inline"><span class="i">%g</span></code>
 and <code class="inline"><span class="i">%G</span></code>
 for numbers with the modulus of the
exponent less than 100 is system-dependent: it may be three or less
(zero-padded as necessary).  In other words, 1.23 times ten to the
99th may be either &quot;1.23e99&quot; or &quot;1.23e099&quot;.</p>
<p>Between the <code class="inline"><span class="i">%</span></code>
 and the format letter, you may specify a number of
additional attributes controlling the interpretation of the format.
In order, these are:</p>
<ul>
<li><a name="format-parameter-index"></a><b>format parameter index</b>
<p>An explicit format parameter index, such as <code class="inline">2$</code>. By default sprintf
will format the next unused argument in the list, but this allows you
to take the arguments out of order, e.g.:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;%2$d %1$d&#39;</span><span class="cm">,</span> <span class="n">12</span><span class="cm">,</span> <span class="n">34</span><span class="sc">;</span>      <span class="c"># prints &quot;34 12&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;%3$d %d %1$d&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="cm">,</span> <span class="n">2</span><span class="cm">,</span> <span class="n">3</span><span class="sc">;</span>  <span class="c"># prints &quot;3 1 1&quot;</span></li></ol></pre></li>
<li><a name="flags"></a><b>flags</b>
<p>one or more of:</p>
<pre class="verbatim"><ol><li>   space   prefix non-negative number with a space</li><li>   +       prefix non-negative number with a plus sign</li><li>   -       left-justify within the field</li><li>   0       use zeros, not spaces, to right-justify</li><li>   #       ensure the leading "0" for any octal,</li><li>           prefix non-zero hexadecimal with "0x" or "0X",</li><li>           prefix non-zero binary with "0b" or "0B"</li></ol></pre><p>For example:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">'&lt;% d&gt;'</span><span class="cm">,</span>  <span class="n">12</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt; 12&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">'&lt;%+d&gt;'</span><span class="cm">,</span>  <span class="n">12</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;+12&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">'&lt;%6s&gt;'</span><span class="cm">,</span>  <span class="n">12</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;    12&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">'&lt;%-6s&gt;'</span><span class="cm">,</span> <span class="n">12</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;12    &gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">'&lt;%06s&gt;'</span><span class="cm">,</span> <span class="n">12</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;000012&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">'&lt;%#o&gt;'</span><span class="cm">,</span>  <span class="n">12</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;014&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">'&lt;%#x&gt;'</span><span class="cm">,</span>  <span class="n">12</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;0xc&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">'&lt;%#X&gt;'</span><span class="cm">,</span>  <span class="n">12</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;0XC&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">'&lt;%#b&gt;'</span><span class="cm">,</span>  <span class="n">12</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;0b1100&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">'&lt;%#B&gt;'</span><span class="cm">,</span>  <span class="n">12</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;0B1100&gt;&quot;</span></li></ol></pre><p>When a space and a plus sign are given as the flags at once,
a plus sign is used to prefix a positive number.</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">'&lt;%+ d&gt;'</span><span class="cm">,</span> <span class="n">12</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;+12&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">'&lt;% +d&gt;'</span><span class="cm">,</span> <span class="n">12</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;+12&gt;&quot;</span></li></ol></pre><p>When the # flag and a precision are given in the %o conversion,
the precision is incremented if it&#39;s necessary for the leading &quot;0&quot;.</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">'&lt;%#.5o&gt;'</span><span class="cm">,</span> <span class="n">012</span><span class="sc">;</span>      <span class="c"># prints &quot;&lt;00012&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">'&lt;%#.5o&gt;'</span><span class="cm">,</span> <span class="n">012345</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;012345&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">'&lt;%#.0o&gt;'</span><span class="cm">,</span> <span class="n">0</span><span class="sc">;</span>        <span class="c"># prints &quot;&lt;0&gt;&quot;</span></li></ol></pre></li>
<li><a name="vector-flag"></a><b>vector flag</b>
<p>This flag tells perl to interpret the supplied string as a vector of
integers, one for each character in the string. Perl applies the format to
each integer in turn, then joins the resulting strings with a separator (a
dot <code class="inline">.</code> by default). This can be useful for displaying ordinal values of
characters in arbitrary strings:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;%vd&quot;</span><span class="cm">,</span> <span class="q">&quot;AB\x{100}&quot;</span><span class="sc">;</span>           <span class="c"># prints &quot;65.66.256&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;version is v%vd\n&quot;</span><span class="cm">,</span> <span class="i">$^V</span><span class="sc">;</span>     <span class="c"># Perl&#39;s version</span></li></ol></pre><p>Put an asterisk <code class="inline"><span class="i">*</span></code>
 before the <code class="inline"><span class="w">v</span></code>
 to override the string to
use to separate the numbers:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;address is %*vX\n&quot;</span><span class="cm">,</span> <span class="q">&quot;:&quot;</span><span class="cm">,</span> <span class="i">$addr</span><span class="sc">;</span>   <span class="c"># IPv6 address</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;bits are %0*v8b\n&quot;</span><span class="cm">,</span> <span class="q">&quot; &quot;</span><span class="cm">,</span> <span class="i">$bits</span><span class="sc">;</span>   <span class="c"># random bitstring</span></li></ol></pre><p>You can also explicitly specify the argument number to use for
the join string using e.g. <code class="inline">*2$v</code>:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;%*4$vX %*4$vX %*4$vX&#39;</span><span class="cm">,</span> <span class="i">@addr</span>[<span class="n">1</span>..<span class="n">3</span>]<span class="cm">,</span> <span class="q">&quot;:&quot;</span><span class="sc">;</span>   <span class="c"># 3 IPv6 addresses</span></li></ol></pre></li>
<li><a name="(minimum)-width"></a><b>(minimum) width</b>
<p>Arguments are usually formatted to be only as wide as required to
display the given value. You can override the width by putting
a number here, or get the width from the next argument (with <code class="inline"><span class="i">*</span></code>
)
or from a specified argument (with e.g. <code class="inline">*2$</code>):</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%s&gt;&#39;</span><span class="cm">,</span> <span class="q">&quot;a&quot;</span><span class="sc">;</span>       <span class="c"># prints &quot;&lt;a&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%6s&gt;&#39;</span><span class="cm">,</span> <span class="q">&quot;a&quot;</span><span class="sc">;</span>      <span class="c"># prints &quot;&lt;     a&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%*s&gt;&#39;</span><span class="cm">,</span> <span class="n">6</span><span class="cm">,</span> <span class="q">&quot;a&quot;</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;     a&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%*2$s&gt;&#39;</span><span class="cm">,</span> <span class="q">&quot;a&quot;</span><span class="cm">,</span> <span class="n">6</span><span class="sc">;</span> <span class="c"># prints &quot;&lt;     a&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%2s&gt;&#39;</span><span class="cm">,</span> <span class="q">&quot;long&quot;</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;long&gt;&quot; (does not truncate)</span></li></ol></pre><p>If a field width obtained through <code class="inline"><span class="i">*</span></code>
 is negative, it has the same
effect as the <code class="inline">-</code>
 flag: left-justification.</p>
</li>
<li><a name="precision%2c-or-maximum-width"></a><b>precision, or maximum width
</b>
<p>You can specify a precision (for numeric conversions) or a maximum
width (for string conversions) by specifying a <code class="inline">.</code> followed by a number.
For floating point formats, with the exception of &#39;g&#39; and &#39;G&#39;, this specifies
the number of decimal places to show (the default being 6), e.g.:</p>
<pre class="verbatim"><ol><li>  <span class="c"># these examples are subject to system-specific variation</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%f&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>    <span class="c"># prints &quot;&lt;1.000000&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.1f&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>  <span class="c"># prints &quot;&lt;1.0&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.0f&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>  <span class="c"># prints &quot;&lt;1&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%e&gt;&#39;</span><span class="cm">,</span> <span class="n">10</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;1.000000e+01&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.1e&gt;&#39;</span><span class="cm">,</span> <span class="n">10</span><span class="sc">;</span> <span class="c"># prints &quot;&lt;1.0e+01&gt;&quot;</span></li></ol></pre><p>For &#39;g&#39; and &#39;G&#39;, this specifies the maximum number of digits to show,
including prior to the decimal point as well as after it, e.g.:</p>
<pre class="verbatim"><ol><li>  <span class="c"># these examples are subject to system-specific variation</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%g&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>        <span class="c"># prints &quot;&lt;1&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.10g&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>     <span class="c"># prints &quot;&lt;1&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%g&gt;&#39;</span><span class="cm">,</span> <span class="n">100</span><span class="sc">;</span>      <span class="c"># prints &quot;&lt;100&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.1g&gt;&#39;</span><span class="cm">,</span> <span class="n">100</span><span class="sc">;</span>    <span class="c"># prints &quot;&lt;1e+02&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.2g&gt;&#39;</span><span class="cm">,</span> <span class="n">100.01</span><span class="sc">;</span> <span class="c"># prints &quot;&lt;1e+02&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.5g&gt;&#39;</span><span class="cm">,</span> <span class="n">100.01</span><span class="sc">;</span> <span class="c"># prints &quot;&lt;100.01&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.4g&gt;&#39;</span><span class="cm">,</span> <span class="n">100.01</span><span class="sc">;</span> <span class="c"># prints &quot;&lt;100&gt;&quot;</span></li></ol></pre><p>For integer conversions, specifying a precision implies that the
output of the number itself should be zero-padded to this width,
where the 0 flag is ignored:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.6d&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>      <span class="c"># prints &quot;&lt;000001&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%+.6d&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>     <span class="c"># prints &quot;&lt;+000001&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%-10.6d&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;000001    &gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%10.6d&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>    <span class="c"># prints &quot;&lt;    000001&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%010.6d&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;    000001&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%+10.6d&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;   +000001&gt;&quot;</span></li><li></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.6x&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>      <span class="c"># prints &quot;&lt;000001&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%#.6x&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>     <span class="c"># prints &quot;&lt;0x000001&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%-10.6x&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;000001    &gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%10.6x&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>    <span class="c"># prints &quot;&lt;    000001&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%010.6x&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;    000001&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%#10.6x&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;  0x000001&gt;&quot;</span></li></ol></pre><p>For string conversions, specifying a precision truncates the string
to fit in the specified width:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.5s&gt;&#39;</span><span class="cm">,</span> <span class="q">&quot;truncated&quot;</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;trunc&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%10.5s&gt;&#39;</span><span class="cm">,</span> <span class="q">&quot;truncated&quot;</span><span class="sc">;</span> <span class="c"># prints &quot;&lt;     trunc&gt;&quot;</span></li></ol></pre><p>You can also get the precision from the next argument using <code class="inline">.*</code>:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.6x&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>       <span class="c"># prints &quot;&lt;000001&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.*x&gt;&#39;</span><span class="cm">,</span> <span class="n">6</span><span class="cm">,</span> <span class="n">1</span><span class="sc">;</span>    <span class="c"># prints &quot;&lt;000001&gt;&quot;</span></li></ol></pre><p>If a precision obtained through <code class="inline"><span class="i">*</span></code>
 is negative, it has the same
effect as no precision.</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.*s&gt;&#39;</span><span class="cm">,</span>  <span class="n">7</span><span class="cm">,</span> <span class="q">&quot;string&quot;</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;string&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.*s&gt;&#39;</span><span class="cm">,</span>  <span class="n">3</span><span class="cm">,</span> <span class="q">&quot;string&quot;</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;str&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.*s&gt;&#39;</span><span class="cm">,</span>  <span class="n">0</span><span class="cm">,</span> <span class="q">&quot;string&quot;</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.*s&gt;&#39;</span><span class="cm">,</span> <span class="n">-1</span><span class="cm">,</span> <span class="q">&quot;string&quot;</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;string&gt;&quot;</span></li><li></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.*d&gt;&#39;</span><span class="cm">,</span>  <span class="n">1</span><span class="cm">,</span> <span class="n">0</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;0&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.*d&gt;&#39;</span><span class="cm">,</span>  <span class="n">0</span><span class="cm">,</span> <span class="n">0</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;&gt;&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.*d&gt;&#39;</span><span class="cm">,</span> <span class="n">-1</span><span class="cm">,</span> <span class="n">0</span><span class="sc">;</span>   <span class="c"># prints &quot;&lt;0&gt;&quot;</span></li></ol></pre><p>You cannot currently get the precision from a specified number,
but it is intended that this will be possible in the future using
e.g. <code class="inline">.*2$</code>:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%.*2$x&gt;&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="cm">,</span> <span class="n">6</span><span class="sc">;</span>   <span class="c"># INVALID, but in future will print &quot;&lt;000001&gt;&quot;</span></li></ol></pre></li>
<li><a name="size"></a><b>size</b>
<p>For numeric conversions, you can specify the size to interpret the
number as using <code class="inline"><span class="w">l</span></code>
, <code class="inline"><span class="w">h</span></code>
, <code class="inline"><span class="w">V</span></code>
, <code class="inline"><a class="l_k" href="../functions/q.html">q</a></code>, <code class="inline"><span class="w">L</span></code>
, or <code class="inline"><span class="w">ll</span></code>
. For integer
conversions (<code class="inline"><span class="w">d</span> <span class="w">u</span> <span class="w">o</span> <span class="w">x</span> <span class="w">X</span> <span class="w">b</span> <span class="w">i</span> <span class="w">D</span> <span class="w">U</span> <span class="w">O</span></code>
), numbers are usually assumed to be
whatever the default integer size is on your platform (usually 32 or 64
bits), but you can override this to use instead one of the standard C types,
as supported by the compiler used to build Perl:</p>
<pre class="verbatim"><ol><li>   l           interpret integer as C type "long" or "unsigned long"</li><li>   h           interpret integer as C type "short" or "unsigned short"</li><li>   q, L or ll  interpret integer as C type "long long", "unsigned long long".</li><li>               or "quads" (typically 64-bit integers)</li></ol></pre><p>The last will produce errors if Perl does not understand &quot;quads&quot; in your
installation. (This requires that either the platform natively supports quads
or Perl was specifically compiled to support quads.) You can find out
whether your Perl supports quads via <a href="../Config.html">Config</a>:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="../functions/use.html">use</a> <span class="w">Config</span><span class="sc">;</span></li><li>	<span class="s">(</span><span class="i">$Config</span>{<span class="w">use64bitint</span>} eq <span class="q">&#39;define&#39;</span> || <span class="i">$Config</span>{<span class="w">longsize</span>} &gt;= <span class="n">8</span><span class="s">)</span> &amp;&amp;</li><li>		<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;quads\n&quot;</span><span class="sc">;</span></li></ol></pre><p>For floating point conversions (<code class="inline"><span class="w">e</span> <span class="w">f</span> <span class="w">g</span> <span class="w">E</span> <span class="w">F</span> <span class="w">G</span></code>
), numbers are usually assumed
to be the default floating point size on your platform (double or long double),
but you can force &#39;long double&#39; with <code class="inline"><a class="l_k" href="../functions/q.html">q</a></code>, <code class="inline"><span class="w">L</span></code>
, or <code class="inline"><span class="w">ll</span></code>
 if your
platform supports them. You can find out whether your Perl supports long
doubles via <a href="../Config.html">Config</a>:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="../functions/use.html">use</a> <span class="w">Config</span><span class="sc">;</span></li><li>	<span class="i">$Config</span>{<span class="w">d_longdbl</span>} eq <span class="q">&#39;define&#39;</span> &amp;&amp; <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;long doubles\n&quot;</span><span class="sc">;</span></li></ol></pre><p>You can find out whether Perl considers &#39;long double&#39; to be the default
floating point size to use on your platform via <a href="../Config.html">Config</a>:</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="../functions/use.html">use</a> <span class="w">Config</span><span class="sc">;</span></li><li>        <span class="s">(</span><span class="i">$Config</span>{<span class="w">uselongdouble</span>} eq <span class="q">&#39;define&#39;</span><span class="s">)</span> &amp;&amp;</li><li>                <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;long doubles by default\n&quot;</span><span class="sc">;</span></li></ol></pre><p>It can also be the case that long doubles and doubles are the same thing:</p>
<pre class="verbatim"><ol><li>        <a class="l_k" href="../functions/use.html">use</a> <span class="w">Config</span><span class="sc">;</span></li><li>        <span class="s">(</span><span class="i">$Config</span>{<span class="w">doublesize</span>} == <span class="i">$Config</span>{<span class="w">longdblsize</span>}<span class="s">)</span> &amp;&amp;</li><li>                <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;doubles are long doubles\n&quot;</span><span class="sc">;</span></li></ol></pre><p>The size specifier <code class="inline"><span class="w">V</span></code>
 has no effect for Perl code, but it is supported
for compatibility with XS code; it means &#39;use the standard size for
a Perl integer (or floating-point number)&#39;, which is already the
default for Perl code.</p>
</li>
<li><a name="order-of-arguments"></a><b>order of arguments</b>
<p>Normally, sprintf takes the next unused argument as the value to
format for each format specification. If the format specification
uses <code class="inline"><span class="i">*</span></code>
 to require additional arguments, these are consumed from
the argument list in the order in which they appear in the format
specification <i>before</i> the value to format. Where an argument is
specified using an explicit index, this does not affect the normal
order for the arguments (even when the explicitly specified index
would have been the next argument in any case).</p>
<p>So:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&#39;&lt;%*.*s&gt;&#39;</span><span class="cm">,</span> <span class="i">$a</span><span class="cm">,</span> <span class="i">$b</span><span class="cm">,</span> <span class="i">$c</span><span class="sc">;</span></li></ol></pre><p>would use <code class="inline"><span class="i">$a</span></code>
 for the width, <code class="inline"><span class="i">$b</span></code>
 for the precision and <code class="inline"><span class="i">$c</span></code>

as the value to format, while:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">'&lt;%*1$.*s&gt;'</span><span class="cm">,</span> <span class="i">$a</span><span class="cm">,</span> <span class="i">$b</span><span class="sc">;</span></li></ol></pre><p>would use <code class="inline"><span class="i">$a</span></code>
 for the width and the precision, and <code class="inline"><span class="i">$b</span></code>
 as the
value to format.</p>
<p>Here are some more examples - beware that when using an explicit
index, the <code class="inline"><span class="i">$</span></code>
 may need to be escaped:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;%2\$d %d\n&quot;</span><span class="cm">,</span>    <span class="n">12</span><span class="cm">,</span> <span class="n">34</span><span class="sc">;</span>		<span class="c"># will print &quot;34 12\n&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;%2\$d %d %d\n&quot;</span><span class="cm">,</span> <span class="n">12</span><span class="cm">,</span> <span class="n">34</span><span class="sc">;</span>		<span class="c"># will print &quot;34 12 34\n&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;%3\$d %d %d\n&quot;</span><span class="cm">,</span> <span class="n">12</span><span class="cm">,</span> <span class="n">34</span><span class="cm">,</span> <span class="n">56</span><span class="sc">;</span>		<span class="c"># will print &quot;56 12 34\n&quot;</span></li><li>  <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;%2\$*3\$d %d\n&quot;</span><span class="cm">,</span> <span class="n">12</span><span class="cm">,</span> <span class="n">34</span><span class="cm">,</span> <span class="n">3</span><span class="sc">;</span>		<span class="c"># will print &quot; 34 12\n&quot;</span></li></ol><
-----

function sqrt
expr|
Return the square root of EXPR.  If EXPR is omitted, returns square
root of <code class="inline"><span class="i">$_</span></code>
.  Only works on non-negative operands, unless you&#39;ve
loaded the standard Math::Complex module.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Math::Complex</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/sqrt.html">sqrt</a><span class="s">(</span><span class="n">-2</span><span class="s">)</span><span class="sc">;</span>    <span class="c"># prints 1.4142135623731i</span></li></ol><
-----

function sqrt
Return the square root of EXPR.  If EXPR is omitted, returns square
root of <code class="inline"><span class="i">$_</span></code>
.  Only works on non-negative operands, unless you&#39;ve
loaded the standard Math::Complex module.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Math::Complex</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/sqrt.html">sqrt</a><span class="s">(</span><span class="n">-2</span><span class="s">)</span><span class="sc">;</span>    <span class="c"># prints 1.4142135623731i</span></li></ol><
-----

function srand
expr|
Sets the random number seed for the <code class="inline"><a class="l_k" href="../functions/rand.html">rand</a></code> operator.</p>
<p>The point of the function is to &quot;seed&quot; the <code class="inline"><a class="l_k" href="../functions/rand.html">rand</a></code> function so that
<code class="inline"><a class="l_k" href="../functions/rand.html">rand</a></code> can produce a different sequence each time you run your
program.</p>
<p>If srand() is not called explicitly, it is called implicitly at the
first use of the <code class="inline"><a class="l_k" href="../functions/rand.html">rand</a></code> operator.  However, this was not the case in
versions of Perl before 5.004, so if your script will run under older
Perl versions, it should call <code class="inline"><a class="l_k" href="../functions/srand.html">srand</a></code>.</p>
<p>Most programs won&#39;t even call srand() at all, except those that
need a cryptographically-strong starting point rather than the
generally acceptable default, which is based on time of day,
process ID, and memory allocation, or the <i>/dev/urandom</i> device,
if available.</p>
<p>You can call srand($seed) with the same $seed to reproduce the
<i>same</i> sequence from rand(), but this is usually reserved for
generating predictable results for testing or debugging.
Otherwise, don&#39;t call srand() more than once in your program.</p>
<p>Do <b>not</b> call srand() (i.e. without an argument) more than once in
a script.  The internal state of the random number generator should
contain more entropy than can be provided by any seed, so calling
srand() again actually <i>loses</i> randomness.</p>
<p>Most implementations of <code class="inline"><a class="l_k" href="../functions/srand.html">srand</a></code> take an integer and will silently
truncate decimal numbers.  This means <code class="inline"><a class="l_k" href="../functions/srand.html">srand(42)</a></code> will usually
produce the same results as <code class="inline"><a class="l_k" href="../functions/srand.html">srand(42.1)</a></code>.  To be safe, always pass
<code class="inline"><a class="l_k" href="../functions/srand.html">srand</a></code> an integer.</p>
<p>In versions of Perl prior to 5.004 the default seed was just the
current <code class="inline"><a class="l_k" href="../functions/time.html">time</a></code>.  This isn&#39;t a particularly good seed, so many old
programs supply their own seed value (often <code class="inline"><a class="l_k" href="../functions/time.html">time</a> ^ <span class="i">$$</span></code>
 or <code class="inline"><a class="l_k" href="../functions/time.html">time</a> ^
<span class="s">(</span><span class="i">$$</span> + <span class="s">(</span><span class="i">$$</span> &lt;&lt; <span class="n">15</span><span class="s">)</span><span class="s">)</span></code>
), but that isn&#39;t necessary any more.</p>
<p>For cryptographic purposes, however, you need something much more random 
than the default seed.  Checksumming the compressed output of one or more
rapidly changing operating system status programs is the usual method.  For
example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/srand.html">srand</a> <span class="s">(</span><a class="l_k" href="../functions/time.html">time</a> ^ <span class="i">$$</span> ^ <a class="l_k" href="../functions/unpack.html">unpack</a> <span class="q">&quot;%L*&quot;</span><span class="cm">,</span> <span class="q">`ps axww | gzip -f`</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If you&#39;re particularly concerned with this, see the <code class="inline"><span class="w">Math::TrulyRandom</span></code>

module in CPAN.</p>
<p>Frequently called programs (like CGI scripts) that simply use</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/time.html">time</a> ^ <span class="i">$$</span></li></ol></pre><p>for a seed can fall prey to the mathematical property that</p>
<pre class="verbatim"><ol><li>    <span class="w">a</span>^<span class="w">b</span> == <span class="s">(</span><span class="w">a</span>+<span class="n">1</span><span class="s">)</span>^<span class="s">(</span><span class="w">b</span>+<span class="n">1</span><span class="s">)</span></li></ol></pre><p>one-third of the time.  So don&#39;t do that.
-----

function srand
Sets the random number seed for the <code class="inline"><a class="l_k" href="../functions/rand.html">rand</a></code> operator.</p>
<p>The point of the function is to &quot;seed&quot; the <code class="inline"><a class="l_k" href="../functions/rand.html">rand</a></code> function so that
<code class="inline"><a class="l_k" href="../functions/rand.html">rand</a></code> can produce a different sequence each time you run your
program.</p>
<p>If srand() is not called explicitly, it is called implicitly at the
first use of the <code class="inline"><a class="l_k" href="../functions/rand.html">rand</a></code> operator.  However, this was not the case in
versions of Perl before 5.004, so if your script will run under older
Perl versions, it should call <code class="inline"><a class="l_k" href="../functions/srand.html">srand</a></code>.</p>
<p>Most programs won&#39;t even call srand() at all, except those that
need a cryptographically-strong starting point rather than the
generally acceptable default, which is based on time of day,
process ID, and memory allocation, or the <i>/dev/urandom</i> device,
if available.</p>
<p>You can call srand($seed) with the same $seed to reproduce the
<i>same</i> sequence from rand(), but this is usually reserved for
generating predictable results for testing or debugging.
Otherwise, don&#39;t call srand() more than once in your program.</p>
<p>Do <b>not</b> call srand() (i.e. without an argument) more than once in
a script.  The internal state of the random number generator should
contain more entropy than can be provided by any seed, so calling
srand() again actually <i>loses</i> randomness.</p>
<p>Most implementations of <code class="inline"><a class="l_k" href="../functions/srand.html">srand</a></code> take an integer and will silently
truncate decimal numbers.  This means <code class="inline"><a class="l_k" href="../functions/srand.html">srand(42)</a></code> will usually
produce the same results as <code class="inline"><a class="l_k" href="../functions/srand.html">srand(42.1)</a></code>.  To be safe, always pass
<code class="inline"><a class="l_k" href="../functions/srand.html">srand</a></code> an integer.</p>
<p>In versions of Perl prior to 5.004 the default seed was just the
current <code class="inline"><a class="l_k" href="../functions/time.html">time</a></code>.  This isn&#39;t a particularly good seed, so many old
programs supply their own seed value (often <code class="inline"><a class="l_k" href="../functions/time.html">time</a> ^ <span class="i">$$</span></code>
 or <code class="inline"><a class="l_k" href="../functions/time.html">time</a> ^
<span class="s">(</span><span class="i">$$</span> + <span class="s">(</span><span class="i">$$</span> &lt;&lt; <span class="n">15</span><span class="s">)</span><span class="s">)</span></code>
), but that isn&#39;t necessary any more.</p>
<p>For cryptographic purposes, however, you need something much more random 
than the default seed.  Checksumming the compressed output of one or more
rapidly changing operating system status programs is the usual method.  For
example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/srand.html">srand</a> <span class="s">(</span><a class="l_k" href="../functions/time.html">time</a> ^ <span class="i">$$</span> ^ <a class="l_k" href="../functions/unpack.html">unpack</a> <span class="q">&quot;%L*&quot;</span><span class="cm">,</span> <span class="q">`ps axww | gzip -f`</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If you&#39;re particularly concerned with this, see the <code class="inline"><span class="w">Math::TrulyRandom</span></code>

module in CPAN.</p>
<p>Frequently called programs (like CGI scripts) that simply use</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/time.html">time</a> ^ <span class="i">$$</span></li></ol></pre><p>for a seed can fall prey to the mathematical property that</p>
<pre class="verbatim"><ol><li>    <span class="w">a</span>^<span class="w">b</span> == <span class="s">(</span><span class="w">a</span>+<span class="n">1</span><span class="s">)</span>^<span class="s">(</span><span class="w">b</span>+<span class="n">1</span><span class="s">)</span></li></ol></pre><p>one-third of the time.  So don&#39;t do that.
-----

function stat
filehandle|
Returns a 13-element list giving the status info for a file, either
the file opened via FILEHANDLE or DIRHANDLE, or named by EXPR.  If EXPR is 
omitted, it stats <code class="inline"><span class="i">$_</span></code>
.  Returns a null list if the stat fails.  Typically
used as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$dev</span><span class="cm">,</span><span class="i">$ino</span><span class="cm">,</span><span class="i">$mode</span><span class="cm">,</span><span class="i">$nlink</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$rdev</span><span class="cm">,</span><span class="i">$size</span><span class="cm">,</span></li><li>       <span class="i">$atime</span><span class="cm">,</span><span class="i">$mtime</span><span class="cm">,</span><span class="i">$ctime</span><span class="cm">,</span><span class="i">$blksize</span><span class="cm">,</span><span class="i">$blocks</span><span class="s">)</span></li><li>           = <a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Not all fields are supported on all filesystem types.  Here are the
meanings of the fields:</p>
<pre class="verbatim"><ol><li>  0 dev      device number of filesystem</li><li>  1 ino      inode number</li><li>  2 mode     file mode  (type and permissions)</li><li>  3 nlink    number of (hard) links to the file</li><li>  4 uid      numeric user ID of file's owner</li><li>  5 gid      numeric group ID of file's owner</li><li>  6 rdev     the device identifier (special files only)</li><li>  7 size     total size of file, in bytes</li><li>  8 atime    last access time in seconds since the epoch</li><li>  9 mtime    last modify time in seconds since the epoch</li><li> 10 ctime    inode change time in seconds since the epoch (*)</li><li> 11 blksize  preferred block size for file system I/O</li><li> 12 blocks   actual number of blocks allocated</li></ol></pre><p>(The epoch was at 00:00 January 1, 1970 GMT.)</p>
<p>(*) Not all fields are supported on all filesystem types. Notably, the
ctime field is non-portable.  In particular, you cannot expect it to be a
&quot;creation time&quot;, see <a href="../perlport.html#Files-and-Filesystems">"Files and Filesystems" in perlport</a> for details.</p>
<p>If <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code> is passed the special filehandle consisting of an underline, no
stat is done, but the current contents of the stat structure from the
last <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code>, <code class="inline"><a class="l_k" href="../functions/lstat.html">lstat</a></code>, or filetest are returned.  Example:</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span>-x <span class="i">$file</span> &amp;&amp; <span class="s">(</span><span class="s">(</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="w">_</span><span class="s">)</span><span class="s">)</span> &amp;&amp; <span class="i">$d</span> &lt; <span class="n">0</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;$file is executable NFS file\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>(This works on machines only for which the device number is negative
under NFS.)</p>
<p>Because the mode contains both the file type and its permissions, you
should mask off the file type portion and (s)printf using a <code class="inline"><span class="q">&quot;%o&quot;</span></code>

if you want to see the real permissions.</p>
<pre class="verbatim"><ol><li>    <span class="i">$mode</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="s">)</span>[<span class="n">2</span>]<span class="sc">;</span></li><li>    <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;Permissions are %04o\n&quot;</span><span class="cm">,</span> <span class="i">$mode</span> &amp; <span class="n">07777</span><span class="sc">;</span></li></ol></pre><p>In scalar context, <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code> returns a boolean value indicating success
or failure, and, if successful, sets the information associated with
the special filehandle <code class="inline"><span class="w">_</span></code>
.</p>
<p>The <a href="../File/stat.html">File::stat</a> module provides a convenient, by-name access mechanism:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>    <span class="i">$sb</span> = <a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;File is %s, size is %s, perm %04o, mtime %s\n&quot;</span><span class="cm">,</span></li><li>	<span class="i">$filename</span><span class="cm">,</span> <span class="i">$sb</span><span class="i">-&gt;size</span><span class="cm">,</span> <span class="i">$sb</span><span class="i">-&gt;mode</span> &amp; <span class="n">07777</span><span class="cm">,</span></li><li>	<a class="l_k" href="../functions/scalar.html">scalar</a> <a class="l_k" href="../functions/localtime.html">localtime</a> <span class="i">$sb</span><span class="i">-&gt;mtime</span><span class="sc">;</span></li></ol></pre><p>You can import symbolic mode constants (<code class="inline"><span class="w">S_IF</span>*</code>
) and functions
(<code class="inline"><span class="w">S_IS</span>*</code>
) from the Fcntl module:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Fcntl</span> <span class="q">&#39;:mode&#39;</span><span class="sc">;</span></li><li></li><li>    <span class="i">$mode</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="s">)</span>[<span class="n">2</span>]<span class="sc">;</span></li><li></li><li>    <span class="i">$user_rwx</span>      = <span class="s">(</span><span class="i">$mode</span> &amp; <span class="w">S_IRWXU</span><span class="s">)</span> &gt;&gt; <span class="n">6</span><span class="sc">;</span></li><li>    <span class="i">$group_read</span>    = <span class="s">(</span><span class="i">$mode</span> &amp; <span class="w">S_IRGRP</span><span class="s">)</span> &gt;&gt; <span class="n">3</span><span class="sc">;</span></li><li>    <span class="i">$other_execute</span> =  <span class="i">$mode</span> &amp; <span class="w">S_IXOTH</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;Permissions are %04o\n&quot;</span><span class="cm">,</span> <span class="i">S_IMODE</span><span class="s">(</span><span class="i">$mode</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li></li><li>    <span class="i">$is_setuid</span>     =  <span class="i">$mode</span> &amp; <span class="w">S_ISUID</span><span class="sc">;</span></li><li>    <span class="i">$is_directory</span>  =  <span class="i">S_ISDIR</span><span class="s">(</span><span class="i">$mode</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>You could write the last two using the <code class="inline">-u</code>
 and <code class="inline">-d</code>
 operators.
The commonly available <code class="inline"><span class="w">S_IF</span>*</code>
 constants are</p>
<pre class="verbatim"><ol><li>    <span class="c"># Permissions: read, write, execute, for user, group, others.</span></li><li></li><li>    <span class="w">S_IRWXU</span> <span class="w">S_IRUSR</span> <span class="w">S_IWUSR</span> <span class="w">S_IXUSR</span></li><li>    <span class="w">S_IRWXG</span> <span class="w">S_IRGRP</span> <span class="w">S_IWGRP</span> <span class="w">S_IXGRP</span></li><li>    <span class="w">S_IRWXO</span> <span class="w">S_IROTH</span> <span class="w">S_IWOTH</span> <span class="w">S_IXOTH</span></li><li></li><li>    <span class="c"># Setuid/Setgid/Stickiness/SaveText.</span></li><li>    <span class="c"># Note that the exact meaning of these is system dependent.</span></li><li></li><li>    <span class="w">S_ISUID</span> <span class="w">S_ISGID</span> <span class="w">S_ISVTX</span> <span class="w">S_ISTXT</span></li><li></li><li>    <span class="c"># File types.  Not necessarily all are available on your system.</span></li><li></li><li>    <span class="w">S_IFREG</span> <span class="w">S_IFDIR</span> <span class="w">S_IFLNK</span> <span class="w">S_IFBLK</span> <span class="w">S_IFCHR</span> <span class="w">S_IFIFO</span> <span class="w">S_IFSOCK</span> <span class="w">S_IFWHT</span> <span class="w">S_ENFMT</span></li><li></li><li>    <span class="c"># The following are compatibility aliases for S_IRUSR, S_IWUSR, S_IXUSR.</span></li><li></li><li>    <span class="w">S_IREAD</span> <span class="w">S_IWRITE</span> <span class="w">S_IEXEC</span></li></ol></pre><p>and the <code class="inline"><span class="w">S_IF</span>*</code>
 functions are</p>
<pre class="verbatim"><ol><li>    S_IMODE($mode)	the part of $mode containing the permission bits</li><li>			and the setuid/setgid/sticky bits</li><li></li><li>    S_IFMT($mode)	the part of $mode containing the file type</li><li>			which can be bit-anded with e.g. S_IFREG</li><li>                        or with the following functions</li><li></li><li>    # The operators -f, -d, -l, -b, -c, -p, and -S.</li><li></li><li>    S_ISREG($mode) S_ISDIR($mode) S_ISLNK($mode)</li><li>    S_ISBLK($mode) S_ISCHR($mode) S_ISFIFO($mode) S_ISSOCK($mode)</li><li></li><li>    # No direct -X operator counterpart, but for the first one</li><li>    # the -g operator is often equivalent.  The ENFMT stands for</li><li>    # record flocking enforcement, a platform-dependent feature.</li><li></li><li>    S_ISENFMT($mode) S_ISWHT($mode)</li></ol></pre><p>See your native chmod(2) and stat(2) documentation for more details
about the <code class="inline"><span class="w">S_</span>*</code>
 constants.  To get status info for a symbolic link
instead of the target file behind the link, use the <code class="inline"><a class="l_k" href="../functions/lstat.html">lstat</a></code> function.
-----

function stat
expr|
Returns a 13-element list giving the status info for a file, either
the file opened via FILEHANDLE or DIRHANDLE, or named by EXPR.  If EXPR is 
omitted, it stats <code class="inline"><span class="i">$_</span></code>
.  Returns a null list if the stat fails.  Typically
used as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$dev</span><span class="cm">,</span><span class="i">$ino</span><span class="cm">,</span><span class="i">$mode</span><span class="cm">,</span><span class="i">$nlink</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$rdev</span><span class="cm">,</span><span class="i">$size</span><span class="cm">,</span></li><li>       <span class="i">$atime</span><span class="cm">,</span><span class="i">$mtime</span><span class="cm">,</span><span class="i">$ctime</span><span class="cm">,</span><span class="i">$blksize</span><span class="cm">,</span><span class="i">$blocks</span><span class="s">)</span></li><li>           = <a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Not all fields are supported on all filesystem types.  Here are the
meanings of the fields:</p>
<pre class="verbatim"><ol><li>  0 dev      device number of filesystem</li><li>  1 ino      inode number</li><li>  2 mode     file mode  (type and permissions)</li><li>  3 nlink    number of (hard) links to the file</li><li>  4 uid      numeric user ID of file's owner</li><li>  5 gid      numeric group ID of file's owner</li><li>  6 rdev     the device identifier (special files only)</li><li>  7 size     total size of file, in bytes</li><li>  8 atime    last access time in seconds since the epoch</li><li>  9 mtime    last modify time in seconds since the epoch</li><li> 10 ctime    inode change time in seconds since the epoch (*)</li><li> 11 blksize  preferred block size for file system I/O</li><li> 12 blocks   actual number of blocks allocated</li></ol></pre><p>(The epoch was at 00:00 January 1, 1970 GMT.)</p>
<p>(*) Not all fields are supported on all filesystem types. Notably, the
ctime field is non-portable.  In particular, you cannot expect it to be a
&quot;creation time&quot;, see <a href="../perlport.html#Files-and-Filesystems">"Files and Filesystems" in perlport</a> for details.</p>
<p>If <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code> is passed the special filehandle consisting of an underline, no
stat is done, but the current contents of the stat structure from the
last <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code>, <code class="inline"><a class="l_k" href="../functions/lstat.html">lstat</a></code>, or filetest are returned.  Example:</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span>-x <span class="i">$file</span> &amp;&amp; <span class="s">(</span><span class="s">(</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="w">_</span><span class="s">)</span><span class="s">)</span> &amp;&amp; <span class="i">$d</span> &lt; <span class="n">0</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;$file is executable NFS file\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>(This works on machines only for which the device number is negative
under NFS.)</p>
<p>Because the mode contains both the file type and its permissions, you
should mask off the file type portion and (s)printf using a <code class="inline"><span class="q">&quot;%o&quot;</span></code>

if you want to see the real permissions.</p>
<pre class="verbatim"><ol><li>    <span class="i">$mode</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="s">)</span>[<span class="n">2</span>]<span class="sc">;</span></li><li>    <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;Permissions are %04o\n&quot;</span><span class="cm">,</span> <span class="i">$mode</span> &amp; <span class="n">07777</span><span class="sc">;</span></li></ol></pre><p>In scalar context, <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code> returns a boolean value indicating success
or failure, and, if successful, sets the information associated with
the special filehandle <code class="inline"><span class="w">_</span></code>
.</p>
<p>The <a href="../File/stat.html">File::stat</a> module provides a convenient, by-name access mechanism:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>    <span class="i">$sb</span> = <a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;File is %s, size is %s, perm %04o, mtime %s\n&quot;</span><span class="cm">,</span></li><li>	<span class="i">$filename</span><span class="cm">,</span> <span class="i">$sb</span><span class="i">-&gt;size</span><span class="cm">,</span> <span class="i">$sb</span><span class="i">-&gt;mode</span> &amp; <span class="n">07777</span><span class="cm">,</span></li><li>	<a class="l_k" href="../functions/scalar.html">scalar</a> <a class="l_k" href="../functions/localtime.html">localtime</a> <span class="i">$sb</span><span class="i">-&gt;mtime</span><span class="sc">;</span></li></ol></pre><p>You can import symbolic mode constants (<code class="inline"><span class="w">S_IF</span>*</code>
) and functions
(<code class="inline"><span class="w">S_IS</span>*</code>
) from the Fcntl module:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Fcntl</span> <span class="q">&#39;:mode&#39;</span><span class="sc">;</span></li><li></li><li>    <span class="i">$mode</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="s">)</span>[<span class="n">2</span>]<span class="sc">;</span></li><li></li><li>    <span class="i">$user_rwx</span>      = <span class="s">(</span><span class="i">$mode</span> &amp; <span class="w">S_IRWXU</span><span class="s">)</span> &gt;&gt; <span class="n">6</span><span class="sc">;</span></li><li>    <span class="i">$group_read</span>    = <span class="s">(</span><span class="i">$mode</span> &amp; <span class="w">S_IRGRP</span><span class="s">)</span> &gt;&gt; <span class="n">3</span><span class="sc">;</span></li><li>    <span class="i">$other_execute</span> =  <span class="i">$mode</span> &amp; <span class="w">S_IXOTH</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;Permissions are %04o\n&quot;</span><span class="cm">,</span> <span class="i">S_IMODE</span><span class="s">(</span><span class="i">$mode</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li></li><li>    <span class="i">$is_setuid</span>     =  <span class="i">$mode</span> &amp; <span class="w">S_ISUID</span><span class="sc">;</span></li><li>    <span class="i">$is_directory</span>  =  <span class="i">S_ISDIR</span><span class="s">(</span><span class="i">$mode</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>You could write the last two using the <code class="inline">-u</code>
 and <code class="inline">-d</code>
 operators.
The commonly available <code class="inline"><span class="w">S_IF</span>*</code>
 constants are</p>
<pre class="verbatim"><ol><li>    <span class="c"># Permissions: read, write, execute, for user, group, others.</span></li><li></li><li>    <span class="w">S_IRWXU</span> <span class="w">S_IRUSR</span> <span class="w">S_IWUSR</span> <span class="w">S_IXUSR</span></li><li>    <span class="w">S_IRWXG</span> <span class="w">S_IRGRP</span> <span class="w">S_IWGRP</span> <span class="w">S_IXGRP</span></li><li>    <span class="w">S_IRWXO</span> <span class="w">S_IROTH</span> <span class="w">S_IWOTH</span> <span class="w">S_IXOTH</span></li><li></li><li>    <span class="c"># Setuid/Setgid/Stickiness/SaveText.</span></li><li>    <span class="c"># Note that the exact meaning of these is system dependent.</span></li><li></li><li>    <span class="w">S_ISUID</span> <span class="w">S_ISGID</span> <span class="w">S_ISVTX</span> <span class="w">S_ISTXT</span></li><li></li><li>    <span class="c"># File types.  Not necessarily all are available on your system.</span></li><li></li><li>    <span class="w">S_IFREG</span> <span class="w">S_IFDIR</span> <span class="w">S_IFLNK</span> <span class="w">S_IFBLK</span> <span class="w">S_IFCHR</span> <span class="w">S_IFIFO</span> <span class="w">S_IFSOCK</span> <span class="w">S_IFWHT</span> <span class="w">S_ENFMT</span></li><li></li><li>    <span class="c"># The following are compatibility aliases for S_IRUSR, S_IWUSR, S_IXUSR.</span></li><li></li><li>    <span class="w">S_IREAD</span> <span class="w">S_IWRITE</span> <span class="w">S_IEXEC</span></li></ol></pre><p>and the <code class="inline"><span class="w">S_IF</span>*</code>
 functions are</p>
<pre class="verbatim"><ol><li>    S_IMODE($mode)	the part of $mode containing the permission bits</li><li>			and the setuid/setgid/sticky bits</li><li></li><li>    S_IFMT($mode)	the part of $mode containing the file type</li><li>			which can be bit-anded with e.g. S_IFREG</li><li>                        or with the following functions</li><li></li><li>    # The operators -f, -d, -l, -b, -c, -p, and -S.</li><li></li><li>    S_ISREG($mode) S_ISDIR($mode) S_ISLNK($mode)</li><li>    S_ISBLK($mode) S_ISCHR($mode) S_ISFIFO($mode) S_ISSOCK($mode)</li><li></li><li>    # No direct -X operator counterpart, but for the first one</li><li>    # the -g operator is often equivalent.  The ENFMT stands for</li><li>    # record flocking enforcement, a platform-dependent feature.</li><li></li><li>    S_ISENFMT($mode) S_ISWHT($mode)</li></ol></pre><p>See your native chmod(2) and stat(2) documentation for more details
about the <code class="inline"><span class="w">S_</span>*</code>
 constants.  To get status info for a symbolic link
instead of the target file behind the link, use the <code class="inline"><a class="l_k" href="../functions/lstat.html">lstat</a></code> function.
-----

function stat
dirhandle|
Returns a 13-element list giving the status info for a file, either
the file opened via FILEHANDLE or DIRHANDLE, or named by EXPR.  If EXPR is 
omitted, it stats <code class="inline"><span class="i">$_</span></code>
.  Returns a null list if the stat fails.  Typically
used as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$dev</span><span class="cm">,</span><span class="i">$ino</span><span class="cm">,</span><span class="i">$mode</span><span class="cm">,</span><span class="i">$nlink</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$rdev</span><span class="cm">,</span><span class="i">$size</span><span class="cm">,</span></li><li>       <span class="i">$atime</span><span class="cm">,</span><span class="i">$mtime</span><span class="cm">,</span><span class="i">$ctime</span><span class="cm">,</span><span class="i">$blksize</span><span class="cm">,</span><span class="i">$blocks</span><span class="s">)</span></li><li>           = <a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Not all fields are supported on all filesystem types.  Here are the
meanings of the fields:</p>
<pre class="verbatim"><ol><li>  0 dev      device number of filesystem</li><li>  1 ino      inode number</li><li>  2 mode     file mode  (type and permissions)</li><li>  3 nlink    number of (hard) links to the file</li><li>  4 uid      numeric user ID of file's owner</li><li>  5 gid      numeric group ID of file's owner</li><li>  6 rdev     the device identifier (special files only)</li><li>  7 size     total size of file, in bytes</li><li>  8 atime    last access time in seconds since the epoch</li><li>  9 mtime    last modify time in seconds since the epoch</li><li> 10 ctime    inode change time in seconds since the epoch (*)</li><li> 11 blksize  preferred block size for file system I/O</li><li> 12 blocks   actual number of blocks allocated</li></ol></pre><p>(The epoch was at 00:00 January 1, 1970 GMT.)</p>
<p>(*) Not all fields are supported on all filesystem types. Notably, the
ctime field is non-portable.  In particular, you cannot expect it to be a
&quot;creation time&quot;, see <a href="../perlport.html#Files-and-Filesystems">"Files and Filesystems" in perlport</a> for details.</p>
<p>If <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code> is passed the special filehandle consisting of an underline, no
stat is done, but the current contents of the stat structure from the
last <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code>, <code class="inline"><a class="l_k" href="../functions/lstat.html">lstat</a></code>, or filetest are returned.  Example:</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span>-x <span class="i">$file</span> &amp;&amp; <span class="s">(</span><span class="s">(</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="w">_</span><span class="s">)</span><span class="s">)</span> &amp;&amp; <span class="i">$d</span> &lt; <span class="n">0</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;$file is executable NFS file\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>(This works on machines only for which the device number is negative
under NFS.)</p>
<p>Because the mode contains both the file type and its permissions, you
should mask off the file type portion and (s)printf using a <code class="inline"><span class="q">&quot;%o&quot;</span></code>

if you want to see the real permissions.</p>
<pre class="verbatim"><ol><li>    <span class="i">$mode</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="s">)</span>[<span class="n">2</span>]<span class="sc">;</span></li><li>    <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;Permissions are %04o\n&quot;</span><span class="cm">,</span> <span class="i">$mode</span> &amp; <span class="n">07777</span><span class="sc">;</span></li></ol></pre><p>In scalar context, <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code> returns a boolean value indicating success
or failure, and, if successful, sets the information associated with
the special filehandle <code class="inline"><span class="w">_</span></code>
.</p>
<p>The <a href="../File/stat.html">File::stat</a> module provides a convenient, by-name access mechanism:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>    <span class="i">$sb</span> = <a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;File is %s, size is %s, perm %04o, mtime %s\n&quot;</span><span class="cm">,</span></li><li>	<span class="i">$filename</span><span class="cm">,</span> <span class="i">$sb</span><span class="i">-&gt;size</span><span class="cm">,</span> <span class="i">$sb</span><span class="i">-&gt;mode</span> &amp; <span class="n">07777</span><span class="cm">,</span></li><li>	<a class="l_k" href="../functions/scalar.html">scalar</a> <a class="l_k" href="../functions/localtime.html">localtime</a> <span class="i">$sb</span><span class="i">-&gt;mtime</span><span class="sc">;</span></li></ol></pre><p>You can import symbolic mode constants (<code class="inline"><span class="w">S_IF</span>*</code>
) and functions
(<code class="inline"><span class="w">S_IS</span>*</code>
) from the Fcntl module:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Fcntl</span> <span class="q">&#39;:mode&#39;</span><span class="sc">;</span></li><li></li><li>    <span class="i">$mode</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="s">)</span>[<span class="n">2</span>]<span class="sc">;</span></li><li></li><li>    <span class="i">$user_rwx</span>      = <span class="s">(</span><span class="i">$mode</span> &amp; <span class="w">S_IRWXU</span><span class="s">)</span> &gt;&gt; <span class="n">6</span><span class="sc">;</span></li><li>    <span class="i">$group_read</span>    = <span class="s">(</span><span class="i">$mode</span> &amp; <span class="w">S_IRGRP</span><span class="s">)</span> &gt;&gt; <span class="n">3</span><span class="sc">;</span></li><li>    <span class="i">$other_execute</span> =  <span class="i">$mode</span> &amp; <span class="w">S_IXOTH</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;Permissions are %04o\n&quot;</span><span class="cm">,</span> <span class="i">S_IMODE</span><span class="s">(</span><span class="i">$mode</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li></li><li>    <span class="i">$is_setuid</span>     =  <span class="i">$mode</span> &amp; <span class="w">S_ISUID</span><span class="sc">;</span></li><li>    <span class="i">$is_directory</span>  =  <span class="i">S_ISDIR</span><span class="s">(</span><span class="i">$mode</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>You could write the last two using the <code class="inline">-u</code>
 and <code class="inline">-d</code>
 operators.
The commonly available <code class="inline"><span class="w">S_IF</span>*</code>
 constants are</p>
<pre class="verbatim"><ol><li>    <span class="c"># Permissions: read, write, execute, for user, group, others.</span></li><li></li><li>    <span class="w">S_IRWXU</span> <span class="w">S_IRUSR</span> <span class="w">S_IWUSR</span> <span class="w">S_IXUSR</span></li><li>    <span class="w">S_IRWXG</span> <span class="w">S_IRGRP</span> <span class="w">S_IWGRP</span> <span class="w">S_IXGRP</span></li><li>    <span class="w">S_IRWXO</span> <span class="w">S_IROTH</span> <span class="w">S_IWOTH</span> <span class="w">S_IXOTH</span></li><li></li><li>    <span class="c"># Setuid/Setgid/Stickiness/SaveText.</span></li><li>    <span class="c"># Note that the exact meaning of these is system dependent.</span></li><li></li><li>    <span class="w">S_ISUID</span> <span class="w">S_ISGID</span> <span class="w">S_ISVTX</span> <span class="w">S_ISTXT</span></li><li></li><li>    <span class="c"># File types.  Not necessarily all are available on your system.</span></li><li></li><li>    <span class="w">S_IFREG</span> <span class="w">S_IFDIR</span> <span class="w">S_IFLNK</span> <span class="w">S_IFBLK</span> <span class="w">S_IFCHR</span> <span class="w">S_IFIFO</span> <span class="w">S_IFSOCK</span> <span class="w">S_IFWHT</span> <span class="w">S_ENFMT</span></li><li></li><li>    <span class="c"># The following are compatibility aliases for S_IRUSR, S_IWUSR, S_IXUSR.</span></li><li></li><li>    <span class="w">S_IREAD</span> <span class="w">S_IWRITE</span> <span class="w">S_IEXEC</span></li></ol></pre><p>and the <code class="inline"><span class="w">S_IF</span>*</code>
 functions are</p>
<pre class="verbatim"><ol><li>    S_IMODE($mode)	the part of $mode containing the permission bits</li><li>			and the setuid/setgid/sticky bits</li><li></li><li>    S_IFMT($mode)	the part of $mode containing the file type</li><li>			which can be bit-anded with e.g. S_IFREG</li><li>                        or with the following functions</li><li></li><li>    # The operators -f, -d, -l, -b, -c, -p, and -S.</li><li></li><li>    S_ISREG($mode) S_ISDIR($mode) S_ISLNK($mode)</li><li>    S_ISBLK($mode) S_ISCHR($mode) S_ISFIFO($mode) S_ISSOCK($mode)</li><li></li><li>    # No direct -X operator counterpart, but for the first one</li><li>    # the -g operator is often equivalent.  The ENFMT stands for</li><li>    # record flocking enforcement, a platform-dependent feature.</li><li></li><li>    S_ISENFMT($mode) S_ISWHT($mode)</li></ol></pre><p>See your native chmod(2) and stat(2) documentation for more details
about the <code class="inline"><span class="w">S_</span>*</code>
 constants.  To get status info for a symbolic link
instead of the target file behind the link, use the <code class="inline"><a class="l_k" href="../functions/lstat.html">lstat</a></code> function.
-----

function stat
Returns a 13-element list giving the status info for a file, either
the file opened via FILEHANDLE or DIRHANDLE, or named by EXPR.  If EXPR is 
omitted, it stats <code class="inline"><span class="i">$_</span></code>
.  Returns a null list if the stat fails.  Typically
used as follows:</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$dev</span><span class="cm">,</span><span class="i">$ino</span><span class="cm">,</span><span class="i">$mode</span><span class="cm">,</span><span class="i">$nlink</span><span class="cm">,</span><span class="i">$uid</span><span class="cm">,</span><span class="i">$gid</span><span class="cm">,</span><span class="i">$rdev</span><span class="cm">,</span><span class="i">$size</span><span class="cm">,</span></li><li>       <span class="i">$atime</span><span class="cm">,</span><span class="i">$mtime</span><span class="cm">,</span><span class="i">$ctime</span><span class="cm">,</span><span class="i">$blksize</span><span class="cm">,</span><span class="i">$blocks</span><span class="s">)</span></li><li>           = <a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Not all fields are supported on all filesystem types.  Here are the
meanings of the fields:</p>
<pre class="verbatim"><ol><li>  0 dev      device number of filesystem</li><li>  1 ino      inode number</li><li>  2 mode     file mode  (type and permissions)</li><li>  3 nlink    number of (hard) links to the file</li><li>  4 uid      numeric user ID of file's owner</li><li>  5 gid      numeric group ID of file's owner</li><li>  6 rdev     the device identifier (special files only)</li><li>  7 size     total size of file, in bytes</li><li>  8 atime    last access time in seconds since the epoch</li><li>  9 mtime    last modify time in seconds since the epoch</li><li> 10 ctime    inode change time in seconds since the epoch (*)</li><li> 11 blksize  preferred block size for file system I/O</li><li> 12 blocks   actual number of blocks allocated</li></ol></pre><p>(The epoch was at 00:00 January 1, 1970 GMT.)</p>
<p>(*) Not all fields are supported on all filesystem types. Notably, the
ctime field is non-portable.  In particular, you cannot expect it to be a
&quot;creation time&quot;, see <a href="../perlport.html#Files-and-Filesystems">"Files and Filesystems" in perlport</a> for details.</p>
<p>If <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code> is passed the special filehandle consisting of an underline, no
stat is done, but the current contents of the stat structure from the
last <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code>, <code class="inline"><a class="l_k" href="../functions/lstat.html">lstat</a></code>, or filetest are returned.  Example:</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span>-x <span class="i">$file</span> &amp;&amp; <span class="s">(</span><span class="s">(</span><span class="i">$d</span><span class="s">)</span> = <a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="w">_</span><span class="s">)</span><span class="s">)</span> &amp;&amp; <span class="i">$d</span> &lt; <span class="n">0</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;$file is executable NFS file\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>(This works on machines only for which the device number is negative
under NFS.)</p>
<p>Because the mode contains both the file type and its permissions, you
should mask off the file type portion and (s)printf using a <code class="inline"><span class="q">&quot;%o&quot;</span></code>

if you want to see the real permissions.</p>
<pre class="verbatim"><ol><li>    <span class="i">$mode</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="s">)</span>[<span class="n">2</span>]<span class="sc">;</span></li><li>    <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;Permissions are %04o\n&quot;</span><span class="cm">,</span> <span class="i">$mode</span> &amp; <span class="n">07777</span><span class="sc">;</span></li></ol></pre><p>In scalar context, <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code> returns a boolean value indicating success
or failure, and, if successful, sets the information associated with
the special filehandle <code class="inline"><span class="w">_</span></code>
.</p>
<p>The <a href="../File/stat.html">File::stat</a> module provides a convenient, by-name access mechanism:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">File::stat</span><span class="sc">;</span></li><li>    <span class="i">$sb</span> = <a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;File is %s, size is %s, perm %04o, mtime %s\n&quot;</span><span class="cm">,</span></li><li>	<span class="i">$filename</span><span class="cm">,</span> <span class="i">$sb</span><span class="i">-&gt;size</span><span class="cm">,</span> <span class="i">$sb</span><span class="i">-&gt;mode</span> &amp; <span class="n">07777</span><span class="cm">,</span></li><li>	<a class="l_k" href="../functions/scalar.html">scalar</a> <a class="l_k" href="../functions/localtime.html">localtime</a> <span class="i">$sb</span><span class="i">-&gt;mtime</span><span class="sc">;</span></li></ol></pre><p>You can import symbolic mode constants (<code class="inline"><span class="w">S_IF</span>*</code>
) and functions
(<code class="inline"><span class="w">S_IS</span>*</code>
) from the Fcntl module:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Fcntl</span> <span class="q">&#39;:mode&#39;</span><span class="sc">;</span></li><li></li><li>    <span class="i">$mode</span> = <span class="s">(</span><a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="s">)</span>[<span class="n">2</span>]<span class="sc">;</span></li><li></li><li>    <span class="i">$user_rwx</span>      = <span class="s">(</span><span class="i">$mode</span> &amp; <span class="w">S_IRWXU</span><span class="s">)</span> &gt;&gt; <span class="n">6</span><span class="sc">;</span></li><li>    <span class="i">$group_read</span>    = <span class="s">(</span><span class="i">$mode</span> &amp; <span class="w">S_IRGRP</span><span class="s">)</span> &gt;&gt; <span class="n">3</span><span class="sc">;</span></li><li>    <span class="i">$other_execute</span> =  <span class="i">$mode</span> &amp; <span class="w">S_IXOTH</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;Permissions are %04o\n&quot;</span><span class="cm">,</span> <span class="i">S_IMODE</span><span class="s">(</span><span class="i">$mode</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li></li><li>    <span class="i">$is_setuid</span>     =  <span class="i">$mode</span> &amp; <span class="w">S_ISUID</span><span class="sc">;</span></li><li>    <span class="i">$is_directory</span>  =  <span class="i">S_ISDIR</span><span class="s">(</span><span class="i">$mode</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>You could write the last two using the <code class="inline">-u</code>
 and <code class="inline">-d</code>
 operators.
The commonly available <code class="inline"><span class="w">S_IF</span>*</code>
 constants are</p>
<pre class="verbatim"><ol><li>    <span class="c"># Permissions: read, write, execute, for user, group, others.</span></li><li></li><li>    <span class="w">S_IRWXU</span> <span class="w">S_IRUSR</span> <span class="w">S_IWUSR</span> <span class="w">S_IXUSR</span></li><li>    <span class="w">S_IRWXG</span> <span class="w">S_IRGRP</span> <span class="w">S_IWGRP</span> <span class="w">S_IXGRP</span></li><li>    <span class="w">S_IRWXO</span> <span class="w">S_IROTH</span> <span class="w">S_IWOTH</span> <span class="w">S_IXOTH</span></li><li></li><li>    <span class="c"># Setuid/Setgid/Stickiness/SaveText.</span></li><li>    <span class="c"># Note that the exact meaning of these is system dependent.</span></li><li></li><li>    <span class="w">S_ISUID</span> <span class="w">S_ISGID</span> <span class="w">S_ISVTX</span> <span class="w">S_ISTXT</span></li><li></li><li>    <span class="c"># File types.  Not necessarily all are available on your system.</span></li><li></li><li>    <span class="w">S_IFREG</span> <span class="w">S_IFDIR</span> <span class="w">S_IFLNK</span> <span class="w">S_IFBLK</span> <span class="w">S_IFCHR</span> <span class="w">S_IFIFO</span> <span class="w">S_IFSOCK</span> <span class="w">S_IFWHT</span> <span class="w">S_ENFMT</span></li><li></li><li>    <span class="c"># The following are compatibility aliases for S_IRUSR, S_IWUSR, S_IXUSR.</span></li><li></li><li>    <span class="w">S_IREAD</span> <span class="w">S_IWRITE</span> <span class="w">S_IEXEC</span></li></ol></pre><p>and the <code class="inline"><span class="w">S_IF</span>*</code>
 functions are</p>
<pre class="verbatim"><ol><li>    S_IMODE($mode)	the part of $mode containing the permission bits</li><li>			and the setuid/setgid/sticky bits</li><li></li><li>    S_IFMT($mode)	the part of $mode containing the file type</li><li>			which can be bit-anded with e.g. S_IFREG</li><li>                        or with the following functions</li><li></li><li>    # The operators -f, -d, -l, -b, -c, -p, and -S.</li><li></li><li>    S_ISREG($mode) S_ISDIR($mode) S_ISLNK($mode)</li><li>    S_ISBLK($mode) S_ISCHR($mode) S_ISFIFO($mode) S_ISSOCK($mode)</li><li></li><li>    # No direct -X operator counterpart, but for the first one</li><li>    # the -g operator is often equivalent.  The ENFMT stands for</li><li>    # record flocking enforcement, a platform-dependent feature.</li><li></li><li>    S_ISENFMT($mode) S_ISWHT($mode)</li></ol></pre><p>See your native chmod(2) and stat(2) documentation for more details
about the <code class="inline"><span class="w">S_</span>*</code>
 constants.  To get status info for a symbolic link
instead of the target file behind the link, use the <code class="inline"><a class="l_k" href="../functions/lstat.html">lstat</a></code> function.
-----

function state
expr|
<code class="inline"><a class="l_k" href="../functions/state.html">state</a></code> declares a lexically scoped variable, just like <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> does.
However, those variables will never be reinitialized, contrary to
lexical variables that are reinitialized each time their enclosing block
is entered.</p>
<p><code class="inline"><a class="l_k" href="../functions/state.html">state</a></code> variables are only enabled when the <code class="inline"><span class="w">feature</span> <span class="q">'state'</span></code>
 pragma is
in effect.  See <a href="../feature.html">feature</a>.
-----

function state
type expr|
<code class="inline"><a class="l_k" href="../functions/state.html">state</a></code> declares a lexically scoped variable, just like <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> does.
However, those variables will never be reinitialized, contrary to
lexical variables that are reinitialized each time their enclosing block
is entered.</p>
<p><code class="inline"><a class="l_k" href="../functions/state.html">state</a></code> variables are only enabled when the <code class="inline"><span class="w">feature</span> <span class="q">'state'</span></code>
 pragma is
in effect.  See <a href="../feature.html">feature</a>.
-----

function state
expr : attrs|
<code class="inline"><a class="l_k" href="../functions/state.html">state</a></code> declares a lexically scoped variable, just like <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> does.
However, those variables will never be reinitialized, contrary to
lexical variables that are reinitialized each time their enclosing block
is entered.</p>
<p><code class="inline"><a class="l_k" href="../functions/state.html">state</a></code> variables are only enabled when the <code class="inline"><span class="w">feature</span> <span class="q">'state'</span></code>
 pragma is
in effect.  See <a href="../feature.html">feature</a>.
-----

function state
type expr : attrs|
<code class="inline"><a class="l_k" href="../functions/state.html">state</a></code> declares a lexically scoped variable, just like <code class="inline"><a class="l_k" href="../functions/my.html">my</a></code> does.
However, those variables will never be reinitialized, contrary to
lexical variables that are reinitialized each time their enclosing block
is entered.</p>
<p><code class="inline"><a class="l_k" href="../functions/state.html">state</a></code> variables are only enabled when the <code class="inline"><span class="w">feature</span> <span class="q">'state'</span></code>
 pragma is
in effect.  See <a href="../feature.html">feature</a>.
-----

function study
scalar|
Takes extra time to study SCALAR (<code class="inline"><span class="i">$_</span></code>
 if unspecified) in anticipation of
doing many pattern matches on the string before it is next modified.
This may or may not save time, depending on the nature and number of
patterns you are searching on, and on the distribution of character
frequencies in the string to be searched--you probably want to compare
run times with and without it to see which runs faster.  Those loops
that scan for many short constant strings (including the constant
parts of more complex patterns) will benefit most.  You may have only
one <code class="inline"><a class="l_k" href="../functions/study.html">study</a></code> active at a time--if you study a different scalar the first
is &quot;unstudied&quot;.  (The way <code class="inline"><a class="l_k" href="../functions/study.html">study</a></code> works is this: a linked list of every
character in the string to be searched is made, so we know, for
example, where all the <code class="inline"><span class="q">&#39;k&#39;</span></code>
 characters are.  From each search string,
the rarest character is selected, based on some static frequency tables
constructed from some C programs and English text.  Only those places
that contain this &quot;rarest&quot; character are examined.)</p>
<p>For example, here is a loop that inserts index producing entries
before any line containing a certain pattern:</p>
<pre class="verbatim"><ol><li>    while <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/study.html">study</a><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;.IX foo\n&quot;</span> 	if <span class="q">/\bfoo\b/</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;.IX bar\n&quot;</span> 	if <span class="q">/\bbar\b/</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;.IX blurfl\n&quot;</span> 	if <span class="q">/\bblurfl\b/</span><span class="sc">;</span></li><li>	<span class="c"># ...</span></li><li>	<a class="l_k" href="../functions/print.html">print</a><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>In searching for <code class="inline"><span class="q">/\bfoo\b/</span></code>
, only those locations in <code class="inline"><span class="i">$_</span></code>
 that contain <code class="inline"><span class="w">f</span></code>

will be looked at, because <code class="inline"><span class="w">f</span></code>
 is rarer than <code class="inline"><span class="w">o</span></code>
.  In general, this is
a big win except in pathological cases.  The only question is whether
it saves you more time than it took to build the linked list in the
first place.</p>
<p>Note that if you have to look for strings that you don&#39;t know till
runtime, you can build an entire loop as a string and <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code> that to
avoid recompiling all your patterns all the time.  Together with
undefining <code class="inline"><span class="i">$/</span></code>
 to input entire files as one record, this can be very
fast, often faster than specialized programs like fgrep(1).  The following
scans a list of files (<code class="inline"><span class="i">@files</span></code>
) for a list of words (<code class="inline"><span class="i">@words</span></code>
), and prints
out the names of those files that contain a match:</p>
<pre class="verbatim"><ol><li>    <span class="i">$search</span> = <span class="q">&#39;while (&lt;&gt;) { study;&#39;</span><span class="sc">;</span></li><li>    foreach <span class="i">$word</span> <span class="s">(</span><span class="i">@words</span><span class="s">)</span> <span class="s">{</span></li><li>	<span class="i">$search</span> .= <span class="q">&quot;++\$seen{\$ARGV} if /\\b$word\\b/;\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    <span class="i">$search</span> .= <span class="q">&quot;}&quot;</span><span class="sc">;</span></li><li>    <span class="i">@ARGV</span> = <span class="i">@files</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">$/</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/eval.html">eval</a> <span class="i">$search</span><span class="sc">;</span>		<span class="c"># this screams</span></li><li>    <span class="i">$/</span> = <span class="q">&quot;\n&quot;</span><span class="sc">;</span>		<span class="c"># put back to normal input delimiter</span></li><li>    foreach <span class="i">$file</span> <span class="s">(</span><a class="l_k" href="../functions/sort.html">sort</a> <a class="l_k" href="../functions/keys.html">keys</a><span class="s">(</span><span class="i">%seen</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="i">$file</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol><
-----

function study
Takes extra time to study SCALAR (<code class="inline"><span class="i">$_</span></code>
 if unspecified) in anticipation of
doing many pattern matches on the string before it is next modified.
This may or may not save time, depending on the nature and number of
patterns you are searching on, and on the distribution of character
frequencies in the string to be searched--you probably want to compare
run times with and without it to see which runs faster.  Those loops
that scan for many short constant strings (including the constant
parts of more complex patterns) will benefit most.  You may have only
one <code class="inline"><a class="l_k" href="../functions/study.html">study</a></code> active at a time--if you study a different scalar the first
is &quot;unstudied&quot;.  (The way <code class="inline"><a class="l_k" href="../functions/study.html">study</a></code> works is this: a linked list of every
character in the string to be searched is made, so we know, for
example, where all the <code class="inline"><span class="q">&#39;k&#39;</span></code>
 characters are.  From each search string,
the rarest character is selected, based on some static frequency tables
constructed from some C programs and English text.  Only those places
that contain this &quot;rarest&quot; character are examined.)</p>
<p>For example, here is a loop that inserts index producing entries
before any line containing a certain pattern:</p>
<pre class="verbatim"><ol><li>    while <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/study.html">study</a><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;.IX foo\n&quot;</span> 	if <span class="q">/\bfoo\b/</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;.IX bar\n&quot;</span> 	if <span class="q">/\bbar\b/</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;.IX blurfl\n&quot;</span> 	if <span class="q">/\bblurfl\b/</span><span class="sc">;</span></li><li>	<span class="c"># ...</span></li><li>	<a class="l_k" href="../functions/print.html">print</a><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>In searching for <code class="inline"><span class="q">/\bfoo\b/</span></code>
, only those locations in <code class="inline"><span class="i">$_</span></code>
 that contain <code class="inline"><span class="w">f</span></code>

will be looked at, because <code class="inline"><span class="w">f</span></code>
 is rarer than <code class="inline"><span class="w">o</span></code>
.  In general, this is
a big win except in pathological cases.  The only question is whether
it saves you more time than it took to build the linked list in the
first place.</p>
<p>Note that if you have to look for strings that you don&#39;t know till
runtime, you can build an entire loop as a string and <code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code> that to
avoid recompiling all your patterns all the time.  Together with
undefining <code class="inline"><span class="i">$/</span></code>
 to input entire files as one record, this can be very
fast, often faster than specialized programs like fgrep(1).  The following
scans a list of files (<code class="inline"><span class="i">@files</span></code>
) for a list of words (<code class="inline"><span class="i">@words</span></code>
), and prints
out the names of those files that contain a match:</p>
<pre class="verbatim"><ol><li>    <span class="i">$search</span> = <span class="q">&#39;while (&lt;&gt;) { study;&#39;</span><span class="sc">;</span></li><li>    foreach <span class="i">$word</span> <span class="s">(</span><span class="i">@words</span><span class="s">)</span> <span class="s">{</span></li><li>	<span class="i">$search</span> .= <span class="q">&quot;++\$seen{\$ARGV} if /\\b$word\\b/;\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    <span class="i">$search</span> .= <span class="q">&quot;}&quot;</span><span class="sc">;</span></li><li>    <span class="i">@ARGV</span> = <span class="i">@files</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">$/</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/eval.html">eval</a> <span class="i">$search</span><span class="sc">;</span>		<span class="c"># this screams</span></li><li>    <span class="i">$/</span> = <span class="q">&quot;\n&quot;</span><span class="sc">;</span>		<span class="c"># put back to normal input delimiter</span></li><li>    foreach <span class="i">$file</span> <span class="s">(</span><a class="l_k" href="../functions/sort.html">sort</a> <a class="l_k" href="../functions/keys.html">keys</a><span class="s">(</span><span class="i">%seen</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="i">$file</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol><
-----

function sub
name block|
This is subroutine definition, not a real function <i>per se</i>.
Without a BLOCK it&#39;s just a forward declaration.  Without a NAME,
it&#39;s an anonymous function declaration, and does actually return
a value: the CODE ref of the closure you just created.</p>
<p>See <a href="../perlsub.html">perlsub</a> and <a href="../perlref.html">perlref</a> for details about subroutines and
references, and <a href="../attributes.html">attributes</a> and <a href="../Attribute/Handlers.html">Attribute::Handlers</a> for more
information about attributes.
-----

function sub
name (proto) block|
This is subroutine definition, not a real function <i>per se</i>.
Without a BLOCK it&#39;s just a forward declaration.  Without a NAME,
it&#39;s an anonymous function declaration, and does actually return
a value: the CODE ref of the closure you just created.</p>
<p>See <a href="../perlsub.html">perlsub</a> and <a href="../perlref.html">perlref</a> for details about subroutines and
references, and <a href="../attributes.html">attributes</a> and <a href="../Attribute/Handlers.html">Attribute::Handlers</a> for more
information about attributes.
-----

function sub
name : attrs block|
This is subroutine definition, not a real function <i>per se</i>.
Without a BLOCK it&#39;s just a forward declaration.  Without a NAME,
it&#39;s an anonymous function declaration, and does actually return
a value: the CODE ref of the closure you just created.</p>
<p>See <a href="../perlsub.html">perlsub</a> and <a href="../perlref.html">perlref</a> for details about subroutines and
references, and <a href="../attributes.html">attributes</a> and <a href="../Attribute/Handlers.html">Attribute::Handlers</a> for more
information about attributes.
-----

function sub
name (proto) : attrs block|
This is subroutine definition, not a real function <i>per se</i>.
Without a BLOCK it&#39;s just a forward declaration.  Without a NAME,
it&#39;s an anonymous function declaration, and does actually return
a value: the CODE ref of the closure you just created.</p>
<p>See <a href="../perlsub.html">perlsub</a> and <a href="../perlref.html">perlref</a> for details about subroutines and
references, and <a href="../attributes.html">attributes</a> and <a href="../Attribute/Handlers.html">Attribute::Handlers</a> for more
information about attributes.
-----

function substr
expr|
offset|
length|
replacement|
Extracts a substring out of EXPR and returns it.  First character is at
offset <code class="inline"><span class="n">0</span></code>
, or whatever you&#39;ve set <code class="inline"><span class="i">$[</span></code>
 to (but don&#39;t do that).
If OFFSET is negative (or more precisely, less than <code class="inline"><span class="i">$[</span></code>
), starts
that far from the end of the string.  If LENGTH is omitted, returns
everything to the end of the string.  If LENGTH is negative, leaves that
many characters off the end of the string.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$s</span> = <span class="q">&quot;The black cat climbed the green tree&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$color</span>  = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$s</span><span class="cm">,</span> <span class="n">4</span><span class="cm">,</span> <span class="n">5</span><span class="sc">;</span>	<span class="c"># black</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$middle</span> = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$s</span><span class="cm">,</span> <span class="n">4</span><span class="cm">,</span> <span class="n">-11</span><span class="sc">;</span>	<span class="c"># black cat climbed the</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$end</span>    = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$s</span><span class="cm">,</span> <span class="n">14</span><span class="sc">;</span>		<span class="c"># climbed the green tree</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$tail</span>   = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$s</span><span class="cm">,</span> <span class="n">-4</span><span class="sc">;</span>		<span class="c"># tree</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$z</span>      = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$s</span><span class="cm">,</span> <span class="n">-4</span><span class="cm">,</span> <span class="n">2</span><span class="sc">;</span>	<span class="c"># tr</span></li></ol></pre><p>You can use the substr() function as an lvalue, in which case EXPR
must itself be an lvalue.  If you assign something shorter than LENGTH,
the string will shrink, and if you assign something longer than LENGTH,
the string will grow to accommodate it.  To keep the string the same
length you may need to pad or chop your value using <code class="inline"><a class="l_k" href="../functions/sprintf.html">sprintf</a></code>.</p>
<p>If OFFSET and LENGTH specify a substring that is partly outside the
string, only the part within the string is returned.  If the substring
is beyond either end of the string, substr() returns the undefined
value and produces a warning.  When used as an lvalue, specifying a
substring that is entirely outside the string is a fatal error.
Here&#39;s an example showing the behavior for boundary cases:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$name</span> = <span class="q">&#39;fred&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$name</span><span class="cm">,</span> <span class="n">4</span><span class="s">)</span> = <span class="q">&#39;dy&#39;</span><span class="sc">;</span>		<span class="c"># $name is now &#39;freddy&#39;</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$null</span> = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$name</span><span class="cm">,</span> <span class="n">6</span><span class="cm">,</span> <span class="n">2</span><span class="sc">;</span>	<span class="c"># returns &#39;&#39; (no warning)</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$oops</span> = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$name</span><span class="cm">,</span> <span class="n">7</span><span class="sc">;</span>		<span class="c"># returns undef, with warning</span></li><li>    <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$name</span><span class="cm">,</span> <span class="n">7</span><span class="s">)</span> = <span class="q">&#39;gap&#39;</span><span class="sc">;</span>		<span class="c"># fatal error</span></li></ol></pre><p>An alternative to using substr() as an lvalue is to specify the
replacement string as the 4th argument.  This allows you to replace
parts of the EXPR and return what was there before in one operation,
just as you can with splice().</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$s</span> = <span class="q">&quot;The black cat climbed the green tree&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$z</span> = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$s</span><span class="cm">,</span> <span class="n">14</span><span class="cm">,</span> <span class="n">7</span><span class="cm">,</span> <span class="q">&quot;jumped from&quot;</span><span class="sc">;</span>	<span class="c"># climbed</span></li><li>    <span class="c"># $s is now &quot;The black cat jumped from the green tree&quot;</span></li></ol></pre><p>Note that the lvalue returned by the 3-arg version of substr() acts as
a &#39;magic bullet&#39;; each time it is assigned to, it remembers which part
of the original string is being modified; for example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$x</span> = <span class="q">'1234'</span><span class="sc">;</span></li><li>    for <span class="s">(</span><a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$x</span><span class="cm">,</span><span class="n">1</span><span class="cm">,</span><span class="n">2</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$_</span> = <span class="q">'a'</span><span class="sc">;</span>   <a class="l_k" href="../functions/print.html">print</a> <span class="i">$x</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>	<span class="c"># prints 1a4</span></li><li>        <span class="i">$_</span> = <span class="q">'xyz'</span><span class="sc">;</span> <a class="l_k" href="../functions/print.html">print</a> <span class="i">$x</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>	<span class="c"># prints 1xyz4</span></li><li>        <span class="i">$x</span> = <span class="q">'56789'</span><span class="sc">;</span></li><li>        <span class="i">$_</span> = <span class="q">'pq'</span><span class="sc">;</span>  <a class="l_k" href="../functions/print.html">print</a> <span class="i">$x</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>	<span class="c"># prints 5pq9</span></li><li>    <span class="s">}</span></li></ol></pre><p>Prior to Perl version 5.9.1, the result of using an lvalue multiple times was
unspecified.
-----

function substr
expr|
offset|
length|
Extracts a substring out of EXPR and returns it.  First character is at
offset <code class="inline"><span class="n">0</span></code>
, or whatever you&#39;ve set <code class="inline"><span class="i">$[</span></code>
 to (but don&#39;t do that).
If OFFSET is negative (or more precisely, less than <code class="inline"><span class="i">$[</span></code>
), starts
that far from the end of the string.  If LENGTH is omitted, returns
everything to the end of the string.  If LENGTH is negative, leaves that
many characters off the end of the string.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$s</span> = <span class="q">&quot;The black cat climbed the green tree&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$color</span>  = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$s</span><span class="cm">,</span> <span class="n">4</span><span class="cm">,</span> <span class="n">5</span><span class="sc">;</span>	<span class="c"># black</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$middle</span> = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$s</span><span class="cm">,</span> <span class="n">4</span><span class="cm">,</span> <span class="n">-11</span><span class="sc">;</span>	<span class="c"># black cat climbed the</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$end</span>    = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$s</span><span class="cm">,</span> <span class="n">14</span><span class="sc">;</span>		<span class="c"># climbed the green tree</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$tail</span>   = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$s</span><span class="cm">,</span> <span class="n">-4</span><span class="sc">;</span>		<span class="c"># tree</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$z</span>      = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$s</span><span class="cm">,</span> <span class="n">-4</span><span class="cm">,</span> <span class="n">2</span><span class="sc">;</span>	<span class="c"># tr</span></li></ol></pre><p>You can use the substr() function as an lvalue, in which case EXPR
must itself be an lvalue.  If you assign something shorter than LENGTH,
the string will shrink, and if you assign something longer than LENGTH,
the string will grow to accommodate it.  To keep the string the same
length you may need to pad or chop your value using <code class="inline"><a class="l_k" href="../functions/sprintf.html">sprintf</a></code>.</p>
<p>If OFFSET and LENGTH specify a substring that is partly outside the
string, only the part within the string is returned.  If the substring
is beyond either end of the string, substr() returns the undefined
value and produces a warning.  When used as an lvalue, specifying a
substring that is entirely outside the string is a fatal error.
Here&#39;s an example showing the behavior for boundary cases:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$name</span> = <span class="q">&#39;fred&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$name</span><span class="cm">,</span> <span class="n">4</span><span class="s">)</span> = <span class="q">&#39;dy&#39;</span><span class="sc">;</span>		<span class="c"># $name is now &#39;freddy&#39;</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$null</span> = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$name</span><span class="cm">,</span> <span class="n">6</span><span class="cm">,</span> <span class="n">2</span><span class="sc">;</span>	<span class="c"># returns &#39;&#39; (no warning)</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$oops</span> = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$name</span><span class="cm">,</span> <span class="n">7</span><span class="sc">;</span>		<span class="c"># returns undef, with warning</span></li><li>    <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$name</span><span class="cm">,</span> <span class="n">7</span><span class="s">)</span> = <span class="q">&#39;gap&#39;</span><span class="sc">;</span>		<span class="c"># fatal error</span></li></ol></pre><p>An alternative to using substr() as an lvalue is to specify the
replacement string as the 4th argument.  This allows you to replace
parts of the EXPR and return what was there before in one operation,
just as you can with splice().</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$s</span> = <span class="q">&quot;The black cat climbed the green tree&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$z</span> = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$s</span><span class="cm">,</span> <span class="n">14</span><span class="cm">,</span> <span class="n">7</span><span class="cm">,</span> <span class="q">&quot;jumped from&quot;</span><span class="sc">;</span>	<span class="c"># climbed</span></li><li>    <span class="c"># $s is now &quot;The black cat jumped from the green tree&quot;</span></li></ol></pre><p>Note that the lvalue returned by the 3-arg version of substr() acts as
a &#39;magic bullet&#39;; each time it is assigned to, it remembers which part
of the original string is being modified; for example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$x</span> = <span class="q">'1234'</span><span class="sc">;</span></li><li>    for <span class="s">(</span><a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$x</span><span class="cm">,</span><span class="n">1</span><span class="cm">,</span><span class="n">2</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$_</span> = <span class="q">'a'</span><span class="sc">;</span>   <a class="l_k" href="../functions/print.html">print</a> <span class="i">$x</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>	<span class="c"># prints 1a4</span></li><li>        <span class="i">$_</span> = <span class="q">'xyz'</span><span class="sc">;</span> <a class="l_k" href="../functions/print.html">print</a> <span class="i">$x</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>	<span class="c"># prints 1xyz4</span></li><li>        <span class="i">$x</span> = <span class="q">'56789'</span><span class="sc">;</span></li><li>        <span class="i">$_</span> = <span class="q">'pq'</span><span class="sc">;</span>  <a class="l_k" href="../functions/print.html">print</a> <span class="i">$x</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>	<span class="c"># prints 5pq9</span></li><li>    <span class="s">}</span></li></ol></pre><p>Prior to Perl version 5.9.1, the result of using an lvalue multiple times was
unspecified.
-----

function substr
expr|
offset|
Extracts a substring out of EXPR and returns it.  First character is at
offset <code class="inline"><span class="n">0</span></code>
, or whatever you&#39;ve set <code class="inline"><span class="i">$[</span></code>
 to (but don&#39;t do that).
If OFFSET is negative (or more precisely, less than <code class="inline"><span class="i">$[</span></code>
), starts
that far from the end of the string.  If LENGTH is omitted, returns
everything to the end of the string.  If LENGTH is negative, leaves that
many characters off the end of the string.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$s</span> = <span class="q">&quot;The black cat climbed the green tree&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$color</span>  = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$s</span><span class="cm">,</span> <span class="n">4</span><span class="cm">,</span> <span class="n">5</span><span class="sc">;</span>	<span class="c"># black</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$middle</span> = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$s</span><span class="cm">,</span> <span class="n">4</span><span class="cm">,</span> <span class="n">-11</span><span class="sc">;</span>	<span class="c"># black cat climbed the</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$end</span>    = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$s</span><span class="cm">,</span> <span class="n">14</span><span class="sc">;</span>		<span class="c"># climbed the green tree</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$tail</span>   = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$s</span><span class="cm">,</span> <span class="n">-4</span><span class="sc">;</span>		<span class="c"># tree</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$z</span>      = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$s</span><span class="cm">,</span> <span class="n">-4</span><span class="cm">,</span> <span class="n">2</span><span class="sc">;</span>	<span class="c"># tr</span></li></ol></pre><p>You can use the substr() function as an lvalue, in which case EXPR
must itself be an lvalue.  If you assign something shorter than LENGTH,
the string will shrink, and if you assign something longer than LENGTH,
the string will grow to accommodate it.  To keep the string the same
length you may need to pad or chop your value using <code class="inline"><a class="l_k" href="../functions/sprintf.html">sprintf</a></code>.</p>
<p>If OFFSET and LENGTH specify a substring that is partly outside the
string, only the part within the string is returned.  If the substring
is beyond either end of the string, substr() returns the undefined
value and produces a warning.  When used as an lvalue, specifying a
substring that is entirely outside the string is a fatal error.
Here&#39;s an example showing the behavior for boundary cases:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$name</span> = <span class="q">&#39;fred&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$name</span><span class="cm">,</span> <span class="n">4</span><span class="s">)</span> = <span class="q">&#39;dy&#39;</span><span class="sc">;</span>		<span class="c"># $name is now &#39;freddy&#39;</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$null</span> = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$name</span><span class="cm">,</span> <span class="n">6</span><span class="cm">,</span> <span class="n">2</span><span class="sc">;</span>	<span class="c"># returns &#39;&#39; (no warning)</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$oops</span> = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$name</span><span class="cm">,</span> <span class="n">7</span><span class="sc">;</span>		<span class="c"># returns undef, with warning</span></li><li>    <a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$name</span><span class="cm">,</span> <span class="n">7</span><span class="s">)</span> = <span class="q">&#39;gap&#39;</span><span class="sc">;</span>		<span class="c"># fatal error</span></li></ol></pre><p>An alternative to using substr() as an lvalue is to specify the
replacement string as the 4th argument.  This allows you to replace
parts of the EXPR and return what was there before in one operation,
just as you can with splice().</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$s</span> = <span class="q">&quot;The black cat climbed the green tree&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$z</span> = <a class="l_k" href="../functions/substr.html">substr</a> <span class="i">$s</span><span class="cm">,</span> <span class="n">14</span><span class="cm">,</span> <span class="n">7</span><span class="cm">,</span> <span class="q">&quot;jumped from&quot;</span><span class="sc">;</span>	<span class="c"># climbed</span></li><li>    <span class="c"># $s is now &quot;The black cat jumped from the green tree&quot;</span></li></ol></pre><p>Note that the lvalue returned by the 3-arg version of substr() acts as
a &#39;magic bullet&#39;; each time it is assigned to, it remembers which part
of the original string is being modified; for example:</p>
<pre class="verbatim"><ol><li>    <span class="i">$x</span> = <span class="q">'1234'</span><span class="sc">;</span></li><li>    for <span class="s">(</span><a class="l_k" href="../functions/substr.html">substr</a><span class="s">(</span><span class="i">$x</span><span class="cm">,</span><span class="n">1</span><span class="cm">,</span><span class="n">2</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$_</span> = <span class="q">'a'</span><span class="sc">;</span>   <a class="l_k" href="../functions/print.html">print</a> <span class="i">$x</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>	<span class="c"># prints 1a4</span></li><li>        <span class="i">$_</span> = <span class="q">'xyz'</span><span class="sc">;</span> <a class="l_k" href="../functions/print.html">print</a> <span class="i">$x</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>	<span class="c"># prints 1xyz4</span></li><li>        <span class="i">$x</span> = <span class="q">'56789'</span><span class="sc">;</span></li><li>        <span class="i">$_</span> = <span class="q">'pq'</span><span class="sc">;</span>  <a class="l_k" href="../functions/print.html">print</a> <span class="i">$x</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>	<span class="c"># prints 5pq9</span></li><li>    <span class="s">}</span></li></ol></pre><p>Prior to Perl version 5.9.1, the result of using an lvalue multiple times was
unspecified.
-----

function symlink
oldfile|
newfile|
Creates a new filename symbolically linked to the old filename.
Returns <code class="inline"><span class="n">1</span></code>
 for success, <code class="inline"><span class="n">0</span></code>
 otherwise.  On systems that don&#39;t support
symbolic links, produces a fatal error at run time.  To check for that,
use eval:</p>
<pre class="verbatim"><ol><li>    <span class="i">$symlink_exists</span> = <a class="l_k" href="../functions/eval.html">eval</a> <span class="s">{</span> <a class="l_k" href="../functions/symlink.html">symlink</a><span class="s">(</span><span class="q">&quot;&quot;</span><span class="cm">,</span><span class="q">&quot;&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="n">1</span> <span class="s">}</span><span class="sc">;</span></li></ol><
-----

function syscall
number|
list|
Calls the system call specified as the first element of the list,
passing the remaining elements as arguments to the system call.  If
unimplemented, produces a fatal error.  The arguments are interpreted
as follows: if a given argument is numeric, the argument is passed as
an int.  If not, the pointer to the string value is passed.  You are
responsible to make sure a string is pre-extended long enough to
receive any result that might be written into a string.  You can&#39;t use a
string literal (or other read-only string) as an argument to <code class="inline"><a class="l_k" href="../functions/syscall.html">syscall</a></code>
because Perl has to assume that any string pointer might be written
through.  If your
integer arguments are not literals and have never been interpreted in a
numeric context, you may need to add <code class="inline"><span class="n">0</span></code>
 to them to force them to look
like numbers.  This emulates the <code class="inline"><a class="l_k" href="../functions/syswrite.html">syswrite</a></code> function (or vice versa):</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/require.html">require</a> <span class="q">&#39;syscall.ph&#39;</span><span class="sc">;</span>		<span class="c"># may need to run h2ph</span></li><li>    <span class="i">$s</span> = <span class="q">&quot;hi there\n&quot;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/syscall.html">syscall</a><span class="s">(</span><span class="i">&amp;SYS_write</span><span class="cm">,</span> <a class="l_k" href="../functions/fileno.html">fileno</a><span class="s">(</span><span class="w">STDOUT</span><span class="s">)</span><span class="cm">,</span> <span class="i">$s</span><span class="cm">,</span> <a class="l_k" href="../functions/length.html">length</a> <span class="i">$s</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Note that Perl supports passing of up to only 14 arguments to your system call,
which in practice should usually suffice.</p>
<p>Syscall returns whatever value returned by the system call it calls.
If the system call fails, <code class="inline"><a class="l_k" href="../functions/syscall.html">syscall</a></code> returns <code class="inline"><span class="n">-1</span></code>
 and sets <code class="inline"><span class="i">$!</span></code>
 (errno).
Note that some system calls can legitimately return <code class="inline"><span class="n">-1</span></code>
.  The proper
way to handle such calls is to assign <code class="inline"><span class="i">$!</span>=<span class="n">0</span><span class="sc">;</span></code>
 before the call and
check the value of <code class="inline"><span class="i">$!</span></code>
 if syscall returns <code class="inline"><span class="n">-1</span></code>
.</p>
<p>There&#39;s a problem with <code class="inline"><a class="l_k" href="../functions/syscall.html">syscall(&SYS_pipe)</a></code>: it returns the file
number of the read end of the pipe it creates.  There is no way
to retrieve the file number of the other end.  You can avoid this
problem by using <code class="inline"><a class="l_k" href="../functions/pipe.html">pipe</a></code> instead.
-----

function sysopen
filehandle|
filename|
mode|
Opens the file whose filename is given by FILENAME, and associates it
with FILEHANDLE.  If FILEHANDLE is an expression, its value is used as
the name of the real filehandle wanted.  This function calls the
underlying operating system&#39;s <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> function with the parameters
FILENAME, MODE, PERMS.</p>
<p>The possible values and flag bits of the MODE parameter are
system-dependent; they are available via the standard module <code class="inline"><span class="w">Fcntl</span></code>
.
See the documentation of your operating system&#39;s <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> to see which
values and flag bits are available.  You may combine several flags
using the <code class="inline">|</code>-operator.</p>
<p>Some of the most common values are <code class="inline"><span class="w">O_RDONLY</span></code>
 for opening the file in
read-only mode, <code class="inline"><span class="w">O_WRONLY</span></code>
 for opening the file in write-only mode,
and <code class="inline"><span class="w">O_RDWR</span></code>
 for opening the file in read-write mode.
  </p>
<p>For historical reasons, some values work on almost every system
supported by perl: zero means read-only, one means write-only, and two
means read/write.  We know that these values do <i>not</i> work under
OS/390 &amp; VM/ESA Unix and on the Macintosh; you probably don&#39;t want to
use them in new code.</p>
<p>If the file named by FILENAME does not exist and the <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> call creates
it (typically because MODE includes the <code class="inline"><span class="w">O_CREAT</span></code>
 flag), then the value of
PERMS specifies the permissions of the newly created file.  If you omit
the PERMS argument to <code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code>, Perl uses the octal value <code class="inline"><span class="n">0666</span></code>
.
These permission values need to be in octal, and are modified by your
process&#39;s current <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code>.
</p>
<p>In many systems the <code class="inline"><span class="w">O_EXCL</span></code>
 flag is available for opening files in
exclusive mode.  This is <b>not</b> locking: exclusiveness means here that
if the file already exists, sysopen() fails.  <code class="inline"><span class="w">O_EXCL</span></code>
 may not work
on network filesystems, and has no effect unless the <code class="inline"><span class="w">O_CREAT</span></code>
 flag
is set as well.  Setting <code class="inline"><span class="w">O_CREAT</span>|<span class="w">O_EXCL</span></code>
 prevents the file from
being opened if it is a symbolic link.  It does not protect against
symbolic links in the file&#39;s path.
</p>
<p>Sometimes you may want to truncate an already-existing file.  This
can be done using the <code class="inline"><span class="w">O_TRUNC</span></code>
 flag.  The behavior of
<code class="inline"><span class="w">O_TRUNC</span></code>
 with <code class="inline"><span class="w">O_RDONLY</span></code>
 is undefined.
</p>
<p>You should seldom if ever use <code class="inline"><span class="n">0644</span></code>
 as argument to <code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code>, because
that takes away the user&#39;s option to have a more permissive umask.
Better to omit it.  See the perlfunc(1) entry on <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code> for more
on this.</p>
<p>Note that <code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code> depends on the fdopen() C library function.
On many UNIX systems, fdopen() is known to fail when file descriptors
exceed a certain value, typically 255. If you need more file
descriptors than that, consider rebuilding Perl to use the <code class="inline"><span class="w">sfio</span></code>

library, or perhaps using the POSIX::open() function.</p>
<p>See <a href="../perlopentut.html">perlopentut</a> for a kinder, gentler explanation of opening files.
-----

function sysopen
filehandle|
filename|
mode|
perms|
Opens the file whose filename is given by FILENAME, and associates it
with FILEHANDLE.  If FILEHANDLE is an expression, its value is used as
the name of the real filehandle wanted.  This function calls the
underlying operating system&#39;s <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> function with the parameters
FILENAME, MODE, PERMS.</p>
<p>The possible values and flag bits of the MODE parameter are
system-dependent; they are available via the standard module <code class="inline"><span class="w">Fcntl</span></code>
.
See the documentation of your operating system&#39;s <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> to see which
values and flag bits are available.  You may combine several flags
using the <code class="inline">|</code>-operator.</p>
<p>Some of the most common values are <code class="inline"><span class="w">O_RDONLY</span></code>
 for opening the file in
read-only mode, <code class="inline"><span class="w">O_WRONLY</span></code>
 for opening the file in write-only mode,
and <code class="inline"><span class="w">O_RDWR</span></code>
 for opening the file in read-write mode.
  </p>
<p>For historical reasons, some values work on almost every system
supported by perl: zero means read-only, one means write-only, and two
means read/write.  We know that these values do <i>not</i> work under
OS/390 &amp; VM/ESA Unix and on the Macintosh; you probably don&#39;t want to
use them in new code.</p>
<p>If the file named by FILENAME does not exist and the <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> call creates
it (typically because MODE includes the <code class="inline"><span class="w">O_CREAT</span></code>
 flag), then the value of
PERMS specifies the permissions of the newly created file.  If you omit
the PERMS argument to <code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code>, Perl uses the octal value <code class="inline"><span class="n">0666</span></code>
.
These permission values need to be in octal, and are modified by your
process&#39;s current <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code>.
</p>
<p>In many systems the <code class="inline"><span class="w">O_EXCL</span></code>
 flag is available for opening files in
exclusive mode.  This is <b>not</b> locking: exclusiveness means here that
if the file already exists, sysopen() fails.  <code class="inline"><span class="w">O_EXCL</span></code>
 may not work
on network filesystems, and has no effect unless the <code class="inline"><span class="w">O_CREAT</span></code>
 flag
is set as well.  Setting <code class="inline"><span class="w">O_CREAT</span>|<span class="w">O_EXCL</span></code>
 prevents the file from
being opened if it is a symbolic link.  It does not protect against
symbolic links in the file&#39;s path.
</p>
<p>Sometimes you may want to truncate an already-existing file.  This
can be done using the <code class="inline"><span class="w">O_TRUNC</span></code>
 flag.  The behavior of
<code class="inline"><span class="w">O_TRUNC</span></code>
 with <code class="inline"><span class="w">O_RDONLY</span></code>
 is undefined.
</p>
<p>You should seldom if ever use <code class="inline"><span class="n">0644</span></code>
 as argument to <code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code>, because
that takes away the user&#39;s option to have a more permissive umask.
Better to omit it.  See the perlfunc(1) entry on <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code> for more
on this.</p>
<p>Note that <code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code> depends on the fdopen() C library function.
On many UNIX systems, fdopen() is known to fail when file descriptors
exceed a certain value, typically 255. If you need more file
descriptors than that, consider rebuilding Perl to use the <code class="inline"><span class="w">sfio</span></code>

library, or perhaps using the POSIX::open() function.</p>
<p>See <a href="../perlopentut.html">perlopentut</a> for a kinder, gentler explanation of opening files.
-----

function sysread
filehandle|
scalar|
length|
offset|
Attempts to read LENGTH bytes of data into variable SCALAR from the
specified FILEHANDLE, using the system call read(2).  It bypasses
buffered IO, so mixing this with other kinds of reads, <code class="inline"><a class="l_k" href="../functions/print.html">print</a></code>,
<code class="inline"><a class="l_k" href="../functions/write.html">write</a></code>, <code class="inline"><a class="l_k" href="../functions/seek.html">seek</a></code>, <code class="inline"><a class="l_k" href="../functions/tell.html">tell</a></code>, or <code class="inline"><a class="l_k" href="../functions/eof.html">eof</a></code> can cause confusion because the
perlio or stdio layers usually buffers data.  Returns the number of
bytes actually read, <code class="inline"><span class="n">0</span></code>
 at end of file, or undef if there was an
error (in the latter case <code class="inline"><span class="i">$!</span></code>
 is also set).  SCALAR will be grown or
shrunk so that the last byte actually read is the last byte of the
scalar after the read.</p>
<p>An OFFSET may be specified to place the read data at some place in the
string other than the beginning.  A negative OFFSET specifies
placement at that many characters counting backwards from the end of
the string.  A positive OFFSET greater than the length of SCALAR
results in the string being padded to the required size with <code class="inline"><span class="q">&quot;\0&quot;</span></code>

bytes before the result of the read is appended.</p>
<p>There is no syseof() function, which is ok, since eof() doesn&#39;t work
very well on device files (like ttys) anyway.  Use sysread() and check
for a return value for 0 to decide whether you&#39;re done.</p>
<p>Note that if the filehandle has been marked as <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
 Unicode
characters are read instead of bytes (the LENGTH, OFFSET, and the
return value of sysread() are in Unicode characters).
The <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span>...<span class="s">)</span></code>
 layer implicitly introduces the <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
 layer.
See <a href="../functions/binmode.html">"binmode"</a>, <a href="../functions/open.html">"open"</a>, and the <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> pragma, <a href="../open.html">open</a>.
-----

function sysread
filehandle|
scalar|
length|
Attempts to read LENGTH bytes of data into variable SCALAR from the
specified FILEHANDLE, using the system call read(2).  It bypasses
buffered IO, so mixing this with other kinds of reads, <code class="inline"><a class="l_k" href="../functions/print.html">print</a></code>,
<code class="inline"><a class="l_k" href="../functions/write.html">write</a></code>, <code class="inline"><a class="l_k" href="../functions/seek.html">seek</a></code>, <code class="inline"><a class="l_k" href="../functions/tell.html">tell</a></code>, or <code class="inline"><a class="l_k" href="../functions/eof.html">eof</a></code> can cause confusion because the
perlio or stdio layers usually buffers data.  Returns the number of
bytes actually read, <code class="inline"><span class="n">0</span></code>
 at end of file, or undef if there was an
error (in the latter case <code class="inline"><span class="i">$!</span></code>
 is also set).  SCALAR will be grown or
shrunk so that the last byte actually read is the last byte of the
scalar after the read.</p>
<p>An OFFSET may be specified to place the read data at some place in the
string other than the beginning.  A negative OFFSET specifies
placement at that many characters counting backwards from the end of
the string.  A positive OFFSET greater than the length of SCALAR
results in the string being padded to the required size with <code class="inline"><span class="q">&quot;\0&quot;</span></code>

bytes before the result of the read is appended.</p>
<p>There is no syseof() function, which is ok, since eof() doesn&#39;t work
very well on device files (like ttys) anyway.  Use sysread() and check
for a return value for 0 to decide whether you&#39;re done.</p>
<p>Note that if the filehandle has been marked as <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
 Unicode
characters are read instead of bytes (the LENGTH, OFFSET, and the
return value of sysread() are in Unicode characters).
The <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span>...<span class="s">)</span></code>
 layer implicitly introduces the <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
 layer.
See <a href="../functions/binmode.html">"binmode"</a>, <a href="../functions/open.html">"open"</a>, and the <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> pragma, <a href="../open.html">open</a>.
-----

function sysseek
filehandle|
position|
whence|
Sets FILEHANDLE&#39;s system position in bytes using the system call
lseek(2).  FILEHANDLE may be an expression whose value gives the name
of the filehandle.  The values for WHENCE are <code class="inline"><span class="n">0</span></code>
 to set the new
position to POSITION, <code class="inline"><span class="n">1</span></code>
 to set the it to the current position plus
POSITION, and <code class="inline"><span class="n">2</span></code>
 to set it to EOF plus POSITION (typically
negative).</p>
<p>Note the <i>in bytes</i>: even if the filehandle has been set to operate
on characters (for example by using the <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span><span class="w">utf8</span><span class="s">)</span></code>
 I/O layer),
tell() will return byte offsets, not character offsets (because
implementing that would render sysseek() very slow).</p>
<p>sysseek() bypasses normal buffered IO, so mixing this with reads (other
than <code class="inline"><a class="l_k" href="../functions/sysread.html">sysread</a></code>, for example <code class="inline">&lt;&gt;</code>
 or read()) <code class="inline"><a class="l_k" href="../functions/print.html">print</a></code>, <code class="inline"><a class="l_k" href="../functions/write.html">write</a></code>,
<code class="inline"><a class="l_k" href="../functions/seek.html">seek</a></code>, <code class="inline"><a class="l_k" href="../functions/tell.html">tell</a></code>, or <code class="inline"><a class="l_k" href="../functions/eof.html">eof</a></code> may cause confusion.</p>
<p>For WHENCE, you may also use the constants <code class="inline"><span class="w">SEEK_SET</span></code>
, <code class="inline"><span class="w">SEEK_CUR</span></code>
,
and <code class="inline"><span class="w">SEEK_END</span></code>
 (start of the file, current position, end of the file)
from the Fcntl module.  Use of the constants is also more portable
than relying on 0, 1, and 2.  For example to define a &quot;systell&quot; function:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="../functions/use.html">use</a> <span class="w">Fcntl</span> <span class="q">&#39;SEEK_CUR&#39;</span><span class="sc">;</span></li><li><a name="systell"></a>	sub <span class="m">systell</span> <span class="s">{</span> <a class="l_k" href="../functions/sysseek.html">sysseek</a><span class="s">(</span><span class="i">$_</span>[<span class="n">0</span>]<span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="w">SEEK_CUR</span><span class="s">)</span> <span class="s">}</span></li></ol></pre><p>Returns the new position, or the undefined value on failure.  A position
of zero is returned as the string <code class="inline"><span class="q">&quot;0 but true&quot;</span></code>
; thus <code class="inline"><a class="l_k" href="../functions/sysseek.html">sysseek</a></code> returns
true on success and false on failure, yet you can still easily determine
the new position.
-----

function system
list|
Does exactly the same thing as <code class="inline"><a class="l_k" href="../functions/exec.html">exec</a> <span class="i">LIST</span></code>
, except that a fork is
done first, and the parent process waits for the child process to
complete.  Note that argument processing varies depending on the
number of arguments.  If there is more than one argument in LIST,
or if LIST is an array with more than one value, starts the program
given by the first element of the list with arguments given by the
rest of the list.  If there is only one scalar argument, the argument
is checked for shell metacharacters, and if there are any, the
entire argument is passed to the system&#39;s command shell for parsing
(this is <code class="inline">/bin/sh -c</code> on Unix platforms, but varies on other
platforms).  If there are no shell metacharacters in the argument,
it is split into words and passed directly to <code class="inline"><span class="w">execvp</span></code>
, which is
more efficient.</p>
<p>Beginning with v5.6.0, Perl will attempt to flush all files opened for
output before any operation that may do a fork, but this may not be
supported on some platforms (see <a href="../perlport.html">perlport</a>).  To be safe, you may need
to set <code class="inline"><span class="i">$|</span></code>
 ($AUTOFLUSH in English) or call the <code class="inline"><span class="i">autoflush</span><span class="s">(</span><span class="s">)</span></code>
 method
of <code class="inline"><span class="w">IO::Handle</span></code>
 on any open handles.</p>
<p>The return value is the exit status of the program as returned by the
<code class="inline"><a class="l_k" href="../functions/wait.html">wait</a></code> call.  To get the actual exit value, shift right by eight (see
below). See also <a href="../functions/exec.html">"exec"</a>.  This is <i>not</i> what you want to use to capture
the output from a command, for that you should use merely backticks or
<code class="inline"><a class="l_k" href="../functions/qx.html">qx//</a></code>, as described in <a href="../perlop.html#%60STRING%60">"`STRING`" in perlop</a>.  Return value of -1
indicates a failure to start the program or an error of the wait(2) system
call (inspect $! for the reason).</p>
<p>If you&#39;d like to make <code class="inline"><a class="l_k" href="../functions/system.html">system</a></code> (and many other bits of Perl) die on error,
have a look at the <a href="../autodie.html">autodie</a> pragma.</p>
<p>Like <code class="inline"><a class="l_k" href="../functions/exec.html">exec</a></code>, <code class="inline"><a class="l_k" href="../functions/system.html">system</a></code> allows you to lie to a program about its name if
you use the <code class="inline"><a class="l_k" href="../functions/system.html">system</a> <span class="i">PROGRAM</span> <span class="w">LIST</span></code>
 syntax.  Again, see <a href="../functions/exec.html">"exec"</a>.</p>
<p>Since <code class="inline"><span class="w">SIGINT</span></code>
 and <code class="inline"><span class="w">SIGQUIT</span></code>
 are ignored during the execution of
<code class="inline"><a class="l_k" href="../functions/system.html">system</a></code>, if you expect your program to terminate on receipt of these
signals you will need to arrange to do so yourself based on the return
value.</p>
<pre class="verbatim"><ol><li>    <span class="i">@args</span> = <span class="s">(</span><span class="q">&quot;command&quot;</span><span class="cm">,</span> <span class="q">&quot;arg1&quot;</span><span class="cm">,</span> <span class="q">&quot;arg2&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/system.html">system</a><span class="s">(</span><span class="i">@args</span><span class="s">)</span> == <span class="n">0</span></li><li>	 or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;system @args failed: $?&quot;</span></li></ol></pre><p>If you&#39;d like to manually inspect <code class="inline"><a class="l_k" href="../functions/system.html">system</a></code>&#39;s failure, you can check all
possible failure modes by inspecting <code class="inline"><span class="i">$?</span></code>
 like this:</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span><span class="i">$?</span> == <span class="n">-1</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;failed to execute: $!\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    elsif <span class="s">(</span><span class="i">$?</span> &amp; <span class="n">127</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;child died with signal %d, %s coredump\n&quot;</span><span class="cm">,</span></li><li>	    <span class="s">(</span><span class="i">$?</span> &amp; <span class="n">127</span><span class="s">)</span><span class="cm">,</span>  <span class="s">(</span><span class="i">$?</span> &amp; <span class="n">128</span><span class="s">)</span> ? <span class="q">&#39;with&#39;</span> <span class="co">:</span> <span class="q">&#39;without&#39;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    else <span class="s">{</span></li><li>	<a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;child exited with value %d\n&quot;</span><span class="cm">,</span> <span class="i">$?</span> &gt;&gt; <span class="n">8</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Alternatively you might inspect the value of <code class="inline"><span class="i">$</span>{<span class="w">^CHILD_ERROR_NATIVE</span>}</code>

with the W*() calls of the POSIX extension.</p>
<p>When the arguments get executed via the system shell, results
and return codes will be subject to its quirks and capabilities.
See <a href="../perlop.html#%60STRING%60">"`STRING`" in perlop</a> and <a href="../functions/exec.html">"exec"</a> for details.
-----

function system
program list|
Does exactly the same thing as <code class="inline"><a class="l_k" href="../functions/exec.html">exec</a> <span class="i">LIST</span></code>
, except that a fork is
done first, and the parent process waits for the child process to
complete.  Note that argument processing varies depending on the
number of arguments.  If there is more than one argument in LIST,
or if LIST is an array with more than one value, starts the program
given by the first element of the list with arguments given by the
rest of the list.  If there is only one scalar argument, the argument
is checked for shell metacharacters, and if there are any, the
entire argument is passed to the system&#39;s command shell for parsing
(this is <code class="inline">/bin/sh -c</code> on Unix platforms, but varies on other
platforms).  If there are no shell metacharacters in the argument,
it is split into words and passed directly to <code class="inline"><span class="w">execvp</span></code>
, which is
more efficient.</p>
<p>Beginning with v5.6.0, Perl will attempt to flush all files opened for
output before any operation that may do a fork, but this may not be
supported on some platforms (see <a href="../perlport.html">perlport</a>).  To be safe, you may need
to set <code class="inline"><span class="i">$|</span></code>
 ($AUTOFLUSH in English) or call the <code class="inline"><span class="i">autoflush</span><span class="s">(</span><span class="s">)</span></code>
 method
of <code class="inline"><span class="w">IO::Handle</span></code>
 on any open handles.</p>
<p>The return value is the exit status of the program as returned by the
<code class="inline"><a class="l_k" href="../functions/wait.html">wait</a></code> call.  To get the actual exit value, shift right by eight (see
below). See also <a href="../functions/exec.html">"exec"</a>.  This is <i>not</i> what you want to use to capture
the output from a command, for that you should use merely backticks or
<code class="inline"><a class="l_k" href="../functions/qx.html">qx//</a></code>, as described in <a href="../perlop.html#%60STRING%60">"`STRING`" in perlop</a>.  Return value of -1
indicates a failure to start the program or an error of the wait(2) system
call (inspect $! for the reason).</p>
<p>If you&#39;d like to make <code class="inline"><a class="l_k" href="../functions/system.html">system</a></code> (and many other bits of Perl) die on error,
have a look at the <a href="../autodie.html">autodie</a> pragma.</p>
<p>Like <code class="inline"><a class="l_k" href="../functions/exec.html">exec</a></code>, <code class="inline"><a class="l_k" href="../functions/system.html">system</a></code> allows you to lie to a program about its name if
you use the <code class="inline"><a class="l_k" href="../functions/system.html">system</a> <span class="i">PROGRAM</span> <span class="w">LIST</span></code>
 syntax.  Again, see <a href="../functions/exec.html">"exec"</a>.</p>
<p>Since <code class="inline"><span class="w">SIGINT</span></code>
 and <code class="inline"><span class="w">SIGQUIT</span></code>
 are ignored during the execution of
<code class="inline"><a class="l_k" href="../functions/system.html">system</a></code>, if you expect your program to terminate on receipt of these
signals you will need to arrange to do so yourself based on the return
value.</p>
<pre class="verbatim"><ol><li>    <span class="i">@args</span> = <span class="s">(</span><span class="q">&quot;command&quot;</span><span class="cm">,</span> <span class="q">&quot;arg1&quot;</span><span class="cm">,</span> <span class="q">&quot;arg2&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/system.html">system</a><span class="s">(</span><span class="i">@args</span><span class="s">)</span> == <span class="n">0</span></li><li>	 or <a class="l_k" href="../functions/die.html">die</a> <span class="q">&quot;system @args failed: $?&quot;</span></li></ol></pre><p>If you&#39;d like to manually inspect <code class="inline"><a class="l_k" href="../functions/system.html">system</a></code>&#39;s failure, you can check all
possible failure modes by inspecting <code class="inline"><span class="i">$?</span></code>
 like this:</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span><span class="i">$?</span> == <span class="n">-1</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;failed to execute: $!\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    elsif <span class="s">(</span><span class="i">$?</span> &amp; <span class="n">127</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;child died with signal %d, %s coredump\n&quot;</span><span class="cm">,</span></li><li>	    <span class="s">(</span><span class="i">$?</span> &amp; <span class="n">127</span><span class="s">)</span><span class="cm">,</span>  <span class="s">(</span><span class="i">$?</span> &amp; <span class="n">128</span><span class="s">)</span> ? <span class="q">&#39;with&#39;</span> <span class="co">:</span> <span class="q">&#39;without&#39;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    else <span class="s">{</span></li><li>	<a class="l_k" href="../functions/printf.html">printf</a> <span class="q">&quot;child exited with value %d\n&quot;</span><span class="cm">,</span> <span class="i">$?</span> &gt;&gt; <span class="n">8</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>Alternatively you might inspect the value of <code class="inline"><span class="i">$</span>{<span class="w">^CHILD_ERROR_NATIVE</span>}</code>

with the W*() calls of the POSIX extension.</p>
<p>When the arguments get executed via the system shell, results
and return codes will be subject to its quirks and capabilities.
See <a href="../perlop.html#%60STRING%60">"`STRING`" in perlop</a> and <a href="../functions/exec.html">"exec"</a> for details.
-----

function syswrite
filehandle|
scalar|
length|
offset|
Attempts to write LENGTH bytes of data from variable SCALAR to the
specified FILEHANDLE, using the system call write(2).  If LENGTH is
not specified, writes whole SCALAR.  It bypasses buffered IO, so
mixing this with reads (other than <code class="inline"><a class="l_k" href="../functions/sysread.html">sysread())</a></code>, <code class="inline"><a class="l_k" href="../functions/print.html">print</a></code>, <code class="inline"><a class="l_k" href="../functions/write.html">write</a></code>,
<code class="inline"><a class="l_k" href="../functions/seek.html">seek</a></code>, <code class="inline"><a class="l_k" href="../functions/tell.html">tell</a></code>, or <code class="inline"><a class="l_k" href="../functions/eof.html">eof</a></code> may cause confusion because the perlio and
stdio layers usually buffers data.  Returns the number of bytes
actually written, or <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> if there was an error (in this case the
errno variable <code class="inline"><span class="i">$!</span></code>
 is also set).  If the LENGTH is greater than the
available data in the SCALAR after the OFFSET, only as much data as is
available will be written.</p>
<p>An OFFSET may be specified to write the data from some part of the
string other than the beginning.  A negative OFFSET specifies writing
that many characters counting backwards from the end of the string.
In the case the SCALAR is empty you can use OFFSET but only zero offset.</p>
<p>Note that if the filehandle has been marked as <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
, Unicode
characters are written instead of bytes (the LENGTH, OFFSET, and the
return value of syswrite() are in UTF-8 encoded Unicode characters).
The <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span>...<span class="s">)</span></code>
 layer implicitly introduces the <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
 layer.
See <a href="../functions/binmode.html">"binmode"</a>, <a href="../functions/open.html">"open"</a>, and the <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> pragma, <a href="../open.html">open</a>.
-----

function syswrite
filehandle|
scalar|
length|
Attempts to write LENGTH bytes of data from variable SCALAR to the
specified FILEHANDLE, using the system call write(2).  If LENGTH is
not specified, writes whole SCALAR.  It bypasses buffered IO, so
mixing this with reads (other than <code class="inline"><a class="l_k" href="../functions/sysread.html">sysread())</a></code>, <code class="inline"><a class="l_k" href="../functions/print.html">print</a></code>, <code class="inline"><a class="l_k" href="../functions/write.html">write</a></code>,
<code class="inline"><a class="l_k" href="../functions/seek.html">seek</a></code>, <code class="inline"><a class="l_k" href="../functions/tell.html">tell</a></code>, or <code class="inline"><a class="l_k" href="../functions/eof.html">eof</a></code> may cause confusion because the perlio and
stdio layers usually buffers data.  Returns the number of bytes
actually written, or <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> if there was an error (in this case the
errno variable <code class="inline"><span class="i">$!</span></code>
 is also set).  If the LENGTH is greater than the
available data in the SCALAR after the OFFSET, only as much data as is
available will be written.</p>
<p>An OFFSET may be specified to write the data from some part of the
string other than the beginning.  A negative OFFSET specifies writing
that many characters counting backwards from the end of the string.
In the case the SCALAR is empty you can use OFFSET but only zero offset.</p>
<p>Note that if the filehandle has been marked as <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
, Unicode
characters are written instead of bytes (the LENGTH, OFFSET, and the
return value of syswrite() are in UTF-8 encoded Unicode characters).
The <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span>...<span class="s">)</span></code>
 layer implicitly introduces the <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
 layer.
See <a href="../functions/binmode.html">"binmode"</a>, <a href="../functions/open.html">"open"</a>, and the <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> pragma, <a href="../open.html">open</a>.
-----

function syswrite
filehandle|
scalar|
Attempts to write LENGTH bytes of data from variable SCALAR to the
specified FILEHANDLE, using the system call write(2).  If LENGTH is
not specified, writes whole SCALAR.  It bypasses buffered IO, so
mixing this with reads (other than <code class="inline"><a class="l_k" href="../functions/sysread.html">sysread())</a></code>, <code class="inline"><a class="l_k" href="../functions/print.html">print</a></code>, <code class="inline"><a class="l_k" href="../functions/write.html">write</a></code>,
<code class="inline"><a class="l_k" href="../functions/seek.html">seek</a></code>, <code class="inline"><a class="l_k" href="../functions/tell.html">tell</a></code>, or <code class="inline"><a class="l_k" href="../functions/eof.html">eof</a></code> may cause confusion because the perlio and
stdio layers usually buffers data.  Returns the number of bytes
actually written, or <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> if there was an error (in this case the
errno variable <code class="inline"><span class="i">$!</span></code>
 is also set).  If the LENGTH is greater than the
available data in the SCALAR after the OFFSET, only as much data as is
available will be written.</p>
<p>An OFFSET may be specified to write the data from some part of the
string other than the beginning.  A negative OFFSET specifies writing
that many characters counting backwards from the end of the string.
In the case the SCALAR is empty you can use OFFSET but only zero offset.</p>
<p>Note that if the filehandle has been marked as <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
, Unicode
characters are written instead of bytes (the LENGTH, OFFSET, and the
return value of syswrite() are in UTF-8 encoded Unicode characters).
The <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span>...<span class="s">)</span></code>
 layer implicitly introduces the <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
 layer.
See <a href="../functions/binmode.html">"binmode"</a>, <a href="../functions/open.html">"open"</a>, and the <code class="inline"><a class="l_k" href="../functions/open.html">open</a></code> pragma, <a href="../open.html">open</a>.
-----

function tell
filehandle|
Returns the current position <i>in bytes</i> for FILEHANDLE, or -1 on
error.  FILEHANDLE may be an expression whose value gives the name of
the actual filehandle.  If FILEHANDLE is omitted, assumes the file
last read.</p>
<p>Note the <i>in bytes</i>: even if the filehandle has been set to
operate on characters (for example by using the <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span><span class="w">utf8</span><span class="s">)</span></code>
 open
layer), tell() will return byte offsets, not character offsets (because
that would render seek() and tell() rather slow).</p>
<p>The return value of tell() for the standard streams like the STDIN
depends on the operating system: it may return -1 or something else.
tell() on pipes, fifos, and sockets usually returns -1.</p>
<p>There is no <code class="inline"><span class="w">systell</span></code>
 function.  Use <code class="inline"><a class="l_k" href="../functions/sysseek.html">sysseek</a><span class="s">(</span><span class="w">FH</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span></code>
 for that.</p>
<p>Do not use tell() (or other buffered I/O operations) on a file handle
that has been manipulated by sysread(), syswrite() or sysseek().
Those functions ignore the buffering, while tell() does not.
-----

function tell
Returns the current position <i>in bytes</i> for FILEHANDLE, or -1 on
error.  FILEHANDLE may be an expression whose value gives the name of
the actual filehandle.  If FILEHANDLE is omitted, assumes the file
last read.</p>
<p>Note the <i>in bytes</i>: even if the filehandle has been set to
operate on characters (for example by using the <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span><span class="w">utf8</span><span class="s">)</span></code>
 open
layer), tell() will return byte offsets, not character offsets (because
that would render seek() and tell() rather slow).</p>
<p>The return value of tell() for the standard streams like the STDIN
depends on the operating system: it may return -1 or something else.
tell() on pipes, fifos, and sockets usually returns -1.</p>
<p>There is no <code class="inline"><span class="w">systell</span></code>
 function.  Use <code class="inline"><a class="l_k" href="../functions/sysseek.html">sysseek</a><span class="s">(</span><span class="w">FH</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span></code>
 for that.</p>
<p>Do not use tell() (or other buffered I/O operations) on a file handle
that has been manipulated by sysread(), syswrite() or sysseek().
Those functions ignore the buffering, while tell() does not.
-----

function telldir
dirhandle|
Returns the current position of the <code class="inline"><a class="l_k" href="../functions/readdir.html">readdir</a></code> routines on DIRHANDLE.
Value may be given to <code class="inline"><a class="l_k" href="../functions/seekdir.html">seekdir</a></code> to access a particular location in a
directory.  <code class="inline"><a class="l_k" href="../functions/telldir.html">telldir</a></code> has the same caveats about possible directory
compaction as the corresponding system library routine.
-----

function tie
variable|
classname|
list|
This function binds a variable to a package class that will provide the
implementation for the variable.  VARIABLE is the name of the variable
to be enchanted.  CLASSNAME is the name of a class implementing objects
of correct type.  Any additional arguments are passed to the <code class="inline"><span class="w">new</span></code>

method of the class (meaning <code class="inline"><span class="w">TIESCALAR</span></code>
, <code class="inline"><span class="w">TIEHANDLE</span></code>
, <code class="inline"><span class="w">TIEARRAY</span></code>
,
or <code class="inline"><span class="w">TIEHASH</span></code>
).  Typically these are arguments such as might be passed
to the <code class="inline"><span class="i">dbm_open</span><span class="s">(</span><span class="s">)</span></code>
 function of C.  The object returned by the <code class="inline"><span class="w">new</span></code>

method is also returned by the <code class="inline"><a class="l_k" href="../functions/tie.html">tie</a></code> function, which would be useful
if you want to access other methods in CLASSNAME.</p>
<p>Note that functions such as <code class="inline"><a class="l_k" href="../functions/keys.html">keys</a></code> and <code class="inline"><a class="l_k" href="../functions/values.html">values</a></code> may return huge lists
when used on large objects, like DBM files.  You may prefer to use the
<code class="inline"><a class="l_k" href="../functions/each.html">each</a></code> function to iterate over such.  Example:</p>
<pre class="verbatim"><ol><li>    <span class="c"># print out history file offsets</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">NDBM_File</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/tie.html">tie</a><span class="s">(</span><span class="i">%HIST</span><span class="cm">,</span> <span class="q">&#39;NDBM_File&#39;</span><span class="cm">,</span> <span class="q">&#39;/usr/lib/news/history&#39;</span><span class="cm">,</span> <span class="n">1</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></li><li>    while <span class="s">(</span><span class="s">(</span><span class="i">$key</span><span class="cm">,</span><span class="i">$val</span><span class="s">)</span> = <a class="l_k" href="../functions/each.html">each</a> <span class="i">%HIST</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/print.html">print</a> <span class="i">$key</span><span class="cm">,</span> <span class="q">&#39; = &#39;</span><span class="cm">,</span> <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&#39;L&#39;</span><span class="cm">,</span><span class="i">$val</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    <a class="l_k" href="../functions/untie.html">untie</a><span class="s">(</span><span class="i">%HIST</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>A class implementing a hash should have the following methods:</p>
<pre class="verbatim"><ol><li>    <span class="w">TIEHASH</span> <span class="w">classname</span><span class="cm">,</span> <span class="w">LIST</span></li><li>    <span class="w">FETCH</span> <span class="w">this</span><span class="cm">,</span> <span class="w">key</span></li><li>    <span class="w">STORE</span> <span class="w">this</span><span class="cm">,</span> <span class="w">key</span><span class="cm">,</span> <span class="w">value</span></li><li>    <span class="w">DELETE</span> <span class="w">this</span><span class="cm">,</span> <span class="w">key</span></li><li>    <span class="w">CLEAR</span> <span class="w">this</span></li><li>    <span class="w">EXISTS</span> <span class="w">this</span><span class="cm">,</span> <span class="w">key</span></li><li>    <span class="w">FIRSTKEY</span> <span class="w">this</span></li><li>    <span class="w">NEXTKEY</span> <span class="w">this</span><span class="cm">,</span> <span class="w">lastkey</span></li><li>    <span class="w">SCALAR</span> <span class="w">this</span></li><li>    DESTROY <span class="w">this</span></li><li>    <span class="w">UNTIE</span> <span class="w">this</span></li></ol></pre><p>A class implementing an ordinary array should have the following methods:</p>
<pre class="verbatim"><ol><li>    <span class="w">TIEARRAY</span> <span class="w">classname</span><span class="cm">,</span> <span class="w">LIST</span></li><li>    <span class="w">FETCH</span> <span class="w">this</span><span class="cm">,</span> <span class="w">key</span></li><li>    <span class="w">STORE</span> <span class="w">this</span><span class="cm">,</span> <span class="w">key</span><span class="cm">,</span> <span class="w">value</span></li><li>    <span class="w">FETCHSIZE</span> <span class="w">this</span></li><li>    <span class="w">STORESIZE</span> <span class="w">this</span><span class="cm">,</span> <span class="w">count</span></li><li>    <span class="w">CLEAR</span> <span class="w">this</span></li><li>    <span class="w">PUSH</span> <span class="w">this</span><span class="cm">,</span> <span class="w">LIST</span></li><li>    <span class="w">POP</span> <span class="w">this</span></li><li>    <span class="w">SHIFT</span> <span class="w">this</span></li><li>    <span class="w">UNSHIFT</span> <span class="w">this</span><span class="cm">,</span> <span class="w">LIST</span></li><li>    <span class="w">SPLICE</span> <span class="w">this</span><span class="cm">,</span> <span class="w">offset</span><span class="cm">,</span> <a class="l_k" href="../functions/length.html">length</a><span class="cm">,</span> <span class="w">LIST</span></li><li>    <span class="w">EXTEND</span> <span class="w">this</span><span class="cm">,</span> <span class="w">count</span></li><li>    DESTROY <span class="w">this</span></li><li>    <span class="w">UNTIE</span> <span class="w">this</span></li></ol></pre><p>A class implementing a file handle should have the following methods:</p>
<pre class="verbatim"><ol><li>    TIEHANDLE classname, LIST</li><li>    READ this, scalar, length, offset</li><li>    READLINE this</li><li>    GETC this</li><li>    WRITE this, scalar, length, offset</li><li>    PRINT this, LIST</li><li>    PRINTF this, format, LIST</li><li>    BINMODE this</li><li>    EOF this</li><li>    FILENO this</li><li>    SEEK this, position, whence</li><li>    TELL this</li><li>    OPEN this, mode, LIST</li><li>    CLOSE this</li><li>    DESTROY this</li><li>    UNTIE this</li></ol></pre><p>A class implementing a scalar should have the following methods:</p>
<pre class="verbatim"><ol><li>    <span class="w">TIESCALAR</span> <span class="w">classname</span><span class="cm">,</span> <span class="w">LIST</span></li><li>    <span class="w">FETCH</span> <span class="w">this</span><span class="cm">,</span></li><li>    <span class="w">STORE</span> <span class="w">this</span><span class="cm">,</span> <span class="w">value</span></li><li>    DESTROY <span class="w">this</span></li><li>    <span class="w">UNTIE</span> <span class="w">this</span></li></ol></pre><p>Not all methods indicated above need be implemented.  See <a href="../perltie.html">perltie</a>,
<a href="../Tie/Hash.html">Tie::Hash</a>, <a href="../Tie/Array.html">Tie::Array</a>, <a href="../Tie/Scalar.html">Tie::Scalar</a>, and <a href="../Tie/Handle.html">Tie::Handle</a>.</p>
<p>Unlike <code class="inline"><a class="l_k" href="../functions/dbmopen.html">dbmopen</a></code>, the <code class="inline"><a class="l_k" href="../functions/tie.html">tie</a></code> function will not use or require a module
for you--you need to do that explicitly yourself.  See <a href="../DB_File.html">DB_File</a>
or the <i>Config</i> module for interesting <code class="inline"><a class="l_k" href="../functions/tie.html">tie</a></code> implementations.</p>
<p>For further details see <a href="../perltie.html">perltie</a>, <a href="#tied-VARIABLE">"tied VARIABLE"</a>.
-----

function tied
variable|
Returns a reference to the object underlying VARIABLE (the same value
that was originally returned by the <code class="inline"><a class="l_k" href="../functions/tie.html">tie</a></code> call that bound the variable
to a package.)  Returns the undefined value if VARIABLE isn&#39;t tied to a
package.
-----

function time
Returns the number of non-leap seconds since whatever time the system
considers to be the epoch, suitable for feeding to <code class="inline"><a class="l_k" href="../functions/gmtime.html">gmtime</a></code> and
<code class="inline"><a class="l_k" href="../functions/localtime.html">localtime</a></code>. On most systems the epoch is 00:00:00 UTC, January 1, 1970;
a prominent exception being Mac OS Classic which uses 00:00:00, January 1,
1904 in the current local time zone for its epoch.</p>
<p>For measuring time in better granularity than one second,
you may use either the <a href="../Time/HiRes.html">Time::HiRes</a> module (from CPAN, and starting from
Perl 5.8 part of the standard distribution), or if you have
gettimeofday(2), you may be able to use the <code class="inline"><a class="l_k" href="../functions/syscall.html">syscall</a></code> interface of Perl.
See <a href="../perlfaq8.html">perlfaq8</a> for details.</p>
<p>For date and time processing look at the many related modules on CPAN.
For a comprehensive date and time representation look at the
<a href="http://search.cpan.org/perldoc/DateTime">DateTime</a> module.
-----

function times
Returns a four-element list giving the user and system times, in
seconds, for this process and the children of this process.</p>
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$user</span><span class="cm">,</span><span class="i">$system</span><span class="cm">,</span><span class="i">$cuser</span><span class="cm">,</span><span class="i">$csystem</span><span class="s">)</span> = <a class="l_k" href="../functions/times.html">times</a><span class="sc">;</span></li></ol></pre><p>In scalar context, <code class="inline"><a class="l_k" href="../functions/times.html">times</a></code> returns <code class="inline"><span class="i">$user</span></code>
.</p>
<p>Note that times for children are included only after they terminate.
-----

function tr
///|
The transliteration operator.  Same as <code class="inline"><a class="l_k" href="../functions/y.html">y///</a></code>.  See
<a href="../perlop.html#Quote-and-Quote-like-Operators">"Quote and Quote-like Operators" in perlop</a>.
-----

function truncate
filehandle|
length|
Truncates the file opened on FILEHANDLE, or named by EXPR, to the
specified length.  Produces a fatal error if truncate isn&#39;t implemented
on your system.  Returns true if successful, the undefined value
otherwise.</p>
<p>The behavior is undefined if LENGTH is greater than the length of the
file.</p>
<p>The position in the file of FILEHANDLE is left unchanged.  You may want to
call <a href="../functions/seek.html">seek</a> before writing to the file.
-----

function truncate
expr|
length|
Truncates the file opened on FILEHANDLE, or named by EXPR, to the
specified length.  Produces a fatal error if truncate isn&#39;t implemented
on your system.  Returns true if successful, the undefined value
otherwise.</p>
<p>The behavior is undefined if LENGTH is greater than the length of the
file.</p>
<p>The position in the file of FILEHANDLE is left unchanged.  You may want to
call <a href="../functions/seek.html">seek</a> before writing to the file.
-----

function uc
expr|
Returns an uppercased version of EXPR.  This is the internal function
implementing the <code class="inline">\<span class="w">U</span></code>
 escape in double-quoted strings.  Respects
current LC_CTYPE locale if <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">locale</span></code>
 in force.  See <a href="../perllocale.html">perllocale</a>
and <a href="../perlunicode.html">perlunicode</a> for more details about locale and Unicode support.
It does not attempt to do titlecase mapping on initial letters.  See
<code class="inline"><a class="l_k" href="../functions/ucfirst.html">ucfirst</a></code> for that.</p>
<p>If EXPR is omitted, uses <code class="inline"><span class="i">$_</span></code>
.
-----

function uc
Returns an uppercased version of EXPR.  This is the internal function
implementing the <code class="inline">\<span class="w">U</span></code>
 escape in double-quoted strings.  Respects
current LC_CTYPE locale if <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">locale</span></code>
 in force.  See <a href="../perllocale.html">perllocale</a>
and <a href="../perlunicode.html">perlunicode</a> for more details about locale and Unicode support.
It does not attempt to do titlecase mapping on initial letters.  See
<code class="inline"><a class="l_k" href="../functions/ucfirst.html">ucfirst</a></code> for that.</p>
<p>If EXPR is omitted, uses <code class="inline"><span class="i">$_</span></code>
.
-----

function ucfirst
expr|
Returns the value of EXPR with the first character in uppercase
(titlecase in Unicode).  This is the internal function implementing
the <code class="inline">\<span class="w">u</span></code>
 escape in double-quoted strings.  Respects current LC_CTYPE
locale if <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">locale</span></code>
 in force.  See <a href="../perllocale.html">perllocale</a> and <a href="../perlunicode.html">perlunicode</a>
for more details about locale and Unicode support.</p>
<p>If EXPR is omitted, uses <code class="inline"><span class="i">$_</span></code>
.
-----

function ucfirst
Returns the value of EXPR with the first character in uppercase
(titlecase in Unicode).  This is the internal function implementing
the <code class="inline">\<span class="w">u</span></code>
 escape in double-quoted strings.  Respects current LC_CTYPE
locale if <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">locale</span></code>
 in force.  See <a href="../perllocale.html">perllocale</a> and <a href="../perlunicode.html">perlunicode</a>
for more details about locale and Unicode support.</p>
<p>If EXPR is omitted, uses <code class="inline"><span class="i">$_</span></code>
.
-----

function umask
expr|
Sets the umask for the process to EXPR and returns the previous value.
If EXPR is omitted, merely returns the current umask.</p>
<p>The Unix permission <code class="inline"><span class="w">rwxr</span>-x---</code>
 is represented as three sets of three
bits, or three octal digits: <code class="inline"><span class="n">0750</span></code>
 (the leading 0 indicates octal
and isn&#39;t one of the digits).  The <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code> value is such a number
representing disabled permissions bits.  The permission (or &quot;mode&quot;)
values you pass <code class="inline"><a class="l_k" href="../functions/mkdir.html">mkdir</a></code> or <code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code> are modified by your umask, so
even if you tell <code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code> to create a file with permissions <code class="inline"><span class="n">0777</span></code>
,
if your umask is <code class="inline"><span class="n">0022</span></code>
 then the file will actually be created with
permissions <code class="inline"><span class="n">0755</span></code>
.  If your <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code> were <code class="inline"><span class="n">0027</span></code>
 (group can&#39;t
write; others can&#39;t read, write, or execute), then passing
<code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code> <code class="inline"><span class="n">0666</span></code>
 would create a file with mode <code class="inline"><span class="n">0640</span></code>
 (<code class="inline"><span class="n">0666</span> &amp;~
<span class="n">027</span></code>
 is <code class="inline"><span class="n">0640</span></code>
).</p>
<p>Here&#39;s some advice: supply a creation mode of <code class="inline"><span class="n">0666</span></code>
 for regular
files (in <code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code>) and one of <code class="inline"><span class="n">0777</span></code>
 for directories (in
<code class="inline"><a class="l_k" href="../functions/mkdir.html">mkdir</a></code>) and executable files.  This gives users the freedom of
choice: if they want protected files, they might choose process umasks
of <code class="inline"><span class="n">022</span></code>
, <code class="inline"><span class="n">027</span></code>
, or even the particularly antisocial mask of <code class="inline"><span class="n">077</span></code>
.
Programs should rarely if ever make policy decisions better left to
the user.  The exception to this is when writing files that should be
kept private: mail files, web browser cookies, <i>.rhosts</i> files, and
so on.</p>
<p>If umask(2) is not implemented on your system and you are trying to
restrict access for <i>yourself</i> (i.e., (EXPR &amp; 0700) &gt; 0), produces a
fatal error at run time.  If umask(2) is not implemented and you are
not trying to restrict access for yourself, returns <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>.</p>
<p>Remember that a umask is a number, usually given in octal; it is <i>not</i> a
string of octal digits.  See also <a href="../functions/oct.html">"oct"</a>, if all you have is a string.
-----

function umask
Sets the umask for the process to EXPR and returns the previous value.
If EXPR is omitted, merely returns the current umask.</p>
<p>The Unix permission <code class="inline"><span class="w">rwxr</span>-x---</code>
 is represented as three sets of three
bits, or three octal digits: <code class="inline"><span class="n">0750</span></code>
 (the leading 0 indicates octal
and isn&#39;t one of the digits).  The <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code> value is such a number
representing disabled permissions bits.  The permission (or &quot;mode&quot;)
values you pass <code class="inline"><a class="l_k" href="../functions/mkdir.html">mkdir</a></code> or <code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code> are modified by your umask, so
even if you tell <code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code> to create a file with permissions <code class="inline"><span class="n">0777</span></code>
,
if your umask is <code class="inline"><span class="n">0022</span></code>
 then the file will actually be created with
permissions <code class="inline"><span class="n">0755</span></code>
.  If your <code class="inline"><a class="l_k" href="../functions/umask.html">umask</a></code> were <code class="inline"><span class="n">0027</span></code>
 (group can&#39;t
write; others can&#39;t read, write, or execute), then passing
<code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code> <code class="inline"><span class="n">0666</span></code>
 would create a file with mode <code class="inline"><span class="n">0640</span></code>
 (<code class="inline"><span class="n">0666</span> &amp;~
<span class="n">027</span></code>
 is <code class="inline"><span class="n">0640</span></code>
).</p>
<p>Here&#39;s some advice: supply a creation mode of <code class="inline"><span class="n">0666</span></code>
 for regular
files (in <code class="inline"><a class="l_k" href="../functions/sysopen.html">sysopen</a></code>) and one of <code class="inline"><span class="n">0777</span></code>
 for directories (in
<code class="inline"><a class="l_k" href="../functions/mkdir.html">mkdir</a></code>) and executable files.  This gives users the freedom of
choice: if they want protected files, they might choose process umasks
of <code class="inline"><span class="n">022</span></code>
, <code class="inline"><span class="n">027</span></code>
, or even the particularly antisocial mask of <code class="inline"><span class="n">077</span></code>
.
Programs should rarely if ever make policy decisions better left to
the user.  The exception to this is when writing files that should be
kept private: mail files, web browser cookies, <i>.rhosts</i> files, and
so on.</p>
<p>If umask(2) is not implemented on your system and you are trying to
restrict access for <i>yourself</i> (i.e., (EXPR &amp; 0700) &gt; 0), produces a
fatal error at run time.  If umask(2) is not implemented and you are
not trying to restrict access for yourself, returns <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>.</p>
<p>Remember that a umask is a number, usually given in octal; it is <i>not</i> a
string of octal digits.  See also <a href="../functions/oct.html">"oct"</a>, if all you have is a string.
-----

function undef
expr|
Undefines the value of EXPR, which must be an lvalue.  Use only on a
scalar value, an array (using <code class="inline"><span class="i">@</span></code>
), a hash (using <code class="inline"><span class="i">%</span></code>
), a subroutine
(using <code class="inline"><span class="i">&amp;</span></code>
), or a typeglob (using <code class="inline"><span class="i">*</span></code>
).  (Saying <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a> <span class="i">$hash</span>{<span class="i">$key</span>}</code>

will probably not do what you expect on most predefined variables or
DBM list values, so don&#39;t do that; see <a href="../functions/delete.html">delete</a>.)  Always returns the
undefined value.  You can omit the EXPR, in which case nothing is
undefined, but you still get an undefined value that you could, for
instance, return from a subroutine, assign to a variable or pass as a
parameter.  Examples:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">$foo</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">$bar</span>{<span class="q">&#39;blurfl&#39;</span>}<span class="sc">;</span>      <span class="c"># Compare to: delete $bar{&#39;blurfl&#39;};</span></li><li>    <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">@ary</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">%hash</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">&amp;mysub</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">*xyz</span><span class="sc">;</span>       <span class="c"># destroys $xyz, @xyz, %xyz, &amp;xyz, etc.</span></li><li>    <a class="l_k" href="../functions/return.html">return</a> <span class="s">(</span><a class="l_k" href="../functions/wantarray.html">wantarray</a> ? <span class="s">(</span><a class="l_k" href="../functions/undef.html">undef</a><span class="cm">,</span> <span class="i">$errmsg</span><span class="s">)</span> <span class="co">:</span> <a class="l_k" href="../functions/undef.html">undef</a><span class="s">)</span> if <span class="i">$they_blew_it</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/select.html">select</a> <a class="l_k" href="../functions/undef.html">undef</a><span class="cm">,</span> <a class="l_k" href="../functions/undef.html">undef</a><span class="cm">,</span> <a class="l_k" href="../functions/undef.html">undef</a><span class="cm">,</span> <span class="n">0.25</span><span class="sc">;</span></li><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span> <span class="i">$b</span><span class="cm">,</span> <a class="l_k" href="../functions/undef.html">undef</a><span class="cm">,</span> <span class="i">$c</span><span class="s">)</span> = <span class="i">&amp;foo</span><span class="sc">;</span>       <span class="c"># Ignore third value returned</span></li></ol></pre><p>Note that this is a unary operator, not a list operator.
-----

function undef
Undefines the value of EXPR, which must be an lvalue.  Use only on a
scalar value, an array (using <code class="inline"><span class="i">@</span></code>
), a hash (using <code class="inline"><span class="i">%</span></code>
), a subroutine
(using <code class="inline"><span class="i">&amp;</span></code>
), or a typeglob (using <code class="inline"><span class="i">*</span></code>
).  (Saying <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a> <span class="i">$hash</span>{<span class="i">$key</span>}</code>

will probably not do what you expect on most predefined variables or
DBM list values, so don&#39;t do that; see <a href="../functions/delete.html">delete</a>.)  Always returns the
undefined value.  You can omit the EXPR, in which case nothing is
undefined, but you still get an undefined value that you could, for
instance, return from a subroutine, assign to a variable or pass as a
parameter.  Examples:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">$foo</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">$bar</span>{<span class="q">&#39;blurfl&#39;</span>}<span class="sc">;</span>      <span class="c"># Compare to: delete $bar{&#39;blurfl&#39;};</span></li><li>    <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">@ary</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">%hash</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">&amp;mysub</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/undef.html">undef</a> <span class="i">*xyz</span><span class="sc">;</span>       <span class="c"># destroys $xyz, @xyz, %xyz, &amp;xyz, etc.</span></li><li>    <a class="l_k" href="../functions/return.html">return</a> <span class="s">(</span><a class="l_k" href="../functions/wantarray.html">wantarray</a> ? <span class="s">(</span><a class="l_k" href="../functions/undef.html">undef</a><span class="cm">,</span> <span class="i">$errmsg</span><span class="s">)</span> <span class="co">:</span> <a class="l_k" href="../functions/undef.html">undef</a><span class="s">)</span> if <span class="i">$they_blew_it</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/select.html">select</a> <a class="l_k" href="../functions/undef.html">undef</a><span class="cm">,</span> <a class="l_k" href="../functions/undef.html">undef</a><span class="cm">,</span> <a class="l_k" href="../functions/undef.html">undef</a><span class="cm">,</span> <span class="n">0.25</span><span class="sc">;</span></li><li>    <span class="s">(</span><span class="i">$a</span><span class="cm">,</span> <span class="i">$b</span><span class="cm">,</span> <a class="l_k" href="../functions/undef.html">undef</a><span class="cm">,</span> <span class="i">$c</span><span class="s">)</span> = <span class="i">&amp;foo</span><span class="sc">;</span>       <span class="c"># Ignore third value returned</span></li></ol></pre><p>Note that this is a unary operator, not a list operator.
-----

function unlink
list|
Deletes a list of files.  Returns the number of files successfully
deleted.</p>
<pre class="verbatim"><ol><li>    <span class="i">$cnt</span> = <a class="l_k" href="../functions/unlink.html">unlink</a> <span class="q">&#39;a&#39;</span><span class="cm">,</span> <span class="q">&#39;b&#39;</span><span class="cm">,</span> <span class="q">&#39;c&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/unlink.html">unlink</a> <span class="i">@goners</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/unlink.html">unlink</a> <span class="q">&lt;*.bak&gt;</span><span class="sc">;</span></li></ol></pre><p>Note: <code class="inline"><a class="l_k" href="../functions/unlink.html">unlink</a></code> will not attempt to delete directories unless you are superuser
and the <b>-U</b> flag is supplied to Perl.  Even if these conditions are
met, be warned that unlinking a directory can inflict damage on your
filesystem.  Finally, using <code class="inline"><a class="l_k" href="../functions/unlink.html">unlink</a></code> on directories is not supported on 
many operating systems.  Use <code class="inline"><a class="l_k" href="../functions/rmdir.html">rmdir</a></code> instead.</p>
<p>If LIST is omitted, uses <code class="inline"><span class="i">$_</span></code>
.
-----

function unlink
Deletes a list of files.  Returns the number of files successfully
deleted.</p>
<pre class="verbatim"><ol><li>    <span class="i">$cnt</span> = <a class="l_k" href="../functions/unlink.html">unlink</a> <span class="q">&#39;a&#39;</span><span class="cm">,</span> <span class="q">&#39;b&#39;</span><span class="cm">,</span> <span class="q">&#39;c&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/unlink.html">unlink</a> <span class="i">@goners</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/unlink.html">unlink</a> <span class="q">&lt;*.bak&gt;</span><span class="sc">;</span></li></ol></pre><p>Note: <code class="inline"><a class="l_k" href="../functions/unlink.html">unlink</a></code> will not attempt to delete directories unless you are superuser
and the <b>-U</b> flag is supplied to Perl.  Even if these conditions are
met, be warned that unlinking a directory can inflict damage on your
filesystem.  Finally, using <code class="inline"><a class="l_k" href="../functions/unlink.html">unlink</a></code> on directories is not supported on 
many operating systems.  Use <code class="inline"><a class="l_k" href="../functions/rmdir.html">rmdir</a></code> instead.</p>
<p>If LIST is omitted, uses <code class="inline"><span class="i">$_</span></code>
.
-----

function unpack
template|
expr|
<code class="inline"><a class="l_k" href="../functions/unpack.html">unpack</a></code> does the reverse of <code class="inline"><a class="l_k" href="../functions/pack.html">pack</a></code>: it takes a string
and expands it out into a list of values.
(In scalar context, it returns merely the first value produced.)</p>
<p>If EXPR is omitted, unpacks the <code class="inline"><span class="i">$_</span></code>
 string.</p>
<p>The string is broken into chunks described by the TEMPLATE.  Each chunk
is converted separately to a value.  Typically, either the string is a result
of <code class="inline"><a class="l_k" href="../functions/pack.html">pack</a></code>, or the characters of the string represent a C structure of some
kind.</p>
<p>The TEMPLATE has the same format as in the <code class="inline"><a class="l_k" href="../functions/pack.html">pack</a></code> function.
Here&#39;s a subroutine that does substring:</p>
<pre class="verbatim"><ol><li><a name="substr"></a>    sub <span class="m">substr</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a><span class="s">(</span><span class="i">$what</span><span class="cm">,</span><span class="i">$where</span><span class="cm">,</span><span class="i">$howmuch</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;x$where a$howmuch&quot;</span><span class="cm">,</span> <span class="i">$what</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>and then there&#39;s</p>
<pre class="verbatim"><ol><li><a name="ordinal"></a>    sub <span class="m">ordinal</span> <span class="s">{</span> <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;W&quot;</span><span class="cm">,</span><span class="i">$_</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span> <span class="s">}</span> <span class="c"># same as ord()</span></li></ol></pre><p>In addition to fields allowed in pack(), you may prefix a field with
a %&lt;number&gt; to indicate that
you want a &lt;number&gt;-bit checksum of the items instead of the items
themselves.  Default is a 16-bit checksum.  Checksum is calculated by
summing numeric values of expanded values (for string fields the sum of
<code class="inline"><a class="l_k" href="../functions/ord.html">ord($char)</a></code> is taken, for bit fields the sum of zeroes and ones).</p>
<p>For example, the following
computes the same number as the System V sum program:</p>
<pre class="verbatim"><ol><li>    <span class="i">$checksum</span> = <a class="l_k" href="../functions/do.html">do</a> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/local.html">local</a> <span class="i">$/</span><span class="sc">;</span>  <span class="c"># slurp!</span></li><li>	<a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;%32W*&quot;</span><span class="cm">,</span>&lt;&gt;<span class="s">)</span> % <span class="n">65535</span><span class="sc">;</span></li><li>    <span class="s">}</span><span class="sc">;</span></li></ol></pre><p>The following efficiently counts the number of set bits in a bit vector:</p>
<pre class="verbatim"><ol><li>    <span class="i">$setbits</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;%32b*&quot;</span><span class="cm">,</span> <span class="i">$selectmask</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline"><span class="w">p</span></code>
 and <code class="inline"><span class="w">P</span></code>
 formats should be used with care.  Since Perl
has no way of checking whether the value passed to <code class="inline"><a class="l_k" href="../functions/unpack.html">unpack()</a></code>
corresponds to a valid memory location, passing a pointer value that&#39;s
not known to be valid is likely to have disastrous consequences.</p>
<p>If there are more pack codes or if the repeat count of a field or a group
is larger than what the remainder of the input string allows, the result
is not well defined: in some cases, the repeat count is decreased, or
<code class="inline"><a class="l_k" href="../functions/unpack.html">unpack()</a></code> will produce null strings or zeroes, or terminate with an
error. If the input string is longer than one described by the TEMPLATE,
the rest is ignored.</p>
<p>See <a href="../functions/pack.html">"pack"</a> for more examples and notes.
-----

function unpack
template|
<code class="inline"><a class="l_k" href="../functions/unpack.html">unpack</a></code> does the reverse of <code class="inline"><a class="l_k" href="../functions/pack.html">pack</a></code>: it takes a string
and expands it out into a list of values.
(In scalar context, it returns merely the first value produced.)</p>
<p>If EXPR is omitted, unpacks the <code class="inline"><span class="i">$_</span></code>
 string.</p>
<p>The string is broken into chunks described by the TEMPLATE.  Each chunk
is converted separately to a value.  Typically, either the string is a result
of <code class="inline"><a class="l_k" href="../functions/pack.html">pack</a></code>, or the characters of the string represent a C structure of some
kind.</p>
<p>The TEMPLATE has the same format as in the <code class="inline"><a class="l_k" href="../functions/pack.html">pack</a></code> function.
Here&#39;s a subroutine that does substring:</p>
<pre class="verbatim"><ol><li><a name="substr"></a>    sub <span class="m">substr</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/my.html">my</a><span class="s">(</span><span class="i">$what</span><span class="cm">,</span><span class="i">$where</span><span class="cm">,</span><span class="i">$howmuch</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;x$where a$howmuch&quot;</span><span class="cm">,</span> <span class="i">$what</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>and then there&#39;s</p>
<pre class="verbatim"><ol><li><a name="ordinal"></a>    sub <span class="m">ordinal</span> <span class="s">{</span> <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;W&quot;</span><span class="cm">,</span><span class="i">$_</span>[<span class="n">0</span>]<span class="s">)</span><span class="sc">;</span> <span class="s">}</span> <span class="c"># same as ord()</span></li></ol></pre><p>In addition to fields allowed in pack(), you may prefix a field with
a %&lt;number&gt; to indicate that
you want a &lt;number&gt;-bit checksum of the items instead of the items
themselves.  Default is a 16-bit checksum.  Checksum is calculated by
summing numeric values of expanded values (for string fields the sum of
<code class="inline"><a class="l_k" href="../functions/ord.html">ord($char)</a></code> is taken, for bit fields the sum of zeroes and ones).</p>
<p>For example, the following
computes the same number as the System V sum program:</p>
<pre class="verbatim"><ol><li>    <span class="i">$checksum</span> = <a class="l_k" href="../functions/do.html">do</a> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/local.html">local</a> <span class="i">$/</span><span class="sc">;</span>  <span class="c"># slurp!</span></li><li>	<a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;%32W*&quot;</span><span class="cm">,</span>&lt;&gt;<span class="s">)</span> % <span class="n">65535</span><span class="sc">;</span></li><li>    <span class="s">}</span><span class="sc">;</span></li></ol></pre><p>The following efficiently counts the number of set bits in a bit vector:</p>
<pre class="verbatim"><ol><li>    <span class="i">$setbits</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;%32b*&quot;</span><span class="cm">,</span> <span class="i">$selectmask</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline"><span class="w">p</span></code>
 and <code class="inline"><span class="w">P</span></code>
 formats should be used with care.  Since Perl
has no way of checking whether the value passed to <code class="inline"><a class="l_k" href="../functions/unpack.html">unpack()</a></code>
corresponds to a valid memory location, passing a pointer value that&#39;s
not known to be valid is likely to have disastrous consequences.</p>
<p>If there are more pack codes or if the repeat count of a field or a group
is larger than what the remainder of the input string allows, the result
is not well defined: in some cases, the repeat count is decreased, or
<code class="inline"><a class="l_k" href="../functions/unpack.html">unpack()</a></code> will produce null strings or zeroes, or terminate with an
error. If the input string is longer than one described by the TEMPLATE,
the rest is ignored.</p>
<p>See <a href="../functions/pack.html">"pack"</a> for more examples and notes.
-----

function unshift
array|
list|
Does the opposite of a <code class="inline"><a class="l_k" href="../functions/shift.html">shift</a></code>.  Or the opposite of a <code class="inline"><a class="l_k" href="../functions/push.html">push</a></code>,
depending on how you look at it.  Prepends list to the front of the
array, and returns the new number of elements in the array.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/unshift.html">unshift</a><span class="s">(</span><span class="i">@ARGV</span><span class="cm">,</span> <span class="q">&#39;-e&#39;</span><span class="s">)</span> unless <span class="i">$ARGV</span>[<span class="n">0</span>] =~ <span class="q">/^-/</span><span class="sc">;</span></li></ol></pre><p>Note the LIST is prepended whole, not one element at a time, so the
prepended elements stay in the same order.  Use <code class="inline"><a class="l_k" href="../functions/reverse.html">reverse</a></code> to do the
reverse.
-----

function untie
variable|
Breaks the binding between a variable and a package.  (See <code class="inline"><a class="l_k" href="../functions/tie.html">tie</a></code>.)
Has no effect if the variable is not tied.
-----

function use
module version list|
Imports some semantics into the current package from the named module,
generally by aliasing certain subroutine or variable names into your
package.  It is exactly equivalent to</p>
<pre class="verbatim"><ol><li>    BEGIN <span class="s">{</span> <a class="l_k" href="../functions/require.html">require</a> <span class="w">Module</span><span class="sc">;</span> <span class="w">Module</span><span class="w">-&gt;import</span><span class="s">(</span> <span class="w">LIST</span> <span class="s">)</span><span class="sc">;</span> <span class="s">}</span></li></ol></pre><p>except that Module <i>must</i> be a bareword.</p>
<p>In the peculiar <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">VERSION</span></code>
 form, VERSION may be either a numeric
argument such as 5.006, which will be compared to <code class="inline"><span class="i">$]</span></code>
, or a literal of
the form v5.6.1, which will be compared to <code class="inline"><span class="i">$^V</span></code>
 (aka $PERL_VERSION).  A
fatal error is produced if VERSION is greater than the version of the
current Perl interpreter; Perl will not attempt to parse the rest of the
file.  Compare with <a href="../functions/require.html">"require"</a>, which can do a similar check at run time.
Symmetrically, <code class="inline"><a class="l_k" href="../functions/no.html">no</a> <span class="w">VERSION</span></code>
 allows you to specify that you want a version
of perl older than the specified one.</p>
<p>Specifying VERSION as a literal of the form v5.6.1 should generally be
avoided, because it leads to misleading error messages under earlier
versions of Perl (that is, prior to 5.6.0) that do not support this
syntax.  The equivalent numeric version should be used instead.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="v">v5.6.1</span><span class="sc">;</span>		<span class="c"># compile time version check</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="v">5.6.1</span><span class="sc">;</span>		<span class="c"># ditto</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="n">5.006_001</span><span class="sc">;</span>	<span class="c"># ditto; preferred for backwards compatibility</span></li></ol></pre><p>This is often useful if you need to check the current Perl version before
<code class="inline"><a class="l_k" href="../functions/use.html">use</a></code>ing library modules that won&#39;t work with older versions of Perl.
(We try not to do this more than we have to.)</p>
<p>Also, if the specified perl version is greater than or equal to 5.9.5,
<code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">VERSION</span></code>
 will also load the <code class="inline"><span class="w">feature</span></code>
 pragma and enable all
features available in the requested version.  See <a href="../feature.html">feature</a>.</p>
<p>The <code class="inline">BEGIN</code>
 forces the <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> and <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> to happen at compile time.  The
<code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> makes sure the module is loaded into memory if it hasn&#39;t been
yet.  The <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> is not a builtin--it&#39;s just an ordinary static method
call into the <code class="inline"><span class="w">Module</span></code>
 package to tell the module to import the list of
features back into the current package.  The module can implement its
<code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> method any way it likes, though most modules just choose to
derive their <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> method via inheritance from the <code class="inline"><span class="w">Exporter</span></code>
 class that
is defined in the <code class="inline"><span class="w">Exporter</span></code>
 module.  See <a href="../Exporter.html">Exporter</a>.  If no <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code>
method can be found then the call is skipped, even if there is an AUTOLOAD
method.</p>
<p>If you do not want to call the package&#39;s <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> method (for instance,
to stop your namespace from being altered), explicitly supply the empty list:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Module</span> <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>That is exactly equivalent to</p>
<pre class="verbatim"><ol><li>    BEGIN <span class="s">{</span> <a class="l_k" href="../functions/require.html">require</a> <span class="w">Module</span> <span class="s">}</span></li></ol></pre><p>If the VERSION argument is present between Module and LIST, then the
<code class="inline"><a class="l_k" href="../functions/use.html">use</a></code> will call the VERSION method in class Module with the given
version as an argument.  The default VERSION method, inherited from
the UNIVERSAL class, croaks if the given version is larger than the
value of the variable <code class="inline"><span class="i">$Module::VERSION</span></code>
.</p>
<p>Again, there is a distinction between omitting LIST (<code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> called
with no arguments) and an explicit empty LIST <code class="inline"><span class="s">(</span><span class="s">)</span></code>
 (<code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> not
called).  Note that there is no comma after VERSION!</p>
<p>Because this is a wide-open interface, pragmas (compiler directives)
are also implemented this way.  Currently implemented pragmas are:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">constant</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">diagnostics</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">integer</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">sigtrap</span>  <span class="q">qw(SEGV BUS)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">strict</span>   <span class="q">qw(subs vars refs)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">subs</span>     <span class="q">qw(afunc blurfl)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">warnings</span> <span class="q">qw(all)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <a class="l_k" href="../functions/sort.html">sort</a>     <span class="q">qw(stable _quicksort _mergesort)</span><span class="sc">;</span></li></ol></pre><p>Some of these pseudo-modules import semantics into the current
block scope (like <code class="inline"><span class="w">strict</span></code>
 or <code class="inline"><span class="w">integer</span></code>
, unlike ordinary modules,
which import symbols into the current package (which are effective
through the end of the file).</p>
<p>There&#39;s a corresponding <code class="inline"><a class="l_k" href="../functions/no.html">no</a></code> command that unimports meanings imported
by <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code>, i.e., it calls <code class="inline"><span class="w">unimport</span> <span class="w">Module</span> <span class="w">LIST</span></code>
 instead of <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code>.
It behaves exactly as <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> does with respect to VERSION, an
omitted LIST, empty LIST, or no unimport method being found.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/no.html">no</a> <span class="w">integer</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/no.html">no</a> <span class="w">strict</span> <span class="q">&#39;refs&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/no.html">no</a> <span class="w">warnings</span><span class="sc">;</span></li></ol></pre><p>See <a href="../perlmodlib.html">perlmodlib</a> for a list of standard modules and pragmas.  See <a href="../perlrun.html">perlrun</a>
for the <code class="inline">-M</code>
 and <code class="inline">-m</code> command-line options to perl that give <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code>
functionality from the command-line.
-----

function use
module version|
Imports some semantics into the current package from the named module,
generally by aliasing certain subroutine or variable names into your
package.  It is exactly equivalent to</p>
<pre class="verbatim"><ol><li>    BEGIN <span class="s">{</span> <a class="l_k" href="../functions/require.html">require</a> <span class="w">Module</span><span class="sc">;</span> <span class="w">Module</span><span class="w">-&gt;import</span><span class="s">(</span> <span class="w">LIST</span> <span class="s">)</span><span class="sc">;</span> <span class="s">}</span></li></ol></pre><p>except that Module <i>must</i> be a bareword.</p>
<p>In the peculiar <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">VERSION</span></code>
 form, VERSION may be either a numeric
argument such as 5.006, which will be compared to <code class="inline"><span class="i">$]</span></code>
, or a literal of
the form v5.6.1, which will be compared to <code class="inline"><span class="i">$^V</span></code>
 (aka $PERL_VERSION).  A
fatal error is produced if VERSION is greater than the version of the
current Perl interpreter; Perl will not attempt to parse the rest of the
file.  Compare with <a href="../functions/require.html">"require"</a>, which can do a similar check at run time.
Symmetrically, <code class="inline"><a class="l_k" href="../functions/no.html">no</a> <span class="w">VERSION</span></code>
 allows you to specify that you want a version
of perl older than the specified one.</p>
<p>Specifying VERSION as a literal of the form v5.6.1 should generally be
avoided, because it leads to misleading error messages under earlier
versions of Perl (that is, prior to 5.6.0) that do not support this
syntax.  The equivalent numeric version should be used instead.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="v">v5.6.1</span><span class="sc">;</span>		<span class="c"># compile time version check</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="v">5.6.1</span><span class="sc">;</span>		<span class="c"># ditto</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="n">5.006_001</span><span class="sc">;</span>	<span class="c"># ditto; preferred for backwards compatibility</span></li></ol></pre><p>This is often useful if you need to check the current Perl version before
<code class="inline"><a class="l_k" href="../functions/use.html">use</a></code>ing library modules that won&#39;t work with older versions of Perl.
(We try not to do this more than we have to.)</p>
<p>Also, if the specified perl version is greater than or equal to 5.9.5,
<code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">VERSION</span></code>
 will also load the <code class="inline"><span class="w">feature</span></code>
 pragma and enable all
features available in the requested version.  See <a href="../feature.html">feature</a>.</p>
<p>The <code class="inline">BEGIN</code>
 forces the <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> and <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> to happen at compile time.  The
<code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> makes sure the module is loaded into memory if it hasn&#39;t been
yet.  The <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> is not a builtin--it&#39;s just an ordinary static method
call into the <code class="inline"><span class="w">Module</span></code>
 package to tell the module to import the list of
features back into the current package.  The module can implement its
<code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> method any way it likes, though most modules just choose to
derive their <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> method via inheritance from the <code class="inline"><span class="w">Exporter</span></code>
 class that
is defined in the <code class="inline"><span class="w">Exporter</span></code>
 module.  See <a href="../Exporter.html">Exporter</a>.  If no <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code>
method can be found then the call is skipped, even if there is an AUTOLOAD
method.</p>
<p>If you do not want to call the package&#39;s <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> method (for instance,
to stop your namespace from being altered), explicitly supply the empty list:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Module</span> <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>That is exactly equivalent to</p>
<pre class="verbatim"><ol><li>    BEGIN <span class="s">{</span> <a class="l_k" href="../functions/require.html">require</a> <span class="w">Module</span> <span class="s">}</span></li></ol></pre><p>If the VERSION argument is present between Module and LIST, then the
<code class="inline"><a class="l_k" href="../functions/use.html">use</a></code> will call the VERSION method in class Module with the given
version as an argument.  The default VERSION method, inherited from
the UNIVERSAL class, croaks if the given version is larger than the
value of the variable <code class="inline"><span class="i">$Module::VERSION</span></code>
.</p>
<p>Again, there is a distinction between omitting LIST (<code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> called
with no arguments) and an explicit empty LIST <code class="inline"><span class="s">(</span><span class="s">)</span></code>
 (<code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> not
called).  Note that there is no comma after VERSION!</p>
<p>Because this is a wide-open interface, pragmas (compiler directives)
are also implemented this way.  Currently implemented pragmas are:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">constant</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">diagnostics</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">integer</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">sigtrap</span>  <span class="q">qw(SEGV BUS)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">strict</span>   <span class="q">qw(subs vars refs)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">subs</span>     <span class="q">qw(afunc blurfl)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">warnings</span> <span class="q">qw(all)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <a class="l_k" href="../functions/sort.html">sort</a>     <span class="q">qw(stable _quicksort _mergesort)</span><span class="sc">;</span></li></ol></pre><p>Some of these pseudo-modules import semantics into the current
block scope (like <code class="inline"><span class="w">strict</span></code>
 or <code class="inline"><span class="w">integer</span></code>
, unlike ordinary modules,
which import symbols into the current package (which are effective
through the end of the file).</p>
<p>There&#39;s a corresponding <code class="inline"><a class="l_k" href="../functions/no.html">no</a></code> command that unimports meanings imported
by <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code>, i.e., it calls <code class="inline"><span class="w">unimport</span> <span class="w">Module</span> <span class="w">LIST</span></code>
 instead of <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code>.
It behaves exactly as <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> does with respect to VERSION, an
omitted LIST, empty LIST, or no unimport method being found.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/no.html">no</a> <span class="w">integer</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/no.html">no</a> <span class="w">strict</span> <span class="q">&#39;refs&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/no.html">no</a> <span class="w">warnings</span><span class="sc">;</span></li></ol></pre><p>See <a href="../perlmodlib.html">perlmodlib</a> for a list of standard modules and pragmas.  See <a href="../perlrun.html">perlrun</a>
for the <code class="inline">-M</code>
 and <code class="inline">-m</code> command-line options to perl that give <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code>
functionality from the command-line.
-----

function use
module list|
Imports some semantics into the current package from the named module,
generally by aliasing certain subroutine or variable names into your
package.  It is exactly equivalent to</p>
<pre class="verbatim"><ol><li>    BEGIN <span class="s">{</span> <a class="l_k" href="../functions/require.html">require</a> <span class="w">Module</span><span class="sc">;</span> <span class="w">Module</span><span class="w">-&gt;import</span><span class="s">(</span> <span class="w">LIST</span> <span class="s">)</span><span class="sc">;</span> <span class="s">}</span></li></ol></pre><p>except that Module <i>must</i> be a bareword.</p>
<p>In the peculiar <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">VERSION</span></code>
 form, VERSION may be either a numeric
argument such as 5.006, which will be compared to <code class="inline"><span class="i">$]</span></code>
, or a literal of
the form v5.6.1, which will be compared to <code class="inline"><span class="i">$^V</span></code>
 (aka $PERL_VERSION).  A
fatal error is produced if VERSION is greater than the version of the
current Perl interpreter; Perl will not attempt to parse the rest of the
file.  Compare with <a href="../functions/require.html">"require"</a>, which can do a similar check at run time.
Symmetrically, <code class="inline"><a class="l_k" href="../functions/no.html">no</a> <span class="w">VERSION</span></code>
 allows you to specify that you want a version
of perl older than the specified one.</p>
<p>Specifying VERSION as a literal of the form v5.6.1 should generally be
avoided, because it leads to misleading error messages under earlier
versions of Perl (that is, prior to 5.6.0) that do not support this
syntax.  The equivalent numeric version should be used instead.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="v">v5.6.1</span><span class="sc">;</span>		<span class="c"># compile time version check</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="v">5.6.1</span><span class="sc">;</span>		<span class="c"># ditto</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="n">5.006_001</span><span class="sc">;</span>	<span class="c"># ditto; preferred for backwards compatibility</span></li></ol></pre><p>This is often useful if you need to check the current Perl version before
<code class="inline"><a class="l_k" href="../functions/use.html">use</a></code>ing library modules that won&#39;t work with older versions of Perl.
(We try not to do this more than we have to.)</p>
<p>Also, if the specified perl version is greater than or equal to 5.9.5,
<code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">VERSION</span></code>
 will also load the <code class="inline"><span class="w">feature</span></code>
 pragma and enable all
features available in the requested version.  See <a href="../feature.html">feature</a>.</p>
<p>The <code class="inline">BEGIN</code>
 forces the <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> and <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> to happen at compile time.  The
<code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> makes sure the module is loaded into memory if it hasn&#39;t been
yet.  The <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> is not a builtin--it&#39;s just an ordinary static method
call into the <code class="inline"><span class="w">Module</span></code>
 package to tell the module to import the list of
features back into the current package.  The module can implement its
<code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> method any way it likes, though most modules just choose to
derive their <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> method via inheritance from the <code class="inline"><span class="w">Exporter</span></code>
 class that
is defined in the <code class="inline"><span class="w">Exporter</span></code>
 module.  See <a href="../Exporter.html">Exporter</a>.  If no <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code>
method can be found then the call is skipped, even if there is an AUTOLOAD
method.</p>
<p>If you do not want to call the package&#39;s <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> method (for instance,
to stop your namespace from being altered), explicitly supply the empty list:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Module</span> <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>That is exactly equivalent to</p>
<pre class="verbatim"><ol><li>    BEGIN <span class="s">{</span> <a class="l_k" href="../functions/require.html">require</a> <span class="w">Module</span> <span class="s">}</span></li></ol></pre><p>If the VERSION argument is present between Module and LIST, then the
<code class="inline"><a class="l_k" href="../functions/use.html">use</a></code> will call the VERSION method in class Module with the given
version as an argument.  The default VERSION method, inherited from
the UNIVERSAL class, croaks if the given version is larger than the
value of the variable <code class="inline"><span class="i">$Module::VERSION</span></code>
.</p>
<p>Again, there is a distinction between omitting LIST (<code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> called
with no arguments) and an explicit empty LIST <code class="inline"><span class="s">(</span><span class="s">)</span></code>
 (<code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> not
called).  Note that there is no comma after VERSION!</p>
<p>Because this is a wide-open interface, pragmas (compiler directives)
are also implemented this way.  Currently implemented pragmas are:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">constant</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">diagnostics</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">integer</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">sigtrap</span>  <span class="q">qw(SEGV BUS)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">strict</span>   <span class="q">qw(subs vars refs)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">subs</span>     <span class="q">qw(afunc blurfl)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">warnings</span> <span class="q">qw(all)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <a class="l_k" href="../functions/sort.html">sort</a>     <span class="q">qw(stable _quicksort _mergesort)</span><span class="sc">;</span></li></ol></pre><p>Some of these pseudo-modules import semantics into the current
block scope (like <code class="inline"><span class="w">strict</span></code>
 or <code class="inline"><span class="w">integer</span></code>
, unlike ordinary modules,
which import symbols into the current package (which are effective
through the end of the file).</p>
<p>There&#39;s a corresponding <code class="inline"><a class="l_k" href="../functions/no.html">no</a></code> command that unimports meanings imported
by <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code>, i.e., it calls <code class="inline"><span class="w">unimport</span> <span class="w">Module</span> <span class="w">LIST</span></code>
 instead of <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code>.
It behaves exactly as <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> does with respect to VERSION, an
omitted LIST, empty LIST, or no unimport method being found.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/no.html">no</a> <span class="w">integer</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/no.html">no</a> <span class="w">strict</span> <span class="q">&#39;refs&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/no.html">no</a> <span class="w">warnings</span><span class="sc">;</span></li></ol></pre><p>See <a href="../perlmodlib.html">perlmodlib</a> for a list of standard modules and pragmas.  See <a href="../perlrun.html">perlrun</a>
for the <code class="inline">-M</code>
 and <code class="inline">-m</code> command-line options to perl that give <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code>
functionality from the command-line.
-----

function use
module|
Imports some semantics into the current package from the named module,
generally by aliasing certain subroutine or variable names into your
package.  It is exactly equivalent to</p>
<pre class="verbatim"><ol><li>    BEGIN <span class="s">{</span> <a class="l_k" href="../functions/require.html">require</a> <span class="w">Module</span><span class="sc">;</span> <span class="w">Module</span><span class="w">-&gt;import</span><span class="s">(</span> <span class="w">LIST</span> <span class="s">)</span><span class="sc">;</span> <span class="s">}</span></li></ol></pre><p>except that Module <i>must</i> be a bareword.</p>
<p>In the peculiar <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">VERSION</span></code>
 form, VERSION may be either a numeric
argument such as 5.006, which will be compared to <code class="inline"><span class="i">$]</span></code>
, or a literal of
the form v5.6.1, which will be compared to <code class="inline"><span class="i">$^V</span></code>
 (aka $PERL_VERSION).  A
fatal error is produced if VERSION is greater than the version of the
current Perl interpreter; Perl will not attempt to parse the rest of the
file.  Compare with <a href="../functions/require.html">"require"</a>, which can do a similar check at run time.
Symmetrically, <code class="inline"><a class="l_k" href="../functions/no.html">no</a> <span class="w">VERSION</span></code>
 allows you to specify that you want a version
of perl older than the specified one.</p>
<p>Specifying VERSION as a literal of the form v5.6.1 should generally be
avoided, because it leads to misleading error messages under earlier
versions of Perl (that is, prior to 5.6.0) that do not support this
syntax.  The equivalent numeric version should be used instead.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="v">v5.6.1</span><span class="sc">;</span>		<span class="c"># compile time version check</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="v">5.6.1</span><span class="sc">;</span>		<span class="c"># ditto</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="n">5.006_001</span><span class="sc">;</span>	<span class="c"># ditto; preferred for backwards compatibility</span></li></ol></pre><p>This is often useful if you need to check the current Perl version before
<code class="inline"><a class="l_k" href="../functions/use.html">use</a></code>ing library modules that won&#39;t work with older versions of Perl.
(We try not to do this more than we have to.)</p>
<p>Also, if the specified perl version is greater than or equal to 5.9.5,
<code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">VERSION</span></code>
 will also load the <code class="inline"><span class="w">feature</span></code>
 pragma and enable all
features available in the requested version.  See <a href="../feature.html">feature</a>.</p>
<p>The <code class="inline">BEGIN</code>
 forces the <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> and <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> to happen at compile time.  The
<code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> makes sure the module is loaded into memory if it hasn&#39;t been
yet.  The <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> is not a builtin--it&#39;s just an ordinary static method
call into the <code class="inline"><span class="w">Module</span></code>
 package to tell the module to import the list of
features back into the current package.  The module can implement its
<code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> method any way it likes, though most modules just choose to
derive their <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> method via inheritance from the <code class="inline"><span class="w">Exporter</span></code>
 class that
is defined in the <code class="inline"><span class="w">Exporter</span></code>
 module.  See <a href="../Exporter.html">Exporter</a>.  If no <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code>
method can be found then the call is skipped, even if there is an AUTOLOAD
method.</p>
<p>If you do not want to call the package&#39;s <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> method (for instance,
to stop your namespace from being altered), explicitly supply the empty list:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Module</span> <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>That is exactly equivalent to</p>
<pre class="verbatim"><ol><li>    BEGIN <span class="s">{</span> <a class="l_k" href="../functions/require.html">require</a> <span class="w">Module</span> <span class="s">}</span></li></ol></pre><p>If the VERSION argument is present between Module and LIST, then the
<code class="inline"><a class="l_k" href="../functions/use.html">use</a></code> will call the VERSION method in class Module with the given
version as an argument.  The default VERSION method, inherited from
the UNIVERSAL class, croaks if the given version is larger than the
value of the variable <code class="inline"><span class="i">$Module::VERSION</span></code>
.</p>
<p>Again, there is a distinction between omitting LIST (<code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> called
with no arguments) and an explicit empty LIST <code class="inline"><span class="s">(</span><span class="s">)</span></code>
 (<code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> not
called).  Note that there is no comma after VERSION!</p>
<p>Because this is a wide-open interface, pragmas (compiler directives)
are also implemented this way.  Currently implemented pragmas are:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">constant</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">diagnostics</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">integer</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">sigtrap</span>  <span class="q">qw(SEGV BUS)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">strict</span>   <span class="q">qw(subs vars refs)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">subs</span>     <span class="q">qw(afunc blurfl)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">warnings</span> <span class="q">qw(all)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <a class="l_k" href="../functions/sort.html">sort</a>     <span class="q">qw(stable _quicksort _mergesort)</span><span class="sc">;</span></li></ol></pre><p>Some of these pseudo-modules import semantics into the current
block scope (like <code class="inline"><span class="w">strict</span></code>
 or <code class="inline"><span class="w">integer</span></code>
, unlike ordinary modules,
which import symbols into the current package (which are effective
through the end of the file).</p>
<p>There&#39;s a corresponding <code class="inline"><a class="l_k" href="../functions/no.html">no</a></code> command that unimports meanings imported
by <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code>, i.e., it calls <code class="inline"><span class="w">unimport</span> <span class="w">Module</span> <span class="w">LIST</span></code>
 instead of <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code>.
It behaves exactly as <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> does with respect to VERSION, an
omitted LIST, empty LIST, or no unimport method being found.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/no.html">no</a> <span class="w">integer</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/no.html">no</a> <span class="w">strict</span> <span class="q">&#39;refs&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/no.html">no</a> <span class="w">warnings</span><span class="sc">;</span></li></ol></pre><p>See <a href="../perlmodlib.html">perlmodlib</a> for a list of standard modules and pragmas.  See <a href="../perlrun.html">perlrun</a>
for the <code class="inline">-M</code>
 and <code class="inline">-m</code> command-line options to perl that give <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code>
functionality from the command-line.
-----

function use
version|
Imports some semantics into the current package from the named module,
generally by aliasing certain subroutine or variable names into your
package.  It is exactly equivalent to</p>
<pre class="verbatim"><ol><li>    BEGIN <span class="s">{</span> <a class="l_k" href="../functions/require.html">require</a> <span class="w">Module</span><span class="sc">;</span> <span class="w">Module</span><span class="w">-&gt;import</span><span class="s">(</span> <span class="w">LIST</span> <span class="s">)</span><span class="sc">;</span> <span class="s">}</span></li></ol></pre><p>except that Module <i>must</i> be a bareword.</p>
<p>In the peculiar <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">VERSION</span></code>
 form, VERSION may be either a numeric
argument such as 5.006, which will be compared to <code class="inline"><span class="i">$]</span></code>
, or a literal of
the form v5.6.1, which will be compared to <code class="inline"><span class="i">$^V</span></code>
 (aka $PERL_VERSION).  A
fatal error is produced if VERSION is greater than the version of the
current Perl interpreter; Perl will not attempt to parse the rest of the
file.  Compare with <a href="../functions/require.html">"require"</a>, which can do a similar check at run time.
Symmetrically, <code class="inline"><a class="l_k" href="../functions/no.html">no</a> <span class="w">VERSION</span></code>
 allows you to specify that you want a version
of perl older than the specified one.</p>
<p>Specifying VERSION as a literal of the form v5.6.1 should generally be
avoided, because it leads to misleading error messages under earlier
versions of Perl (that is, prior to 5.6.0) that do not support this
syntax.  The equivalent numeric version should be used instead.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="v">v5.6.1</span><span class="sc">;</span>		<span class="c"># compile time version check</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="v">5.6.1</span><span class="sc">;</span>		<span class="c"># ditto</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="n">5.006_001</span><span class="sc">;</span>	<span class="c"># ditto; preferred for backwards compatibility</span></li></ol></pre><p>This is often useful if you need to check the current Perl version before
<code class="inline"><a class="l_k" href="../functions/use.html">use</a></code>ing library modules that won&#39;t work with older versions of Perl.
(We try not to do this more than we have to.)</p>
<p>Also, if the specified perl version is greater than or equal to 5.9.5,
<code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">VERSION</span></code>
 will also load the <code class="inline"><span class="w">feature</span></code>
 pragma and enable all
features available in the requested version.  See <a href="../feature.html">feature</a>.</p>
<p>The <code class="inline">BEGIN</code>
 forces the <code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> and <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> to happen at compile time.  The
<code class="inline"><a class="l_k" href="../functions/require.html">require</a></code> makes sure the module is loaded into memory if it hasn&#39;t been
yet.  The <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> is not a builtin--it&#39;s just an ordinary static method
call into the <code class="inline"><span class="w">Module</span></code>
 package to tell the module to import the list of
features back into the current package.  The module can implement its
<code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> method any way it likes, though most modules just choose to
derive their <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> method via inheritance from the <code class="inline"><span class="w">Exporter</span></code>
 class that
is defined in the <code class="inline"><span class="w">Exporter</span></code>
 module.  See <a href="../Exporter.html">Exporter</a>.  If no <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code>
method can be found then the call is skipped, even if there is an AUTOLOAD
method.</p>
<p>If you do not want to call the package&#39;s <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> method (for instance,
to stop your namespace from being altered), explicitly supply the empty list:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">Module</span> <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>That is exactly equivalent to</p>
<pre class="verbatim"><ol><li>    BEGIN <span class="s">{</span> <a class="l_k" href="../functions/require.html">require</a> <span class="w">Module</span> <span class="s">}</span></li></ol></pre><p>If the VERSION argument is present between Module and LIST, then the
<code class="inline"><a class="l_k" href="../functions/use.html">use</a></code> will call the VERSION method in class Module with the given
version as an argument.  The default VERSION method, inherited from
the UNIVERSAL class, croaks if the given version is larger than the
value of the variable <code class="inline"><span class="i">$Module::VERSION</span></code>
.</p>
<p>Again, there is a distinction between omitting LIST (<code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> called
with no arguments) and an explicit empty LIST <code class="inline"><span class="s">(</span><span class="s">)</span></code>
 (<code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> not
called).  Note that there is no comma after VERSION!</p>
<p>Because this is a wide-open interface, pragmas (compiler directives)
are also implemented this way.  Currently implemented pragmas are:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">constant</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">diagnostics</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">integer</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">sigtrap</span>  <span class="q">qw(SEGV BUS)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">strict</span>   <span class="q">qw(subs vars refs)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">subs</span>     <span class="q">qw(afunc blurfl)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">warnings</span> <span class="q">qw(all)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/use.html">use</a> <a class="l_k" href="../functions/sort.html">sort</a>     <span class="q">qw(stable _quicksort _mergesort)</span><span class="sc">;</span></li></ol></pre><p>Some of these pseudo-modules import semantics into the current
block scope (like <code class="inline"><span class="w">strict</span></code>
 or <code class="inline"><span class="w">integer</span></code>
, unlike ordinary modules,
which import symbols into the current package (which are effective
through the end of the file).</p>
<p>There&#39;s a corresponding <code class="inline"><a class="l_k" href="../functions/no.html">no</a></code> command that unimports meanings imported
by <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code>, i.e., it calls <code class="inline"><span class="w">unimport</span> <span class="w">Module</span> <span class="w">LIST</span></code>
 instead of <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code>.
It behaves exactly as <code class="inline"><a class="l_k" href="../functions/import.html">import</a></code> does with respect to VERSION, an
omitted LIST, empty LIST, or no unimport method being found.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/no.html">no</a> <span class="w">integer</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/no.html">no</a> <span class="w">strict</span> <span class="q">&#39;refs&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/no.html">no</a> <span class="w">warnings</span><span class="sc">;</span></li></ol></pre><p>See <a href="../perlmodlib.html">perlmodlib</a> for a list of standard modules and pragmas.  See <a href="../perlrun.html">perlrun</a>
for the <code class="inline">-M</code>
 and <code class="inline">-m</code> command-line options to perl that give <code class="inline"><a class="l_k" href="../functions/use.html">use</a></code>
functionality from the command-line.
-----

function utime
list|
Changes the access and modification times on each file of a list of
files.  The first two elements of the list must be the NUMERICAL access
and modification times, in that order.  Returns the number of files
successfully changed.  The inode change time of each file is set
to the current time.  For example, this code has the same effect as the
Unix touch(1) command when the files <i>already exist</i> and belong to
the user running the program:</p>
<pre class="verbatim"><ol><li>    <span class="c">#!/usr/bin/perl</span></li><li>    <span class="i">$atime</span> = <span class="i">$mtime</span> = <a class="l_k" href="../functions/time.html">time</a><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/utime.html">utime</a> <span class="i">$atime</span><span class="cm">,</span> <span class="i">$mtime</span><span class="cm">,</span> <span class="i">@ARGV</span><span class="sc">;</span></li></ol></pre><p>Since perl 5.7.2, if the first two elements of the list are <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>, then
the utime(2) function in the C library will be called with a null second
argument. On most systems, this will set the file&#39;s access and
modification times to the current time (i.e. equivalent to the example
above) and will even work on other users&#39; files where you have write
permission:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/utime.html">utime</a> <a class="l_k" href="../functions/undef.html">undef</a><span class="cm">,</span> <a class="l_k" href="../functions/undef.html">undef</a><span class="cm">,</span> <span class="i">@ARGV</span><span class="sc">;</span></li></ol></pre><p>Under NFS this will use the time of the NFS server, not the time of
the local machine.  If there is a time synchronization problem, the
NFS server and local machine will have different times.  The Unix
touch(1) command will in fact normally use this form instead of the
one shown in the first example.</p>
<p>Note that only passing one of the first two elements as <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code> will
be equivalent of passing it as 0 and will not have the same effect as
described when they are both <code class="inline"><a class="l_k" href="../functions/undef.html">undef</a></code>.  This case will also trigger an
uninitialized warning.</p>
<p>On systems that support futimes, you might pass file handles among the
files.  On systems that don&#39;t support futimes, passing file handles
produces a fatal error at run time.  The file handles must be passed
as globs or references to be recognized.  Barewords are considered
file names.
-----

function values
hash|
Returns a list consisting of all the values of the named hash.
(In a scalar context, returns the number of values.)</p>
<p>The values are returned in an apparently random order.  The actual
random order is subject to change in future versions of perl, but it
is guaranteed to be the same order as either the <code class="inline"><a class="l_k" href="../functions/keys.html">keys</a></code> or <code class="inline"><a class="l_k" href="../functions/each.html">each</a></code>
function would produce on the same (unmodified) hash.  Since Perl
5.8.1 the ordering is different even between different runs of Perl
for security reasons (see <a href="../perlsec.html#Algorithmic-Complexity-Attacks">"Algorithmic Complexity Attacks" in perlsec</a>).</p>
<p>As a side effect, calling values() resets the HASH&#39;s internal iterator,
see <a href="../functions/each.html">"each"</a>. (In particular, calling values() in void context resets
the iterator with no other overhead.)</p>
<p>Note that the values are not copied, which means modifying them will
modify the contents of the hash:</p>
<pre class="verbatim"><ol><li>    for <span class="s">(</span><a class="l_k" href="../functions/values.html">values</a> <span class="i">%hash</span><span class="s">)</span> 	    <span class="s">{</span> <span class="q">s/foo/bar/g</span> <span class="s">}</span>   <span class="c"># modifies %hash values</span></li><li>    for <span class="s">(</span><span class="i">@hash</span>{<a class="l_k" href="../functions/keys.html">keys</a> <span class="i">%hash</span>}<span class="s">)</span> <span class="s">{</span> <span class="q">s/foo/bar/g</span> <span class="s">}</span>   <span class="c"># same</span></li></ol></pre><p>See also <code class="inline"><a class="l_k" href="../functions/keys.html">keys</a></code>, <code class="inline"><a class="l_k" href="../functions/each.html">each</a></code>, and <code class="inline"><a class="l_k" href="../functions/sort.html">sort</a></code>.
-----

function vec
expr|
offset|
bits|
Treats the string in EXPR as a bit vector made up of elements of
width BITS, and returns the value of the element specified by OFFSET
as an unsigned integer.  BITS therefore specifies the number of bits
that are reserved for each element in the bit vector.  This must
be a power of two from 1 to 32 (or 64, if your platform supports
that).</p>
<p>If BITS is 8, &quot;elements&quot; coincide with bytes of the input string.</p>
<p>If BITS is 16 or more, bytes of the input string are grouped into chunks
of size BITS/8, and each group is converted to a number as with
pack()/unpack() with big-endian formats <code class="inline"><span class="w">n</span></code>
/<code class="inline"><span class="w">N</span></code>
 (and analogously
for BITS==64).  See <a href="http://search.cpan.org/perldoc/&quot;pack&quot;">pack</a> for details.</p>
<p>If bits is 4 or less, the string is broken into bytes, then the bits
of each byte are broken into 8/BITS groups.  Bits of a byte are
numbered in a little-endian-ish way, as in <code class="inline"><span class="n">0x01</span></code>
, <code class="inline"><span class="n">0x02</span></code>
,
<code class="inline"><span class="n">0x04</span></code>
, <code class="inline"><span class="n">0x08</span></code>
, <code class="inline"><span class="n">0x10</span></code>
, <code class="inline"><span class="n">0x20</span></code>
, <code class="inline"><span class="n">0x40</span></code>
, <code class="inline"><span class="n">0x80</span></code>
.  For example,
breaking the single input byte <code class="inline"><a class="l_k" href="../functions/chr.html">chr(0x36)</a></code> into two groups gives a list
<code class="inline"><span class="s">(</span><span class="n">0x6</span><span class="cm">,</span> <span class="n">0x3</span><span class="s">)</span></code>
; breaking it into 4 groups gives <code class="inline"><span class="s">(</span><span class="n">0x2</span><span class="cm">,</span> <span class="n">0x1</span><span class="cm">,</span> <span class="n">0x3</span><span class="cm">,</span> <span class="n">0x0</span><span class="s">)</span></code>
.</p>
<p><code class="inline"><a class="l_k" href="../functions/vec.html">vec</a></code> may also be assigned to, in which case parentheses are needed
to give the expression the correct precedence as in</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/vec.html">vec</a><span class="s">(</span><span class="i">$image</span><span class="cm">,</span> <span class="i">$max_x</span> * <span class="i">$x</span> + <span class="i">$y</span><span class="cm">,</span> <span class="n">8</span><span class="s">)</span> = <span class="n">3</span><span class="sc">;</span></li></ol></pre><p>If the selected element is outside the string, the value 0 is returned.
If an element off the end of the string is written to, Perl will first
extend the string with sufficiently many zero bytes.   It is an error
to try to write off the beginning of the string (i.e. negative OFFSET).</p>
<p>If the string happens to be encoded as UTF-8 internally (and thus has
the UTF8 flag set), this is ignored by <code class="inline"><a class="l_k" href="../functions/vec.html">vec</a></code>, and it operates on the
internal byte string, not the conceptual character string, even if you
only have characters with values less than 256.</p>
<p>Strings created with <code class="inline"><a class="l_k" href="../functions/vec.html">vec</a></code> can also be manipulated with the logical
operators <code class="inline">|</code>, <code class="inline"><span class="i">&amp;</span></code>
, <code class="inline">^</code>, and <code class="inline">~</code>
.  These operators will assume a bit
vector operation is desired when both operands are strings.
See <a href="../perlop.html#Bitwise-String-Operators">"Bitwise String Operators" in perlop</a>.</p>
<p>The following code will build up an ASCII string saying <code class="inline"><span class="q">&#39;PerlPerlPerl&#39;</span></code>
.
The comments show the string after each step.  Note that this code works
in the same way on big-endian or little-endian machines.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$foo</span> = <span class="q">&#39;&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/vec.html">vec</a><span class="s">(</span><span class="i">$foo</span><span class="cm">,</span>  <span class="n">0</span><span class="cm">,</span> <span class="n">32</span><span class="s">)</span> = <span class="n">0x5065726C</span><span class="sc">;</span>	<span class="c"># &#39;Perl&#39;</span></li><li></li><li>    <span class="c"># $foo eq &quot;Perl&quot; eq &quot;\x50\x65\x72\x6C&quot;, 32 bits</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <a class="l_k" href="../functions/vec.html">vec</a><span class="s">(</span><span class="i">$foo</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span> <span class="n">8</span><span class="s">)</span><span class="sc">;</span>		<span class="c"># prints 80 == 0x50 == ord(&#39;P&#39;)</span></li><li></li><li>    <a class="l_k" href="../functions/vec.html">vec</a><span class="s">(</span><span class="i">$foo</span><span class="cm">,</span>  <span class="n">2</span><span class="cm">,</span> <span class="n">16</span><span class="s">)</span> = <span class="n">0x5065</span><span class="sc">;</span>		<span class="c"># &#39;PerlPe&#39;</span></li><li>    <a class="l_k" href="../functions/vec.html">vec</a><span class="s">(</span><span class="i">$foo</span><span class="cm">,</span>  <span class="n">3</span><span class="cm">,</span> <span class="n">16</span><span class="s">)</span> = <span class="n">0x726C</span><span class="sc">;</span>		<span class="c"># &#39;PerlPerl&#39;</span></li><li>    <a class="l_k" href="../functions/vec.html">vec</a><span class="s">(</span><span class="i">$foo</span><span class="cm">,</span>  <span class="n">8</span><span class="cm">,</span>  <span class="n">8</span><span class="s">)</span> = <span class="n">0x50</span><span class="sc">;</span>		<span class="c"># &#39;PerlPerlP&#39;</span></li><li>    <a class="l_k" href="../functions/vec.html">vec</a><span class="s">(</span><span class="i">$foo</span><span class="cm">,</span>  <span class="n">9</span><span class="cm">,</span>  <span class="n">8</span><span class="s">)</span> = <span class="n">0x65</span><span class="sc">;</span>		<span class="c"># &#39;PerlPerlPe&#39;</span></li><li>    <a class="l_k" href="../functions/vec.html">vec</a><span class="s">(</span><span class="i">$foo</span><span class="cm">,</span> <span class="n">20</span><span class="cm">,</span>  <span class="n">4</span><span class="s">)</span> = <span class="n">2</span><span class="sc">;</span>		<span class="c"># &#39;PerlPerlPe&#39;   . &quot;\x02&quot;</span></li><li>    <a class="l_k" href="../functions/vec.html">vec</a><span class="s">(</span><span class="i">$foo</span><span class="cm">,</span> <span class="n">21</span><span class="cm">,</span>  <span class="n">4</span><span class="s">)</span> = <span class="n">7</span><span class="sc">;</span>		<span class="c"># &#39;PerlPerlPer&#39;</span></li><li>                                        <span class="c"># &#39;r&#39; is &quot;\x72&quot;</span></li><li>    <a class="l_k" href="../functions/vec.html">vec</a><span class="s">(</span><span class="i">$foo</span><span class="cm">,</span> <span class="n">45</span><span class="cm">,</span>  <span class="n">2</span><span class="s">)</span> = <span class="n">3</span><span class="sc">;</span>		<span class="c"># &#39;PerlPerlPer&#39;  . &quot;\x0c&quot;</span></li><li>    <a class="l_k" href="../functions/vec.html">vec</a><span class="s">(</span><span class="i">$foo</span><span class="cm">,</span> <span class="n">93</span><span class="cm">,</span>  <span class="n">1</span><span class="s">)</span> = <span class="n">1</span><span class="sc">;</span>		<span class="c"># &#39;PerlPerlPer&#39;  . &quot;\x2c&quot;</span></li><li>    <a class="l_k" href="../functions/vec.html">vec</a><span class="s">(</span><span class="i">$foo</span><span class="cm">,</span> <span class="n">94</span><span class="cm">,</span>  <span class="n">1</span><span class="s">)</span> = <span class="n">1</span><span class="sc">;</span>		<span class="c"># &#39;PerlPerlPerl&#39;</span></li><li>                                        <span class="c"># &#39;l&#39; is &quot;\x6c&quot;</span></li></ol></pre><p>To transform a bit vector into a string or list of 0&#39;s and 1&#39;s, use these:</p>
<pre class="verbatim"><ol><li>    <span class="i">$bits</span> = <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;b*&quot;</span><span class="cm">,</span> <span class="i">$vector</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">@bits</span> = <a class="l_k" href="../functions/split.html">split</a><span class="s">(</span><span class="q">//</span><span class="cm">,</span> <a class="l_k" href="../functions/unpack.html">unpack</a><span class="s">(</span><span class="q">&quot;b*&quot;</span><span class="cm">,</span> <span class="i">$vector</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If you know the exact length in bits, it can be used in place of the <code class="inline"><span class="i">*</span></code>
.</p>
<p>Here is an example to illustrate how the bits actually fall in place:</p>
<pre class="verbatim"><ol><li>    #!/usr/bin/perl -wl</li><li></li><li>    print &lt;&lt;'EOT';</li><li>                                      0         1         2         3</li><li>                       unpack("V",$_) 01234567890123456789012345678901</li><li>    ------------------------------------------------------------------</li><li>    EOT</li><li></li><li>    for $w (0..3) {</li><li>        $width = 2**$w;</li><li>        for ($shift=0; $shift &lt; $width; ++$shift) {</li><li>            for ($off=0; $off &lt; 32/$width; ++$off) {</li><li>                $str = pack("B*", "0"x32);</li><li>                $bits = (1&lt;&lt;$shift);</li><li>                vec($str, $off, $width) = $bits;</li><li>                $res = unpack("b*",$str);</li><li>                $val = unpack("V", $str);</li><li>                write;</li><li>            }</li><li>        }</li><li>    }</li><li></li><li>    format STDOUT =</li><li>    vec($_,@#,@#) = @&lt;&lt; == @######### @&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;</li><li>    $off, $width, $bits, $val, $res</li><li>    .</li><li>    __END__</li></ol></pre><p>Regardless of the machine architecture on which it is run, the above
example should print the following table:</p>
<pre class="verbatim"><ol><li>                                      0         1         2         3</li><li>                       unpack("V",$_) 01234567890123456789012345678901</li><li>    ------------------------------------------------------------------</li><li>    vec($_, 0, 1) = 1   ==          1 10000000000000000000000000000000</li><li>    vec($_, 1, 1) = 1   ==          2 01000000000000000000000000000000</li><li>    vec($_, 2, 1) = 1   ==          4 00100000000000000000000000000000</li><li>    vec($_, 3, 1) = 1   ==          8 00010000000000000000000000000000</li><li>    vec($_, 4, 1) = 1   ==         16 00001000000000000000000000000000</li><li>    vec($_, 5, 1) = 1   ==         32 00000100000000000000000000000000</li><li>    vec($_, 6, 1) = 1   ==         64 00000010000000000000000000000000</li><li>    vec($_, 7, 1) = 1   ==        128 00000001000000000000000000000000</li><li>    vec($_, 8, 1) = 1   ==        256 00000000100000000000000000000000</li><li>    vec($_, 9, 1) = 1   ==        512 00000000010000000000000000000000</li><li>    vec($_,10, 1) = 1   ==       1024 00000000001000000000000000000000</li><li>    vec($_,11, 1) = 1   ==       2048 00000000000100000000000000000000</li><li>    vec($_,12, 1) = 1   ==       4096 00000000000010000000000000000000</li><li>    vec($_,13, 1) = 1   ==       8192 00000000000001000000000000000000</li><li>    vec($_,14, 1) = 1   ==      16384 00000000000000100000000000000000</li><li>    vec($_,15, 1) = 1   ==      32768 00000000000000010000000000000000</li><li>    vec($_,16, 1) = 1   ==      65536 00000000000000001000000000000000</li><li>    vec($_,17, 1) = 1   ==     131072 00000000000000000100000000000000</li><li>    vec($_,18, 1) = 1   ==     262144 00000000000000000010000000000000</li><li>    vec($_,19, 1) = 1   ==     524288 00000000000000000001000000000000</li><li>    vec($_,20, 1) = 1   ==    1048576 00000000000000000000100000000000</li><li>    vec($_,21, 1) = 1   ==    2097152 00000000000000000000010000000000</li><li>    vec($_,22, 1) = 1   ==    4194304 00000000000000000000001000000000</li><li>    vec($_,23, 1) = 1   ==    8388608 00000000000000000000000100000000</li><li>    vec($_,24, 1) = 1   ==   16777216 00000000000000000000000010000000</li><li>    vec($_,25, 1) = 1   ==   33554432 00000000000000000000000001000000</li><li>    vec($_,26, 1) = 1   ==   67108864 00000000000000000000000000100000</li><li>    vec($_,27, 1) = 1   ==  134217728 00000000000000000000000000010000</li><li>    vec($_,28, 1) = 1   ==  268435456 00000000000000000000000000001000</li><li>    vec($_,29, 1) = 1   ==  536870912 00000000000000000000000000000100</li><li>    vec($_,30, 1) = 1   == 1073741824 00000000000000000000000000000010</li><li>    vec($_,31, 1) = 1   == 2147483648 00000000000000000000000000000001</li><li>    vec($_, 0, 2) = 1   ==          1 10000000000000000000000000000000</li><li>    vec($_, 1, 2) = 1   ==          4 00100000000000000000000000000000</li><li>    vec($_, 2, 2) = 1   ==         16 00001000000000000000000000000000</li><li>    vec($_, 3, 2) = 1   ==         64 00000010000000000000000000000000</li><li>    vec($_, 4, 2) = 1   ==        256 00000000100000000000000000000000</li><li>    vec($_, 5, 2) = 1   ==       1024 00000000001000000000000000000000</li><li>    vec($_, 6, 2) = 1   ==       4096 00000000000010000000000000000000</li><li>    vec($_, 7, 2) = 1   ==      16384 00000000000000100000000000000000</li><li>    vec($_, 8, 2) = 1   ==      65536 00000000000000001000000000000000</li><li>    vec($_, 9, 2) = 1   ==     262144 00000000000000000010000000000000</li><li>    vec($_,10, 2) = 1   ==    1048576 00000000000000000000100000000000</li><li>    vec($_,11, 2) = 1   ==    4194304 00000000000000000000001000000000</li><li>    vec($_,12, 2) = 1   ==   16777216 00000000000000000000000010000000</li><li>    vec($_,13, 2) = 1   ==   67108864 00000000000000000000000000100000</li><li>    vec($_,14, 2) = 1   ==  268435456 00000000000000000000000000001000</li><li>    vec($_,15, 2) = 1   == 1073741824 00000000000000000000000000000010</li><li>    vec($_, 0, 2) = 2   ==          2 01000000000000000000000000000000</li><li>    vec($_, 1, 2) = 2   ==          8 00010000000000000000000000000000</li><li>    vec($_, 2, 2) = 2   ==         32 00000100000000000000000000000000</li><li>    vec($_, 3, 2) = 2   ==        128 00000001000000000000000000000000</li><li>    vec($_, 4, 2) = 2   ==        512 00000000010000000000000000000000</li><li>    vec($_, 5, 2) = 2   ==       2048 00000000000100000000000000000000</li><li>    vec($_, 6, 2) = 2   ==       8192 00000000000001000000000000000000</li><li>    vec($_, 7, 2) = 2   ==      32768 00000000000000010000000000000000</li><li>    vec($_, 8, 2) = 2   ==     131072 00000000000000000100000000000000</li><li>    vec($_, 9, 2) = 2   ==     524288 00000000000000000001000000000000</li><li>    vec($_,10, 2) = 2   ==    2097152 00000000000000000000010000000000</li><li>    vec($_,11, 2) = 2   ==    8388608 00000000000000000000000100000000</li><li>    vec($_,12, 2) = 2   ==   33554432 00000000000000000000000001000000</li><li>    vec($_,13, 2) = 2   ==  134217728 00000000000000000000000000010000</li><li>    vec($_,14, 2) = 2   ==  536870912 00000000000000000000000000000100</li><li>    vec($_,15, 2) = 2   == 2147483648 00000000000000000000000000000001</li><li>    vec($_, 0, 4) = 1   ==          1 10000000000000000000000000000000</li><li>    vec($_, 1, 4) = 1   ==         16 00001000000000000000000000000000</li><li>    vec($_, 2, 4) = 1   ==        256 00000000100000000000000000000000</li><li>    vec($_, 3, 4) = 1   ==       4096 00000000000010000000000000000000</li><li>    vec($_, 4, 4) = 1   ==      65536 00000000000000001000000000000000</li><li>    vec($_, 5, 4) = 1   ==    1048576 00000000000000000000100000000000</li><li>    vec($_, 6, 4) = 1   ==   16777216 00000000000000000000000010000000</li><li>    vec($_, 7, 4) = 1   ==  268435456 00000000000000000000000000001000</li><li>    vec($_, 0, 4) = 2   ==          2 01000000000000000000000000000000</li><li>    vec($_, 1, 4) = 2   ==         32 00000100000000000000000000000000</li><li>    vec($_, 2, 4) = 2   ==        512 00000000010000000000000000000000</li><li>    vec($_, 3, 4) = 2   ==       8192 00000000000001000000000000000000</li><li>    vec($_, 4, 4) = 2   ==     131072 00000000000000000100000000000000</li><li>    vec($_, 5, 4) = 2   ==    2097152 00000000000000000000010000000000</li><li>    vec($_, 6, 4) = 2   ==   33554432 00000000000000000000000001000000</li><li>    vec($_, 7, 4) = 2   ==  536870912 00000000000000000000000000000100</li><li>    vec($_, 0, 4) = 4   ==          4 00100000000000000000000000000000</li><li>    vec($_, 1, 4) = 4   ==         64 00000010000000000000000000000000</li><li>    vec($_, 2, 4) = 4   ==       1024 00000000001000000000000000000000</li><li>    vec($_, 3, 4) = 4   ==      16384 00000000000000100000000000000000</li><li>    vec($_, 4, 4) = 4   ==     262144 00000000000000000010000000000000</li><li>    vec($_, 5, 4) = 4   ==    4194304 00000000000000000000001000000000</li><li>    vec($_, 6, 4) = 4   ==   67108864 00000000000000000000000000100000</li><li>    vec($_, 7, 4) = 4   == 1073741824 00000000000000000000000000000010</li><li>    vec($_, 0, 4) = 8   ==          8 00010000000000000000000000000000</li><li>    vec($_, 1, 4) = 8   ==        128 00000001000000000000000000000000</li><li>    vec($_, 2, 4) = 8   ==       2048 00000000000100000000000000000000</li><li>    vec($_, 3, 4) = 8   ==      32768 00000000000000010000000000000000</li><li>    vec($_, 4, 4) = 8   ==     524288 00000000000000000001000000000000</li><li>    vec($_, 5, 4) = 8   ==    8388608 00000000000000000000000100000000</li><li>    vec($_, 6, 4) = 8   ==  134217728 00000000000000000000000000010000</li><li>    vec($_, 7, 4) = 8   == 2147483648 00000000000000000000000000000001</li><li>    vec($_, 0, 8) = 1   ==          1 10000000000000000000000000000000</li><li>    vec($_, 1, 8) = 1   ==        256 00000000100000000000000000000000</li><li>    vec($_, 2, 8) = 1   ==      65536 00000000000000001000000000000000</li><li>    vec($_, 3, 8) = 1   ==   16777216 00000000000000000000000010000000</li><li>    vec($_, 0, 8) = 2   ==          2 01000000000000000000000000000000</li><li>    vec($_, 1, 8) = 2   ==        512 00000000010000000000000000000000</li><li>    vec($_, 2, 8) = 2   ==     131072 00000000000000000100000000000000</li><li>    vec($_, 3, 8) = 2   ==   33554432 00000000000000000000000001000000</li><li>    vec($_, 0, 8) = 4   ==          4 00100000000000000000000000000000</li><li>    vec($_, 1, 8) = 4   ==       1024 00000000001000000000000000000000</li><li>    vec($_, 2, 8) = 4   ==     262144 00000000000000000010000000000000</li><li>    vec($_, 3, 8) = 4   ==   67108864 00000000000000000000000000100000</li><li>    vec($_, 0, 8) = 8   ==          8 00010000000000000000000000000000</li><li>    vec($_, 1, 8) = 8   ==       2048 00000000000100000000000000000000</li><li>    vec($_, 2, 8) = 8   ==     524288 00000000000000000001000000000000</li><li>    vec($_, 3, 8) = 8   ==  134217728 00000000000000000000000000010000</li><li>    vec($_, 0, 8) = 16  ==         16 00001000000000000000000000000000</li><li>    vec($_, 1, 8) = 16  ==       4096 00000000000010000000000000000000</li><li>    vec($_, 2, 8) = 16  ==    1048576 00000000000000000000100000000000</li><li>    vec($_, 3, 8) = 16  ==  268435456 00000000000000000000000000001000</li><li>    vec($_, 0, 8) = 32  ==         32 00000100000000000000000000000000</li><li>    vec($_, 1, 8) = 32  ==       8192 00000000000001000000000000000000</li><li>    vec($_, 2, 8) = 32  ==    2097152 00000000000000000000010000000000</li><li>    vec($_, 3, 8) = 32  ==  536870912 00000000000000000000000000000100</li><li>    vec($_, 0, 8) = 64  ==         64 00000010000000000000000000000000</li><li>    vec($_, 1, 8) = 64  ==      16384 00000000000000100000000000000000</li><li>    vec($_, 2, 8) = 64  ==    4194304 00000000000000000000001000000000</li><li>    vec($_, 3, 8) = 64  == 1073741824 00000000000000000000000000000010</li><li>    vec($_, 0, 8) = 128 ==        128 00000001000000000000000000000000</li><li>    vec($_, 1, 8) = 128 ==      32768 00000000000000010000000000000000</li><li>    vec($_, 2, 8) = 128 ==    8388608 00000000000000000000000100000000</li><li>    vec($_, 3, 8) = 128 == 2147483648 00000000000000000000000000000001</li></ol><
-----

function wait
Behaves like the wait(2) system call on your system: it waits for a child
process to terminate and returns the pid of the deceased process, or
<code class="inline"><span class="n">-1</span></code>
 if there are no child processes.  The status is returned in <code class="inline"><span class="i">$?</span></code>

and <code class="inline"><span class="i">$</span>{<span class="w">^CHILD_ERROR_NATIVE</span>}</code>
.
Note that a return value of <code class="inline"><span class="n">-1</span></code>
 could mean that child processes are
being automatically reaped, as described in <a href="../perlipc.html">perlipc</a>.
-----

function waitpid
pid|
flags|
Waits for a particular child process to terminate and returns the pid of
the deceased process, or <code class="inline"><span class="n">-1</span></code>
 if there is no such child process.  On some
systems, a value of 0 indicates that there are processes still running.
The status is returned in <code class="inline"><span class="i">$?</span></code>
 and <code class="inline"><span class="i">$</span>{<span class="w">^CHILD_ERROR_NATIVE</span>}</code>
.  If you say</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/use.html">use</a> <span class="w">POSIX</span> <span class="q">&quot;:sys_wait_h&quot;</span><span class="sc">;</span></li><li>    <span class="c">#...</span></li><li>    <a class="l_k" href="../functions/do.html">do</a> <span class="s">{</span></li><li>	<span class="i">$kid</span> = <a class="l_k" href="../functions/waitpid.html">waitpid</a><span class="s">(</span><span class="n">-1</span><span class="cm">,</span> <span class="w">WNOHANG</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span> while <span class="i">$kid</span> &gt; <span class="n">0</span><span class="sc">;</span></li></ol></pre><p>then you can do a non-blocking wait for all pending zombie processes.
Non-blocking wait is available on machines supporting either the
waitpid(2) or wait4(2) system calls.  However, waiting for a particular
pid with FLAGS of <code class="inline"><span class="n">0</span></code>
 is implemented everywhere.  (Perl emulates the
system call by remembering the status values of processes that have
exited but have not been harvested by the Perl script yet.)</p>
<p>Note that on some systems, a return value of <code class="inline"><span class="n">-1</span></code>
 could mean that child
processes are being automatically reaped.  See <a href="../perlipc.html">perlipc</a> for details,
and for other examples.
-----

function wantarray
Returns true if the context of the currently executing subroutine or
<code class="inline"><a class="l_k" href="../functions/eval.html">eval</a></code> is looking for a list value.  Returns false if the context is
looking for a scalar.  Returns the undefined value if the context is
looking for no value (void context).</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/return.html">return</a> unless <a class="l_k" href="../functions/defined.html">defined</a> <a class="l_k" href="../functions/wantarray.html">wantarray</a><span class="sc">;</span>	<span class="c"># don&#39;t bother doing more</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">@a</span> = <span class="i">complex_calculation</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/return.html">return</a> <a class="l_k" href="../functions/wantarray.html">wantarray</a> ? <span class="i">@a</span> <span class="co">:</span> <span class="q">&quot;@a&quot;</span><span class="sc">;</span></li></ol></pre><p><code class="inline"><a class="l_k" href="../functions/wantarray.html">wantarray()</a></code>&#39;s result is unspecified in the top level of a file,
in a <code class="inline">BEGIN</code>
, <code class="inline">UNITCHECK</code>
, <code class="inline">CHECK</code>
, <code class="inline">INIT</code>
 or <code class="inline">END</code>
 block, or
in a <code class="inline">DESTROY</code>
 method.</p>
<p>This function should have been named wantlist() instead.
-----

function warn
list|
Prints the value of LIST to STDERR.  If the last element of LIST does
not end in a newline, it appends the same file/line number text as <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>
does.</p>
<p>If LIST is empty and <code class="inline"><span class="i">$@</span></code>
 already contains a value (typically from a
previous eval) that value is used after appending <code class="inline"><span class="q">&quot;\t...caught&quot;</span></code>

to <code class="inline"><span class="i">$@</span></code>
.  This is useful for staying almost, but not entirely similar to
<code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>.</p>
<p>If <code class="inline"><span class="i">$@</span></code>
 is empty then the string <code class="inline"><span class="q">&quot;Warning: Something&#39;s wrong&quot;</span></code>
 is used.</p>
<p>No message is printed if there is a <code class="inline"><span class="i">$SIG</span>{<span class="w">__WARN__</span>}</code>
 handler
installed.  It is the handler&#39;s responsibility to deal with the message
as it sees fit (like, for instance, converting it into a <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code>).  Most
handlers must therefore make arrangements to actually display the
warnings that they are not prepared to deal with, by calling <code class="inline"><a class="l_k" href="../functions/warn.html">warn</a></code>
again in the handler.  Note that this is quite safe and will not
produce an endless loop, since <code class="inline"><span class="w">__WARN__</span></code>
 hooks are not called from
inside one.</p>
<p>You will find this behavior is slightly different from that of
<code class="inline"><span class="i">$SIG</span>{<span class="w">__DIE__</span>}</code>
 handlers (which don&#39;t suppress the error text, but can
instead call <code class="inline"><a class="l_k" href="../functions/die.html">die</a></code> again to change it).</p>
<p>Using a <code class="inline"><span class="w">__WARN__</span></code>
 handler provides a powerful way to silence all
warnings (even the so-called mandatory ones).  An example:</p>
<pre class="verbatim"><ol><li>    <span class="c"># wipe out *all* compile-time warnings</span></li><li>    BEGIN <span class="s">{</span> <span class="i">$SIG</span>{<span class="q">&#39;__WARN__&#39;</span>} = <a class="l_k" href="../functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="../functions/warn.html">warn</a> <span class="i">$_</span>[<span class="n">0</span>] if <span class="i">$DOWARN</span> <span class="s">}</span> <span class="s">}</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$foo</span> = <span class="n">10</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/my.html">my</a> <span class="i">$foo</span> = <span class="n">20</span><span class="sc">;</span>          <span class="c"># no warning about duplicate my $foo,</span></li><li>                           <span class="c"># but hey, you asked for it!</span></li><li>    <span class="c"># no compile-time or run-time warnings before here</span></li><li>    <span class="i">$DOWARN</span> = <span class="n">1</span><span class="sc">;</span></li><li></li><li>    <span class="c"># run-time warnings enabled after here</span></li><li>    <a class="l_k" href="../functions/warn.html">warn</a> <span class="q">&quot;\$foo is alive and $foo!&quot;</span><span class="sc">;</span>     <span class="c"># does show up</span></li></ol></pre><p>See <a href="../perlvar.html">perlvar</a> for details on setting <code class="inline"><span class="i">%SIG</span></code>
 entries, and for more
examples.  See the Carp module for other kinds of warnings using its
carp() and cluck() functions.
-----

function write
filehandle|
Writes a formatted record (possibly multi-line) to the specified FILEHANDLE,
using the format associated with that file.  By default the format for
a file is the one having the same name as the filehandle, but the
format for the current output channel (see the <code class="inline"><a class="l_k" href="../functions/select.html">select</a></code> function) may be set
explicitly by assigning the name of the format to the <code class="inline"><span class="i">$~</span></code>
 variable.</p>
<p>Top of form processing is handled automatically:  if there is
insufficient room on the current page for the formatted record, the
page is advanced by writing a form feed, a special top-of-page format
is used to format the new page header, and then the record is written.
By default the top-of-page format is the name of the filehandle with
&quot;_TOP&quot; appended, but it may be dynamically set to the format of your
choice by assigning the name to the <code class="inline"><span class="i">$^</span></code>
 variable while the filehandle is
selected.  The number of lines remaining on the current page is in
variable <code class="inline"><span class="i">$-</span></code>
, which can be set to <code class="inline"><span class="n">0</span></code>
 to force a new page.</p>
<p>If FILEHANDLE is unspecified, output goes to the current default output
channel, which starts out as STDOUT but may be changed by the
<code class="inline"><a class="l_k" href="../functions/select.html">select</a></code> operator.  If the FILEHANDLE is an EXPR, then the expression
is evaluated and the resulting string is used to look up the name of
the FILEHANDLE at run time.  For more on formats, see <a href="../perlform.html">perlform</a>.</p>
<p>Note that write is <i>not</i> the opposite of <code class="inline"><a class="l_k" href="../functions/read.html">read</a></code>.  Unfortunately.
-----

function write
expr|
Writes a formatted record (possibly multi-line) to the specified FILEHANDLE,
using the format associated with that file.  By default the format for
a file is the one having the same name as the filehandle, but the
format for the current output channel (see the <code class="inline"><a class="l_k" href="../functions/select.html">select</a></code> function) may be set
explicitly by assigning the name of the format to the <code class="inline"><span class="i">$~</span></code>
 variable.</p>
<p>Top of form processing is handled automatically:  if there is
insufficient room on the current page for the formatted record, the
page is advanced by writing a form feed, a special top-of-page format
is used to format the new page header, and then the record is written.
By default the top-of-page format is the name of the filehandle with
&quot;_TOP&quot; appended, but it may be dynamically set to the format of your
choice by assigning the name to the <code class="inline"><span class="i">$^</span></code>
 variable while the filehandle is
selected.  The number of lines remaining on the current page is in
variable <code class="inline"><span class="i">$-</span></code>
, which can be set to <code class="inline"><span class="n">0</span></code>
 to force a new page.</p>
<p>If FILEHANDLE is unspecified, output goes to the current default output
channel, which starts out as STDOUT but may be changed by the
<code class="inline"><a class="l_k" href="../functions/select.html">select</a></code> operator.  If the FILEHANDLE is an EXPR, then the expression
is evaluated and the resulting string is used to look up the name of
the FILEHANDLE at run time.  For more on formats, see <a href="../perlform.html">perlform</a>.</p>
<p>Note that write is <i>not</i> the opposite of <code class="inline"><a class="l_k" href="../functions/read.html">read</a></code>.  Unfortunately.
-----

function write
Writes a formatted record (possibly multi-line) to the specified FILEHANDLE,
using the format associated with that file.  By default the format for
a file is the one having the same name as the filehandle, but the
format for the current output channel (see the <code class="inline"><a class="l_k" href="../functions/select.html">select</a></code> function) may be set
explicitly by assigning the name of the format to the <code class="inline"><span class="i">$~</span></code>
 variable.</p>
<p>Top of form processing is handled automatically:  if there is
insufficient room on the current page for the formatted record, the
page is advanced by writing a form feed, a special top-of-page format
is used to format the new page header, and then the record is written.
By default the top-of-page format is the name of the filehandle with
&quot;_TOP&quot; appended, but it may be dynamically set to the format of your
choice by assigning the name to the <code class="inline"><span class="i">$^</span></code>
 variable while the filehandle is
selected.  The number of lines remaining on the current page is in
variable <code class="inline"><span class="i">$-</span></code>
, which can be set to <code class="inline"><span class="n">0</span></code>
 to force a new page.</p>
<p>If FILEHANDLE is unspecified, output goes to the current default output
channel, which starts out as STDOUT but may be changed by the
<code class="inline"><a class="l_k" href="../functions/select.html">select</a></code> operator.  If the FILEHANDLE is an EXPR, then the expression
is evaluated and the resulting string is used to look up the name of
the FILEHANDLE at run time.  For more on formats, see <a href="../perlform.html">perlform</a>.</p>
<p>Note that write is <i>not</i> the opposite of <code class="inline"><a class="l_k" href="../functions/read.html">read</a></code>.  Unfortunately.
-----

function -X
filehandle|
A file test, where X is one of the letters listed below.  This unary
operator takes one argument, either a filename, a filehandle, or a dirhandle, 
and tests the associated file to see if something is true about it.  If the
argument is omitted, tests <code class="inline"><span class="i">$_</span></code>
, except for <code class="inline">-t</code>
, which tests STDIN.
Unless otherwise documented, it returns <code class="inline"><span class="n">1</span></code>
 for true and <code class="inline"><span class="q">&#39;&#39;</span></code>
 for false, or
the undefined value if the file doesn&#39;t exist.  Despite the funny
names, precedence is the same as any other named unary operator.  The
operator may be any of:</p>
<pre class="verbatim"><ol><li>    -r	File is readable by effective uid/gid.</li><li>    -w	File is writable by effective uid/gid.</li><li>    -x	File is executable by effective uid/gid.</li><li>    -o	File is owned by effective uid.</li><li></li><li>    -R	File is readable by real uid/gid.</li><li>    -W	File is writable by real uid/gid.</li><li>    -X	File is executable by real uid/gid.</li><li>    -O	File is owned by real uid.</li><li></li><li>    -e	File exists.</li><li>    -z	File has zero size (is empty).</li><li>    -s	File has nonzero size (returns size in bytes).</li><li></li><li>    -f	File is a plain file.</li><li>    -d	File is a directory.</li><li>    -l	File is a symbolic link.</li><li>    -p	File is a named pipe (FIFO), or Filehandle is a pipe.</li><li>    -S	File is a socket.</li><li>    -b	File is a block special file.</li><li>    -c	File is a character special file.</li><li>    -t	Filehandle is opened to a tty.</li><li></li><li>    -u	File has setuid bit set.</li><li>    -g	File has setgid bit set.</li><li>    -k	File has sticky bit set.</li><li></li><li>    -T	File is an ASCII text file (heuristic guess).</li><li>    -B	File is a "binary" file (opposite of -T).</li><li></li><li>    -M	Script start time minus file modification time, in days.</li><li>    -A	Same for access time.</li><li>    -C	Same for inode change time (Unix, may differ for other platforms)</li></ol></pre><p>Example:</p>
<pre class="verbatim"><ol><li>    while <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/chomp.html">chomp</a><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/next.html">next</a> unless -f <span class="i">$_</span><span class="sc">;</span>	<span class="c"># ignore specials</span></li><li>	<span class="c">#...</span></li><li>    <span class="s">}</span></li></ol></pre><p>The interpretation of the file permission operators <code class="inline">-r</code>
, <code class="inline">-R</code>
,
<code class="inline">-w</code>
, <code class="inline">-W</code>
, <code class="inline">-x</code>
, and <code class="inline"><a class="l_k" href="../functions/-X.html">-X</a></code> is by default based solely on the mode
of the file and the uids and gids of the user.  There may be other
reasons you can&#39;t actually read, write, or execute the file: for
example network filesystem access controls, ACLs (access control lists),
read-only filesystems, and unrecognized executable formats.  Note
that the use of these six specific operators to verify if some operation
is possible is usually a mistake, because it may be open to race
conditions.</p>
<p>Also note that, for the superuser on the local filesystems, the <code class="inline">-r</code>
,
<code class="inline">-R</code>
, <code class="inline">-w</code>
, and <code class="inline">-W</code>
 tests always return 1, and <code class="inline">-x</code>
 and <code class="inline"><a class="l_k" href="../functions/-X.html">-X</a></code> return 1
if any execute bit is set in the mode.  Scripts run by the superuser
may thus need to do a stat() to determine the actual mode of the file,
or temporarily set their effective uid to something else.</p>
<p>If you are using ACLs, there is a pragma called <code class="inline"><span class="w">filetest</span></code>
 that may
produce more accurate results than the bare stat() mode bits.
When under the <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">filetest</span> <span class="q">&#39;access&#39;</span></code>
 the above-mentioned filetests
will test whether the permission can (not) be granted using the
access() family of system calls.  Also note that the <code class="inline">-x</code>
 and <code class="inline"><a class="l_k" href="../functions/-X.html">-X</a></code> may
under this pragma return true even if there are no execute permission
bits set (nor any extra execute permission ACLs).  This strangeness is
due to the underlying system calls&#39; definitions. Note also that, due to
the implementation of <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">filetest</span> <span class="q">&#39;access&#39;</span></code>
, the <code class="inline"><span class="w">_</span></code>
 special
filehandle won&#39;t cache the results of the file tests when this pragma is
in effect.  Read the documentation for the <code class="inline"><span class="w">filetest</span></code>
 pragma for more
information.</p>
<p>Note that <code class="inline">-s/a/b/</code> does not do a negated substitution.  Saying
<code class="inline">-<a class="l_k" href="../functions/exp.html">exp</a><span class="s">(</span><span class="i">$foo</span><span class="s">)</span></code>
 still works as expected, however--only single letters
following a minus are interpreted as file tests.</p>
<p>The <code class="inline">-T</code>
 and <code class="inline">-B</code>
 switches work as follows.  The first block or so of the
file is examined for odd characters such as strange control codes or
characters with the high bit set.  If too many strange characters (&gt;30%)
are found, it&#39;s a <code class="inline">-B</code>
 file; otherwise it&#39;s a <code class="inline">-T</code>
 file.  Also, any file
containing null in the first block is considered a binary file.  If <code class="inline">-T</code>

or <code class="inline">-B</code>
 is used on a filehandle, the current IO buffer is examined
rather than the first block.  Both <code class="inline">-T</code>
 and <code class="inline">-B</code>
 return true on a null
file, or a file at EOF when testing a filehandle.  Because you have to
read a file to do the <code class="inline">-T</code>
 test, on most occasions you want to use a <code class="inline">-f</code>

against the file first, as in <code class="inline"><a class="l_k" href="../functions/next.html">next</a> unless -f <span class="i">$file</span> &amp;&amp; -T <span class="i">$file</span></code>
.</p>
<p>If any of the file tests (or either the <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code> or <code class="inline"><a class="l_k" href="../functions/lstat.html">lstat</a></code> operators) are given
the special filehandle consisting of a solitary underline, then the stat
structure of the previous file test (or stat operator) is used, saving
a system call.  (This doesn&#39;t work with <code class="inline">-t</code>
, and you need to remember
that lstat() and <code class="inline">-l</code>
 will leave values in the stat structure for the
symbolic link, not the real file.)  (Also, if the stat buffer was filled by
an <code class="inline"><a class="l_k" href="../functions/lstat.html">lstat</a></code> call, <code class="inline">-T</code>
 and <code class="inline">-B</code>
 will reset it with the results of <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a> <span class="w">_</span></code>
).
Example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Can do.\n&quot;</span> if -r <span class="i">$a</span> || -w <span class="i">_</span> || -x <span class="i">_</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Readable\n&quot;</span> if -r <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Writable\n&quot;</span> if -w <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Executable\n&quot;</span> if -x <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Setuid\n&quot;</span> if -u <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Setgid\n&quot;</span> if -g <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Sticky\n&quot;</span> if -k <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Text\n&quot;</span> if -T <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Binary\n&quot;</span> if -B <span class="i">_</span><span class="sc">;</span></li></ol></pre><p>As of Perl 5.9.1, as a form of purely syntactic sugar, you can stack file
test operators, in a way that <code class="inline">-f -w -x <span class="i">$file</span></code>
 is equivalent to
<code class="inline">-x <span class="i">$file</span> &amp;&amp; -w <span class="i">_</span> &amp;&amp; -f <span class="i">_</span></code>
. (This is only syntax fancy: if you use
the return value of <code class="inline">-f <span class="i">$file</span></code>
 as an argument to another filetest
operator, no special magic will happen.)
-----

function -X
expr|
A file test, where X is one of the letters listed below.  This unary
operator takes one argument, either a filename, a filehandle, or a dirhandle, 
and tests the associated file to see if something is true about it.  If the
argument is omitted, tests <code class="inline"><span class="i">$_</span></code>
, except for <code class="inline">-t</code>
, which tests STDIN.
Unless otherwise documented, it returns <code class="inline"><span class="n">1</span></code>
 for true and <code class="inline"><span class="q">&#39;&#39;</span></code>
 for false, or
the undefined value if the file doesn&#39;t exist.  Despite the funny
names, precedence is the same as any other named unary operator.  The
operator may be any of:</p>
<pre class="verbatim"><ol><li>    -r	File is readable by effective uid/gid.</li><li>    -w	File is writable by effective uid/gid.</li><li>    -x	File is executable by effective uid/gid.</li><li>    -o	File is owned by effective uid.</li><li></li><li>    -R	File is readable by real uid/gid.</li><li>    -W	File is writable by real uid/gid.</li><li>    -X	File is executable by real uid/gid.</li><li>    -O	File is owned by real uid.</li><li></li><li>    -e	File exists.</li><li>    -z	File has zero size (is empty).</li><li>    -s	File has nonzero size (returns size in bytes).</li><li></li><li>    -f	File is a plain file.</li><li>    -d	File is a directory.</li><li>    -l	File is a symbolic link.</li><li>    -p	File is a named pipe (FIFO), or Filehandle is a pipe.</li><li>    -S	File is a socket.</li><li>    -b	File is a block special file.</li><li>    -c	File is a character special file.</li><li>    -t	Filehandle is opened to a tty.</li><li></li><li>    -u	File has setuid bit set.</li><li>    -g	File has setgid bit set.</li><li>    -k	File has sticky bit set.</li><li></li><li>    -T	File is an ASCII text file (heuristic guess).</li><li>    -B	File is a "binary" file (opposite of -T).</li><li></li><li>    -M	Script start time minus file modification time, in days.</li><li>    -A	Same for access time.</li><li>    -C	Same for inode change time (Unix, may differ for other platforms)</li></ol></pre><p>Example:</p>
<pre class="verbatim"><ol><li>    while <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/chomp.html">chomp</a><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/next.html">next</a> unless -f <span class="i">$_</span><span class="sc">;</span>	<span class="c"># ignore specials</span></li><li>	<span class="c">#...</span></li><li>    <span class="s">}</span></li></ol></pre><p>The interpretation of the file permission operators <code class="inline">-r</code>
, <code class="inline">-R</code>
,
<code class="inline">-w</code>
, <code class="inline">-W</code>
, <code class="inline">-x</code>
, and <code class="inline"><a class="l_k" href="../functions/-X.html">-X</a></code> is by default based solely on the mode
of the file and the uids and gids of the user.  There may be other
reasons you can&#39;t actually read, write, or execute the file: for
example network filesystem access controls, ACLs (access control lists),
read-only filesystems, and unrecognized executable formats.  Note
that the use of these six specific operators to verify if some operation
is possible is usually a mistake, because it may be open to race
conditions.</p>
<p>Also note that, for the superuser on the local filesystems, the <code class="inline">-r</code>
,
<code class="inline">-R</code>
, <code class="inline">-w</code>
, and <code class="inline">-W</code>
 tests always return 1, and <code class="inline">-x</code>
 and <code class="inline"><a class="l_k" href="../functions/-X.html">-X</a></code> return 1
if any execute bit is set in the mode.  Scripts run by the superuser
may thus need to do a stat() to determine the actual mode of the file,
or temporarily set their effective uid to something else.</p>
<p>If you are using ACLs, there is a pragma called <code class="inline"><span class="w">filetest</span></code>
 that may
produce more accurate results than the bare stat() mode bits.
When under the <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">filetest</span> <span class="q">&#39;access&#39;</span></code>
 the above-mentioned filetests
will test whether the permission can (not) be granted using the
access() family of system calls.  Also note that the <code class="inline">-x</code>
 and <code class="inline"><a class="l_k" href="../functions/-X.html">-X</a></code> may
under this pragma return true even if there are no execute permission
bits set (nor any extra execute permission ACLs).  This strangeness is
due to the underlying system calls&#39; definitions. Note also that, due to
the implementation of <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">filetest</span> <span class="q">&#39;access&#39;</span></code>
, the <code class="inline"><span class="w">_</span></code>
 special
filehandle won&#39;t cache the results of the file tests when this pragma is
in effect.  Read the documentation for the <code class="inline"><span class="w">filetest</span></code>
 pragma for more
information.</p>
<p>Note that <code class="inline">-s/a/b/</code> does not do a negated substitution.  Saying
<code class="inline">-<a class="l_k" href="../functions/exp.html">exp</a><span class="s">(</span><span class="i">$foo</span><span class="s">)</span></code>
 still works as expected, however--only single letters
following a minus are interpreted as file tests.</p>
<p>The <code class="inline">-T</code>
 and <code class="inline">-B</code>
 switches work as follows.  The first block or so of the
file is examined for odd characters such as strange control codes or
characters with the high bit set.  If too many strange characters (&gt;30%)
are found, it&#39;s a <code class="inline">-B</code>
 file; otherwise it&#39;s a <code class="inline">-T</code>
 file.  Also, any file
containing null in the first block is considered a binary file.  If <code class="inline">-T</code>

or <code class="inline">-B</code>
 is used on a filehandle, the current IO buffer is examined
rather than the first block.  Both <code class="inline">-T</code>
 and <code class="inline">-B</code>
 return true on a null
file, or a file at EOF when testing a filehandle.  Because you have to
read a file to do the <code class="inline">-T</code>
 test, on most occasions you want to use a <code class="inline">-f</code>

against the file first, as in <code class="inline"><a class="l_k" href="../functions/next.html">next</a> unless -f <span class="i">$file</span> &amp;&amp; -T <span class="i">$file</span></code>
.</p>
<p>If any of the file tests (or either the <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code> or <code class="inline"><a class="l_k" href="../functions/lstat.html">lstat</a></code> operators) are given
the special filehandle consisting of a solitary underline, then the stat
structure of the previous file test (or stat operator) is used, saving
a system call.  (This doesn&#39;t work with <code class="inline">-t</code>
, and you need to remember
that lstat() and <code class="inline">-l</code>
 will leave values in the stat structure for the
symbolic link, not the real file.)  (Also, if the stat buffer was filled by
an <code class="inline"><a class="l_k" href="../functions/lstat.html">lstat</a></code> call, <code class="inline">-T</code>
 and <code class="inline">-B</code>
 will reset it with the results of <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a> <span class="w">_</span></code>
).
Example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Can do.\n&quot;</span> if -r <span class="i">$a</span> || -w <span class="i">_</span> || -x <span class="i">_</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Readable\n&quot;</span> if -r <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Writable\n&quot;</span> if -w <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Executable\n&quot;</span> if -x <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Setuid\n&quot;</span> if -u <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Setgid\n&quot;</span> if -g <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Sticky\n&quot;</span> if -k <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Text\n&quot;</span> if -T <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Binary\n&quot;</span> if -B <span class="i">_</span><span class="sc">;</span></li></ol></pre><p>As of Perl 5.9.1, as a form of purely syntactic sugar, you can stack file
test operators, in a way that <code class="inline">-f -w -x <span class="i">$file</span></code>
 is equivalent to
<code class="inline">-x <span class="i">$file</span> &amp;&amp; -w <span class="i">_</span> &amp;&amp; -f <span class="i">_</span></code>
. (This is only syntax fancy: if you use
the return value of <code class="inline">-f <span class="i">$file</span></code>
 as an argument to another filetest
operator, no special magic will happen.)
-----

function -X
dirhandle|
A file test, where X is one of the letters listed below.  This unary
operator takes one argument, either a filename, a filehandle, or a dirhandle, 
and tests the associated file to see if something is true about it.  If the
argument is omitted, tests <code class="inline"><span class="i">$_</span></code>
, except for <code class="inline">-t</code>
, which tests STDIN.
Unless otherwise documented, it returns <code class="inline"><span class="n">1</span></code>
 for true and <code class="inline"><span class="q">&#39;&#39;</span></code>
 for false, or
the undefined value if the file doesn&#39;t exist.  Despite the funny
names, precedence is the same as any other named unary operator.  The
operator may be any of:</p>
<pre class="verbatim"><ol><li>    -r	File is readable by effective uid/gid.</li><li>    -w	File is writable by effective uid/gid.</li><li>    -x	File is executable by effective uid/gid.</li><li>    -o	File is owned by effective uid.</li><li></li><li>    -R	File is readable by real uid/gid.</li><li>    -W	File is writable by real uid/gid.</li><li>    -X	File is executable by real uid/gid.</li><li>    -O	File is owned by real uid.</li><li></li><li>    -e	File exists.</li><li>    -z	File has zero size (is empty).</li><li>    -s	File has nonzero size (returns size in bytes).</li><li></li><li>    -f	File is a plain file.</li><li>    -d	File is a directory.</li><li>    -l	File is a symbolic link.</li><li>    -p	File is a named pipe (FIFO), or Filehandle is a pipe.</li><li>    -S	File is a socket.</li><li>    -b	File is a block special file.</li><li>    -c	File is a character special file.</li><li>    -t	Filehandle is opened to a tty.</li><li></li><li>    -u	File has setuid bit set.</li><li>    -g	File has setgid bit set.</li><li>    -k	File has sticky bit set.</li><li></li><li>    -T	File is an ASCII text file (heuristic guess).</li><li>    -B	File is a "binary" file (opposite of -T).</li><li></li><li>    -M	Script start time minus file modification time, in days.</li><li>    -A	Same for access time.</li><li>    -C	Same for inode change time (Unix, may differ for other platforms)</li></ol></pre><p>Example:</p>
<pre class="verbatim"><ol><li>    while <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/chomp.html">chomp</a><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/next.html">next</a> unless -f <span class="i">$_</span><span class="sc">;</span>	<span class="c"># ignore specials</span></li><li>	<span class="c">#...</span></li><li>    <span class="s">}</span></li></ol></pre><p>The interpretation of the file permission operators <code class="inline">-r</code>
, <code class="inline">-R</code>
,
<code class="inline">-w</code>
, <code class="inline">-W</code>
, <code class="inline">-x</code>
, and <code class="inline"><a class="l_k" href="../functions/-X.html">-X</a></code> is by default based solely on the mode
of the file and the uids and gids of the user.  There may be other
reasons you can&#39;t actually read, write, or execute the file: for
example network filesystem access controls, ACLs (access control lists),
read-only filesystems, and unrecognized executable formats.  Note
that the use of these six specific operators to verify if some operation
is possible is usually a mistake, because it may be open to race
conditions.</p>
<p>Also note that, for the superuser on the local filesystems, the <code class="inline">-r</code>
,
<code class="inline">-R</code>
, <code class="inline">-w</code>
, and <code class="inline">-W</code>
 tests always return 1, and <code class="inline">-x</code>
 and <code class="inline"><a class="l_k" href="../functions/-X.html">-X</a></code> return 1
if any execute bit is set in the mode.  Scripts run by the superuser
may thus need to do a stat() to determine the actual mode of the file,
or temporarily set their effective uid to something else.</p>
<p>If you are using ACLs, there is a pragma called <code class="inline"><span class="w">filetest</span></code>
 that may
produce more accurate results than the bare stat() mode bits.
When under the <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">filetest</span> <span class="q">&#39;access&#39;</span></code>
 the above-mentioned filetests
will test whether the permission can (not) be granted using the
access() family of system calls.  Also note that the <code class="inline">-x</code>
 and <code class="inline"><a class="l_k" href="../functions/-X.html">-X</a></code> may
under this pragma return true even if there are no execute permission
bits set (nor any extra execute permission ACLs).  This strangeness is
due to the underlying system calls&#39; definitions. Note also that, due to
the implementation of <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">filetest</span> <span class="q">&#39;access&#39;</span></code>
, the <code class="inline"><span class="w">_</span></code>
 special
filehandle won&#39;t cache the results of the file tests when this pragma is
in effect.  Read the documentation for the <code class="inline"><span class="w">filetest</span></code>
 pragma for more
information.</p>
<p>Note that <code class="inline">-s/a/b/</code> does not do a negated substitution.  Saying
<code class="inline">-<a class="l_k" href="../functions/exp.html">exp</a><span class="s">(</span><span class="i">$foo</span><span class="s">)</span></code>
 still works as expected, however--only single letters
following a minus are interpreted as file tests.</p>
<p>The <code class="inline">-T</code>
 and <code class="inline">-B</code>
 switches work as follows.  The first block or so of the
file is examined for odd characters such as strange control codes or
characters with the high bit set.  If too many strange characters (&gt;30%)
are found, it&#39;s a <code class="inline">-B</code>
 file; otherwise it&#39;s a <code class="inline">-T</code>
 file.  Also, any file
containing null in the first block is considered a binary file.  If <code class="inline">-T</code>

or <code class="inline">-B</code>
 is used on a filehandle, the current IO buffer is examined
rather than the first block.  Both <code class="inline">-T</code>
 and <code class="inline">-B</code>
 return true on a null
file, or a file at EOF when testing a filehandle.  Because you have to
read a file to do the <code class="inline">-T</code>
 test, on most occasions you want to use a <code class="inline">-f</code>

against the file first, as in <code class="inline"><a class="l_k" href="../functions/next.html">next</a> unless -f <span class="i">$file</span> &amp;&amp; -T <span class="i">$file</span></code>
.</p>
<p>If any of the file tests (or either the <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code> or <code class="inline"><a class="l_k" href="../functions/lstat.html">lstat</a></code> operators) are given
the special filehandle consisting of a solitary underline, then the stat
structure of the previous file test (or stat operator) is used, saving
a system call.  (This doesn&#39;t work with <code class="inline">-t</code>
, and you need to remember
that lstat() and <code class="inline">-l</code>
 will leave values in the stat structure for the
symbolic link, not the real file.)  (Also, if the stat buffer was filled by
an <code class="inline"><a class="l_k" href="../functions/lstat.html">lstat</a></code> call, <code class="inline">-T</code>
 and <code class="inline">-B</code>
 will reset it with the results of <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a> <span class="w">_</span></code>
).
Example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Can do.\n&quot;</span> if -r <span class="i">$a</span> || -w <span class="i">_</span> || -x <span class="i">_</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Readable\n&quot;</span> if -r <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Writable\n&quot;</span> if -w <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Executable\n&quot;</span> if -x <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Setuid\n&quot;</span> if -u <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Setgid\n&quot;</span> if -g <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Sticky\n&quot;</span> if -k <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Text\n&quot;</span> if -T <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Binary\n&quot;</span> if -B <span class="i">_</span><span class="sc">;</span></li></ol></pre><p>As of Perl 5.9.1, as a form of purely syntactic sugar, you can stack file
test operators, in a way that <code class="inline">-f -w -x <span class="i">$file</span></code>
 is equivalent to
<code class="inline">-x <span class="i">$file</span> &amp;&amp; -w <span class="i">_</span> &amp;&amp; -f <span class="i">_</span></code>
. (This is only syntax fancy: if you use
the return value of <code class="inline">-f <span class="i">$file</span></code>
 as an argument to another filetest
operator, no special magic will happen.)
-----

function -X
A file test, where X is one of the letters listed below.  This unary
operator takes one argument, either a filename, a filehandle, or a dirhandle, 
and tests the associated file to see if something is true about it.  If the
argument is omitted, tests <code class="inline"><span class="i">$_</span></code>
, except for <code class="inline">-t</code>
, which tests STDIN.
Unless otherwise documented, it returns <code class="inline"><span class="n">1</span></code>
 for true and <code class="inline"><span class="q">&#39;&#39;</span></code>
 for false, or
the undefined value if the file doesn&#39;t exist.  Despite the funny
names, precedence is the same as any other named unary operator.  The
operator may be any of:</p>
<pre class="verbatim"><ol><li>    -r	File is readable by effective uid/gid.</li><li>    -w	File is writable by effective uid/gid.</li><li>    -x	File is executable by effective uid/gid.</li><li>    -o	File is owned by effective uid.</li><li></li><li>    -R	File is readable by real uid/gid.</li><li>    -W	File is writable by real uid/gid.</li><li>    -X	File is executable by real uid/gid.</li><li>    -O	File is owned by real uid.</li><li></li><li>    -e	File exists.</li><li>    -z	File has zero size (is empty).</li><li>    -s	File has nonzero size (returns size in bytes).</li><li></li><li>    -f	File is a plain file.</li><li>    -d	File is a directory.</li><li>    -l	File is a symbolic link.</li><li>    -p	File is a named pipe (FIFO), or Filehandle is a pipe.</li><li>    -S	File is a socket.</li><li>    -b	File is a block special file.</li><li>    -c	File is a character special file.</li><li>    -t	Filehandle is opened to a tty.</li><li></li><li>    -u	File has setuid bit set.</li><li>    -g	File has setgid bit set.</li><li>    -k	File has sticky bit set.</li><li></li><li>    -T	File is an ASCII text file (heuristic guess).</li><li>    -B	File is a "binary" file (opposite of -T).</li><li></li><li>    -M	Script start time minus file modification time, in days.</li><li>    -A	Same for access time.</li><li>    -C	Same for inode change time (Unix, may differ for other platforms)</li></ol></pre><p>Example:</p>
<pre class="verbatim"><ol><li>    while <span class="s">(</span>&lt;&gt;<span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="../functions/chomp.html">chomp</a><span class="sc">;</span></li><li>	<a class="l_k" href="../functions/next.html">next</a> unless -f <span class="i">$_</span><span class="sc">;</span>	<span class="c"># ignore specials</span></li><li>	<span class="c">#...</span></li><li>    <span class="s">}</span></li></ol></pre><p>The interpretation of the file permission operators <code class="inline">-r</code>
, <code class="inline">-R</code>
,
<code class="inline">-w</code>
, <code class="inline">-W</code>
, <code class="inline">-x</code>
, and <code class="inline"><a class="l_k" href="../functions/-X.html">-X</a></code> is by default based solely on the mode
of the file and the uids and gids of the user.  There may be other
reasons you can&#39;t actually read, write, or execute the file: for
example network filesystem access controls, ACLs (access control lists),
read-only filesystems, and unrecognized executable formats.  Note
that the use of these six specific operators to verify if some operation
is possible is usually a mistake, because it may be open to race
conditions.</p>
<p>Also note that, for the superuser on the local filesystems, the <code class="inline">-r</code>
,
<code class="inline">-R</code>
, <code class="inline">-w</code>
, and <code class="inline">-W</code>
 tests always return 1, and <code class="inline">-x</code>
 and <code class="inline"><a class="l_k" href="../functions/-X.html">-X</a></code> return 1
if any execute bit is set in the mode.  Scripts run by the superuser
may thus need to do a stat() to determine the actual mode of the file,
or temporarily set their effective uid to something else.</p>
<p>If you are using ACLs, there is a pragma called <code class="inline"><span class="w">filetest</span></code>
 that may
produce more accurate results than the bare stat() mode bits.
When under the <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">filetest</span> <span class="q">&#39;access&#39;</span></code>
 the above-mentioned filetests
will test whether the permission can (not) be granted using the
access() family of system calls.  Also note that the <code class="inline">-x</code>
 and <code class="inline"><a class="l_k" href="../functions/-X.html">-X</a></code> may
under this pragma return true even if there are no execute permission
bits set (nor any extra execute permission ACLs).  This strangeness is
due to the underlying system calls&#39; definitions. Note also that, due to
the implementation of <code class="inline"><a class="l_k" href="../functions/use.html">use</a> <span class="w">filetest</span> <span class="q">&#39;access&#39;</span></code>
, the <code class="inline"><span class="w">_</span></code>
 special
filehandle won&#39;t cache the results of the file tests when this pragma is
in effect.  Read the documentation for the <code class="inline"><span class="w">filetest</span></code>
 pragma for more
information.</p>
<p>Note that <code class="inline">-s/a/b/</code> does not do a negated substitution.  Saying
<code class="inline">-<a class="l_k" href="../functions/exp.html">exp</a><span class="s">(</span><span class="i">$foo</span><span class="s">)</span></code>
 still works as expected, however--only single letters
following a minus are interpreted as file tests.</p>
<p>The <code class="inline">-T</code>
 and <code class="inline">-B</code>
 switches work as follows.  The first block or so of the
file is examined for odd characters such as strange control codes or
characters with the high bit set.  If too many strange characters (&gt;30%)
are found, it&#39;s a <code class="inline">-B</code>
 file; otherwise it&#39;s a <code class="inline">-T</code>
 file.  Also, any file
containing null in the first block is considered a binary file.  If <code class="inline">-T</code>

or <code class="inline">-B</code>
 is used on a filehandle, the current IO buffer is examined
rather than the first block.  Both <code class="inline">-T</code>
 and <code class="inline">-B</code>
 return true on a null
file, or a file at EOF when testing a filehandle.  Because you have to
read a file to do the <code class="inline">-T</code>
 test, on most occasions you want to use a <code class="inline">-f</code>

against the file first, as in <code class="inline"><a class="l_k" href="../functions/next.html">next</a> unless -f <span class="i">$file</span> &amp;&amp; -T <span class="i">$file</span></code>
.</p>
<p>If any of the file tests (or either the <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a></code> or <code class="inline"><a class="l_k" href="../functions/lstat.html">lstat</a></code> operators) are given
the special filehandle consisting of a solitary underline, then the stat
structure of the previous file test (or stat operator) is used, saving
a system call.  (This doesn&#39;t work with <code class="inline">-t</code>
, and you need to remember
that lstat() and <code class="inline">-l</code>
 will leave values in the stat structure for the
symbolic link, not the real file.)  (Also, if the stat buffer was filled by
an <code class="inline"><a class="l_k" href="../functions/lstat.html">lstat</a></code> call, <code class="inline">-T</code>
 and <code class="inline">-B</code>
 will reset it with the results of <code class="inline"><a class="l_k" href="../functions/stat.html">stat</a> <span class="w">_</span></code>
).
Example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Can do.\n&quot;</span> if -r <span class="i">$a</span> || -w <span class="i">_</span> || -x <span class="i">_</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="../functions/stat.html">stat</a><span class="s">(</span><span class="i">$filename</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Readable\n&quot;</span> if -r <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Writable\n&quot;</span> if -w <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Executable\n&quot;</span> if -x <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Setuid\n&quot;</span> if -u <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Setgid\n&quot;</span> if -g <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Sticky\n&quot;</span> if -k <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Text\n&quot;</span> if -T <span class="i">_</span><span class="sc">;</span></li><li>    <a class="l_k" href="../functions/print.html">print</a> <span class="q">&quot;Binary\n&quot;</span> if -B <span class="i">_</span><span class="sc">;</span></li></ol></pre><p>As of Perl 5.9.1, as a form of purely syntactic sugar, you can stack file
test operators, in a way that <code class="inline">-f -w -x <span class="i">$file</span></code>
 is equivalent to
<code class="inline">-x <span class="i">$file</span> &amp;&amp; -w <span class="i">_</span> &amp;&amp; -f <span class="i">_</span></code>
. (This is only syntax fancy: if you use
the return value of <code class="inline">-f <span class="i">$file</span></code>
 as an argument to another filetest
operator, no special magic will happen.)
-----

function y
///|
The transliteration operator.  Same as <code class="inline"><a class="l_k" href="../functions/tr.html">tr///</a></code>.  See
<a href="../perlop.html#Quote-and-Quote-like-Operators">"Quote and Quote-like Operators" in perlop</a>.
-----

