perlport.html

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

HTML
884
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perlport - Writing portable Perl</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>perlport - Writing portable Perl</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<li><a href="#issues">ISSUES</a></li>
	<ul>

		<li><a href="#newlines">Newlines</a></li>
		<li><a href="#numbers_endianness_and_width">Numbers endianness and Width</a></li>
		<li><a href="#files_and_filesystems">Files and Filesystems</a></li>
		<li><a href="#system_interaction">System Interaction</a></li>
		<li><a href="#command_names_versus_file_pathnames">Command names versus file pathnames</a></li>
		<li><a href="#networking">Networking</a></li>
		<li><a href="#interprocess_communication__ipc_">Interprocess Communication (IPC)</a></li>
		<li><a href="#external_subroutines__xs_">External Subroutines (XS)</a></li>
		<li><a href="#standard_modules">Standard Modules</a></li>
		<li><a href="#time_and_date">Time and Date</a></li>
		<li><a href="#character_sets_and_character_encoding">Character sets and character encoding</a></li>
		<li><a href="#internationalisation">Internationalisation</a></li>
		<li><a href="#system_resources">System Resources</a></li>
		<li><a href="#security">Security</a></li>
		<li><a href="#style">Style</a></li>
	</ul>

	<li><a href="#cpan_testers">CPAN Testers</a></li>
	<li><a href="#platforms">PLATFORMS</a></li>
	<ul>

		<li><a href="#unix">Unix</a></li>
		<li><a href="#dos_and_derivatives">DOS and Derivatives</a></li>
		<li><a href="#mac_os">Mac&nbsp;OS</a></li>
		<li><a href="#vms">VMS</a></li>
		<li><a href="#vos">VOS</a></li>
		<li><a href="#ebcdic_platforms">EBCDIC Platforms</a></li>
		<li><a href="#acorn_risc_os">Acorn RISC OS</a></li>
		<li><a href="#other_perls">Other perls</a></li>
	</ul>

	<li><a href="#function_implementations">FUNCTION IMPLEMENTATIONS</a></li>
	<ul>

		<li><a href="#alphabetical_listing_of_perl_functions">Alphabetical Listing of Perl Functions</a></li>
	</ul>

	<li><a href="#supported_platforms">Supported Platforms</a></li>
	<li><a href="#see_also">SEE ALSO</a></li>
	<li><a href="#authors___contributors">AUTHORS / CONTRIBUTORS</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlport - Writing portable Perl</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>Perl runs on numerous operating systems.  While most of them share
much in common, they also have their own unique features.</p>
<p>This document is meant to help you to find out what constitutes portable
Perl code.  That way once you make a decision to write portably,
you know where the lines are drawn, and you can stay within them.</p>
<p>There is a tradeoff between taking full advantage of one particular
type of computer and taking advantage of a full range of them.
Naturally, as you broaden your range and become more diverse, the
common factors drop, and you are left with an increasingly smaller
area of common ground in which you can operate to accomplish a
particular task.  Thus, when you begin attacking a problem, it is
important to consider under which part of the tradeoff curve you
want to operate.  Specifically, you must decide whether it is
important that the task that you are coding have the full generality
of being portable, or whether to just get the job done right now.
This is the hardest choice to be made.  The rest is easy, because
Perl provides many choices, whichever way you want to approach your
problem.</p>
<p>Looking at it another way, writing portable code is usually about
willfully limiting your available choices.  Naturally, it takes
discipline and sacrifice to do that.  The product of portability
and convenience may be a constant.  You have been warned.</p>
<p>Be aware of two important points:</p>
<dl>
<dt><strong><a name="item_not_all_perl_programs_have_to_be_portable">Not all Perl programs have to be portable</a></strong>

<dd>
<p>There is no reason you should not use Perl as a language to glue Unix
tools together, or to prototype a Macintosh application, or to manage the
Windows registry.  If it makes no sense to aim for portability for one
reason or another in a given program, then don't bother.</p>
</dd>
</li>
<dt><strong><a name="item_nearly_all_of_perl_already_is_portable">Nearly all of Perl already <em>is</em> portable</a></strong>

<dd>
<p>Don't be fooled into thinking that it is hard to create portable Perl
code.  It isn't.  Perl tries its level-best to bridge the gaps between
what's available on different platforms, and all the means available to
use those features.  Thus almost all Perl code runs on any machine
without modification.  But there are some significant issues in
writing portable code, and this document is entirely about those issues.</p>
</dd>
</li>
</dl>
<p>Here's the general rule: When you approach a task commonly done
using a whole range of platforms, think about writing portable
code.  That way, you don't sacrifice much by way of the implementation
choices you can avail yourself of, and at the same time you can give
your users lots of platform choices.  On the other hand, when you have to
take advantage of some unique feature of a particular platform, as is
often the case with systems programming (whether for Unix, Windows,
Mac&nbsp;OS, VMS, etc.), consider writing platform-specific code.</p>
<p>When the code will run on only two or three operating systems, you
may need to consider only the differences of those particular systems.
The important thing is to decide where the code will run and to be
deliberate in your decision.</p>
<p>The material below is separated into three main sections: main issues of
portability (<a href="#issues">ISSUES</a>), platform-specific issues (<a href="#platforms">PLATFORMS</a>), and
built-in perl functions that behave differently on various ports
(<a href="#function_implementations">FUNCTION IMPLEMENTATIONS</a>).</p>
<p>This information should not be considered complete; it includes possibly
transient information about idiosyncrasies of some of the ports, almost
all of which are in a state of constant evolution.  Thus, this material
should be considered a perpetual work in progress
(<code>&lt;IMG SRC=&quot;yellow_sign.gif&quot; ALT=&quot;Under Construction&quot;&gt;</code>).</p>
<p>
</p>
<hr />
<h1><a name="issues">ISSUES</a></h1>
<p>
</p>
<h2><a name="newlines">Newlines</a></h2>
<p>In most operating systems, lines in files are terminated by newlines.
Just what is used as a newline may vary from OS to OS.  Unix
traditionally uses <code>\012</code>, one type of DOSish I/O uses <code>\015\012</code>,
and Mac&nbsp;OS uses <code>\015</code>.</p>
<p>Perl uses <code>\n</code> to represent the &quot;logical&quot; newline, where what is
logical may depend on the platform in use.  In MacPerl, <code>\n</code> always
means <code>\015</code>.  In DOSish perls, <code>\n</code> usually means <code>\012</code>, but
when accessing a file in &quot;text&quot; mode, STDIO translates it to (or
from) <code>\015\012</code>, depending on whether you're reading or writing.
Unix does the same thing on ttys in canonical mode.  <code>\015\012</code>
is commonly referred to as CRLF.</p>
<p>A common cause of unportable programs is the misuse of <a href="../../lib/Pod/perlfunc.html#item_chop"><code>chop()</code></a> to trim
newlines:</p>
<pre>
    <span class="comment"># XXX UNPORTABLE!</span>
    <span class="keyword">while</span><span class="operator">(&lt;</span><span class="variable">FILE</span><span class="operator">&gt;)</span> <span class="operator">{</span>
        <span class="keyword">chop</span><span class="operator">;</span>
        <span class="variable">@array</span> <span class="operator">=</span> <span class="keyword">split</span><span class="operator">(</span><span class="regex">/:/</span><span class="operator">);</span>
        <span class="comment">#...</span>
    <span class="operator">}</span>
</pre>
<p>You can get away with this on Unix and Mac OS (they have a single

⌨️ 快捷键说明

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