perlfunc.html

来自「perl教程」· HTML 代码 · 共 891 行 · 第 1/5 页

HTML
891 行
字号
    <span class="operator">};</span>
    <span class="keyword">if</span> <span class="operator">(</span><span class="variable">$@</span><span class="operator">)</span> <span class="operator">{</span>
        <span class="keyword">die</span> <span class="keyword">unless</span> <span class="variable">$@</span> <span class="keyword">eq</span> <span class="string">"alarm\n"</span><span class="operator">;</span>   <span class="comment"># propagate unexpected errors</span>
        <span class="comment"># timed out</span>
    <span class="operator">}</span>
    <span class="keyword">else</span> <span class="operator">{</span>
        <span class="comment"># didn't</span>
    <span class="operator">}</span>
</pre>
</dd>
<dd>
<p>For more information see <a href="../../lib/Pod/perlipc.html">the perlipc manpage</a>.</p>
</dd>
</li>
<dt><strong><a name="item_atan2">atan2 Y,X</a></strong>

<dd>
<p>Returns the arctangent of Y/X in the range -PI to PI.</p>
</dd>
<dd>
<p>For the tangent operation, you may use the <code>Math::Trig::tan</code>
function, or use the familiar relation:</p>
</dd>
<dd>
<pre>
    <span class="keyword">sub</span><span class="variable"> tan </span><span class="operator">{</span> <span class="keyword">sin</span><span class="operator">(</span><span class="variable">$_</span><span class="operator">[</span><span class="number">0</span><span class="operator">]</span><span class="operator">)</span> <span class="operator">/</span> <span class="keyword">cos</span><span class="operator">(</span><span class="variable">$_</span><span class="operator">[</span><span class="number">0</span><span class="operator">]</span><span class="operator">)</span>  <span class="operator">}</span>
</pre>
</dd>
<dd>
<p>Note that atan2(0, 0) is not well-defined.</p>
</dd>
</li>
<dt><strong><a name="item_bind">bind SOCKET,NAME</a></strong>

<dd>
<p>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="../../lib/Pod/perlipc.html#sockets__client_server_communication">Sockets: Client/Server Communication in the perlipc manpage</a>.</p>
</dd>
</li>
<dt><strong><a name="item_binmode">binmode FILEHANDLE, LAYER</a></strong>

<dt><strong>binmode FILEHANDLE</strong>

