perlopentut.html
来自「perl教程」· HTML 代码 · 共 780 行 · 第 1/5 页
HTML
780 行
<?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>perlopentut - tutorial on opening things in 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>perlopentut - tutorial on opening things in 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="#open_la_shell">Open à la shell</a></li>
<ul>
<li><a href="#simple_opens">Simple Opens</a></li>
<li><a href="#indirect_filehandles">Indirect Filehandles</a></li>
<li><a href="#pipe_opens">Pipe Opens</a></li>
<li><a href="#the_minus_file">The Minus File</a></li>
<li><a href="#mixing_reads_and_writes">Mixing Reads and Writes</a></li>
<li><a href="#filters">Filters</a></li>
</ul>
<li><a href="#open_la_c">Open à la C</a></li>
<ul>
<li><a href="#permissions_la_mode">Permissions à la mode</a></li>
</ul>
<li><a href="#obscure_open_tricks">Obscure Open Tricks</a></li>
<ul>
<li><a href="#reopening_files__dups_">Re-Opening Files (dups)</a></li>
<li><a href="#dispelling_the_dweomer">Dispelling the Dweomer</a></li>
<li><a href="#paths_as_opens">Paths as Opens</a></li>
<li><a href="#single_argument_open">Single Argument Open</a></li>
<li><a href="#playing_with_stdin_and_stdout">Playing with STDIN and STDOUT</a></li>
</ul>
<li><a href="#other_i_o_issues">Other I/O Issues</a></li>
<ul>
<li><a href="#opening_nonfile_files">Opening Non-File Files</a></li>
<li><a href="#opening_named_pipes">Opening Named Pipes</a></li>
<li><a href="#opening_sockets">Opening Sockets</a></li>
<li><a href="#binary_files">Binary Files</a></li>
<li><a href="#file_locking">File Locking</a></li>
<li><a href="#io_layers">IO Layers</a></li>
</ul>
<li><a href="#see_also">SEE ALSO</a></li>
<li><a href="#author_and_copyright">AUTHOR and COPYRIGHT</a></li>
<li><a href="#history">HISTORY</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlopentut - tutorial on opening things in Perl</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>Perl has two simple, built-in ways to open files: the shell way for
convenience, and the C way for precision. The shell way also has 2- and
3-argument forms, which have different semantics for handling the filename.
The choice is yours.</p>
<p>
</p>
<hr />
<h1><a name="open_la_shell">Open à la shell</a></h1>
<p>Perl's <a href="../../lib/Pod/perlfunc.html#item_open"><code>open</code></a> function was designed to mimic the way command-line
redirection in the shell works. Here are some basic examples
from the shell:</p>
<pre>
$ myprogram file1 file2 file3
$ myprogram < inputfile
$ myprogram > outputfile
$ myprogram >> outputfile
$ myprogram | otherprogram
$ otherprogram | myprogram</pre>
<p>And here are some more advanced examples:</p>
<pre>
$ otherprogram | myprogram f1 - f2
$ otherprogram 2>&1 | myprogram -
$ myprogram <&3
$ myprogram >&4</pre>
<p>Programmers accustomed to constructs like those above can take comfort
in learning that Perl directly supports these familiar constructs using
virtually the same syntax as the shell.</p>
<p>
</p>
<h2><a name="simple_opens">Simple Opens</a></h2>
<p>The <a href="../../lib/Pod/perlfunc.html#item_open"><code>open</code></a> function takes two arguments: the first is a filehandle,
and the second is a single string comprising both what to open and how
to open it. <a href="../../lib/Pod/perlfunc.html#item_open"><code>open</code></a> returns true when it works, and when it fails,
returns a false value and sets the special variable <a href="../../lib/Pod/perlvar.html#item___"><code>$!</code></a> to reflect
the system error. If the filehandle was previously opened, it will
be implicitly closed first.</p>
<p>For example:</p>
<pre>
<span class="keyword">open</span><span class="operator">(</span><span class="variable">INFO</span><span class="operator">,</span> <span class="string">"datafile"</span><span class="operator">)</span> <span class="operator">||</span> <span class="keyword">die</span><span class="operator">(</span><span class="string">"can't open datafile: $!"</span><span class="operator">);</span>
<span class="keyword">open</span><span class="operator">(</span><span class="variable">INFO</span><span class="operator">,</span> <span class="string">"< datafile"</span><span class="operator">)</span> <span class="operator">||</span> <span class="keyword">die</span><span class="operator">(</span><span class="string">"can't open datafile: $!"</span><span class="operator">);</span>
<span class="keyword">open</span><span class="operator">(</span><span class="variable">RESULTS</span><span class="operator">,</span><span class="string">"> runstats"</span><span class="operator">)</span> <span class="operator">||</span> <span class="keyword">die</span><span class="operator">(</span><span class="string">"can't open runstats: $!"</span><span class="operator">);</span>
<span class="keyword">open</span><span class="operator">(</span><span class="variable">LOG</span><span class="operator">,</span> <span class="string">">> logfile "</span><span class="operator">)</span> <span class="operator">||</span> <span class="keyword">die</span><span class="operator">(</span><span class="string">"can't open logfile: $!"</span><span class="operator">);</span>
</pre>
<p>If you prefer the low-punctuation version, you could write that this way:</p>
<pre>
<span class="keyword">open</span> <span class="variable">INFO</span><span class="operator">,</span> <span class="string">"< datafile"</span> <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"can't open datafile: $!"</span><span class="operator">;</span>
<span class="keyword">open</span> <span class="variable">RESULTS</span><span class="operator">,</span><span class="string">"> runstats"</span> <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"can't open runstats: $!"</span><span class="operator">;</span>
<span class="keyword">open</span> <span class="variable">LOG</span><span class="operator">,</span> <span class="string">">> logfile "</span> <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"can't open logfile: $!"</span><span class="operator">;</span>
</pre>
<p>A few things to notice. First, the leading less-than is optional.
If omitted, Perl assumes that you want to open the file for reading.</p>
<p>Note also that the first example uses the <code>||</code> logical operator, and the
second uses <code>or</code>, which has lower precedence. Using <code>||</code> in the latter
examples would effectively mean</p>
<pre>
<span class="keyword">open</span> <span class="variable">INFO</span><span class="operator">,</span> <span class="operator">(</span> <span class="string">"< datafile"</span> <span class="operator">||</span> <span class="keyword">die</span> <span class="string">"can't open datafile: $!"</span> <span class="operator">);</span>
</pre>
<p>which is definitely not what you want.</p>
<p>The other important thing to notice is that, just as in the shell,
any whitespace before or after the filename is ignored. This is good,
because you wouldn't want these to do different things:</p>
<pre>
open INFO, "<datafile"
open INFO, "< datafile"
open INFO, "< datafile"</pre>
<p>Ignoring surrounding whitespace also helps for when you read a filename
in from a different file, and forget to trim it before opening:</p>
<pre>
<span class="variable">$filename</span> <span class="operator">=</span> <span class="operator"><</span><span class="variable">INFO</span><span class="operator">>;</span> <span class="comment"># oops, \n still there</span>
<span class="keyword">open</span><span class="operator">(</span><span class="variable">EXTRA</span><span class="operator">,</span> <span class="string">"< $filename"</span><span class="operator">)</span> <span class="operator">||</span> <span class="keyword">die</span> <span class="string">"can't open $filename: $!"</span><span class="operator">;</span>
</pre>
<p>This is not a bug, but a feature. Because <a href="../../lib/Pod/perlfunc.html#item_open"><code>open</code></a> mimics the shell in
its style of using redirection arrows to specify how to open the file, it
also does so with respect to extra whitespace around the filename itself
as well. For accessing files with naughty names, see
<a href="#dispelling_the_dweomer">Dispelling the Dweomer</a>.</p>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?