<dd>
<p>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 <a href="#item_undef"><code>undef</code></a> and sets <a href="../../lib/Pod/perlvar.html#item___"><code>$!</code></a> (errno).</p>
</dd>
<dd>
<p>On some systems (in general, DOS and Windows-based systems) <a href="#item_binmode"><code>binmode()</code></a>
is necessary when you'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't appropriate.  Also, people can
set their I/O to be by default UTF-8 encoded Unicode, not bytes.</p>
</dd>
<dd>
<p>In other words: regardless of platform, use <a href="#item_binmode"><code>binmode()</code></a> on binary data,
like for example images.</p>
</dd>
<dd>
<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 text file makes sense.</p>
</dd>
<dd>
<p>If LAYER is omitted or specified as <a href="../../lib/Pod/perlrun.html#item__3araw"><code>:raw</code></a> 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 <em>&quot;Programming Perl&quot;</em> (the
Camel) or elsewhere, <a href="../../lib/Pod/perlrun.html#item__3araw"><code>:raw</code></a> is <em>not</em> the simply inverse of <a href="../../lib/Pod/perlrun.html#item__3acrlf"><code>:crlf</code></a>
-- other layers which would affect binary nature of the stream are
<em>also</em> disabled. See <a href="../../lib/PerlIO.html">the PerlIO manpage</a>, <a href="../../lib/Pod/perlrun.html">the perlrun manpage</a> and the discussion about the
PERLIO environment variable.</p>
</dd>
<dd>
<p>The <a href="../../lib/Pod/perlrun.html#item__3abytes"><code>:bytes</code></a>, <a href="../../lib/Pod/perlrun.html#item__3acrlf"><code>:crlf</code></a>, and <a href="../../lib/Pod/perlrun.html#item__3autf8"><code>:utf8</code></a>, and any other directives of the
form <code>:...</code>, are called I/O <em>layers</em>.  The <a href="#item_open"><code>open</code></a> pragma can be used to
establish default I/O layers.  See <a href="../../lib/open.html">the open manpage</a>.</p>
</dd>
<dd>
<p><em>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...</em></p>
</dd>
<dd>
<p>To mark FILEHANDLE as UTF-8, use <a href="../../lib/Pod/perlrun.html#item__3autf8"><code>:utf8</code></a>.</p>
</dd>
<dd>
<p>In general, <a href="#item_binmode"><code>binmode()</code></a> should be called after <a href="#item_open"><code>open()</code></a> but before any I/O
is done on the filehandle.  Calling <a href="#item_binmode"><code>binmode()</code></a> will normally flush any
pending buffered output data (and perhaps pending input data) on the
handle.  An exception to this is the <code>:encoding</code> layer that
changes the default character encoding of the handle, see <a href="../../lib/open.html">the open manpage</a>.
The <code>:encoding</code> layer sometimes needs to be called in
mid-stream, and it doesn't flush the stream.  The <code>:encoding</code>
also implicitly pushes on top of itself the <a href="../../lib/Pod/perlrun.html#item__3autf8"><code>:utf8</code></a> layer because
internally Perl will operate on UTF-8 encoded Unicode characters.</p>
</dd>
<dd>
<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>\n</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>\n</code> is made up of more than
one character.</p>
</dd>
<dd>
<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>\n</code> as a simple <code>\cJ</code>,
but what's stored in text files are the two characters <code>\cM\cJ</code>.  That
means that, if you don't use <a href="#item_binmode"><code>binmode()</code></a> on these systems, <code>\cM\cJ</code>
sequences on disk will be converted to <code>\n</code> on input, and any <code>\n</code> in
your program will be converted back to <code>\cM\cJ</code> on output.  This is what
you want for text files, but it can be disastrous for binary files.</p>
</dd>
<dd>
<p>Another consequence of using <a href="#item_binmode"><code>binmode()</code></a> (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>\cZ</code>, the I/O subsystem will regard it as the end of
the file, unless you use binmode().</p>
</dd>
<dd>
<p><a href="#item_binmode"><code>binmode()</code></a> is not only important for <a href="#item_readline"><code>readline()</code></a> and <a href="#item_print"><code>print()</code></a> operations,
but also when using read(), seek(), sysread(), <a href="#item_syswrite"><code>syswrite()</code></a> and <a href="#item_tell"><code>tell()</code></a>
(see <a href="../../lib/Pod/perlport.html">the perlport manpage</a> for more details).  See the <a href="../../lib/Pod/perlvar.html#item___"><code>$/</code></a> and <a href="../../lib/Pod/perlvar.html#item___"><code>$\</code></a> variables
in <a href="../../lib/Pod/perlvar.html">the perlvar manpage</a> for how to manually set your input and output
line-termination sequences.</p>
</dd>
</li>
<dt><strong><a name="item_bless">bless REF,CLASSNAME</a></strong>

<dt><strong>bless REF</strong>

<dd>
<p>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 <a href="#item_bless"><code>bless</code></a> 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="../../lib/Pod/perltoot.html">the perltoot manpage</a> and <a href="../../lib/Pod/perlobj.html">the perlobj manpage</a> for more about the blessing (and blessings)
of objects.</p>
</dd>
<dd>
<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>
</dd>
<dd>
<p>See <a href="../../lib/Pod/perlmod.html#perl_modules">Perl Modules in the perlmod manpage</a>.</p>
</dd>
</li>
<dt><strong><a name="item_caller">caller EXPR</a></strong>

<dt><strong>caller</strong>

<dd>
<p>Returns the context of the current subroutine call.  In scalar context,
returns the caller's package name if there is a caller, that is, if
we're in a subroutine or <a href="#item_eval"><code>eval</code></a> or <a href="#item_require"><code>require</code></a>, and the undefined value
otherwise.  In list context, returns</p>
</dd>
<dd>
<pre>
    <span class="operator">(</span><span class="variable">$package</span><span class="operator">,</span> <span class="variable">$filename</span><span class="operator">,</span> <span class="variable">$line</span><span class="operator">)</span> <span class="operator">=</span> <span class="keyword">caller</span><span class="operator">;</span>
</pre>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?