perlintro.html
来自「perl教程」· HTML 代码 · 共 820 行 · 第 1/4 页
HTML
820 行
<span class="keyword">print</span> <span class="variable">OUTFILE</span> <span class="variable">$record</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="variable">LOGFILE</span> <span class="variable">$logmessage</span><span class="operator">;</span>
</pre>
<p>When you're done with your filehandles, you should <a href="../../lib/Pod/perlfunc.html#item_close"><code>close()</code></a> them
(though to be honest, Perl will clean up after you if you forget):</p>
<pre>
<span class="keyword">close</span> <span class="variable">INFILE</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="regular_expressions">Regular expressions</a></h2>
<p>Perl's regular expression support is both broad and deep, and is the
subject of lengthy documentation in <a href="../../lib/Pod/perlrequick.html">the perlrequick manpage</a>, <a href="../../lib/Pod/perlretut.html">the perlretut manpage</a>, and
elsewhere. However, in short:</p>
<dl>
<dt><strong><a name="item_simple_matching">Simple matching</a></strong>
<dd>
<pre>
<span class="keyword">if</span> <span class="operator">(</span><span class="regex">/foo/</span><span class="operator">)</span> <span class="operator">{</span> <span class="operator">...</span> <span class="operator">}</span> <span class="comment"># true if $_ contains "foo"</span>
<span class="keyword">if</span> <span class="operator">(</span><span class="variable">$a</span> <span class="operator">=~</span> <span class="regex">/foo/</span><span class="operator">)</span> <span class="operator">{</span> <span class="operator">...</span> <span class="operator">}</span> <span class="comment"># true if $a contains "foo"</span>
</pre>
</dd>
<dd>
<p>The <code>//</code> matching operator is documented in <a href="../../lib/Pod/perlop.html">the perlop manpage</a>. It operates on
<a href="../../lib/Pod/perlvar.html#item___"><code>$_</code></a> by default, or can be bound to another variable using the <code>=~</code>
binding operator (also documented in <a href="../../lib/Pod/perlop.html">the perlop manpage</a>).</p>
</dd>
<dt><strong><a name="item_simple_substitution">Simple substitution</a></strong>
<dd>
<pre>
<span class="regex">s/foo/bar/</span><span class="operator">;</span> <span class="comment"># replaces foo with bar in $_</span>
<span class="variable">$a</span> <span class="operator">=~</span> <span class="regex">s/foo/bar/</span><span class="operator">;</span> <span class="comment"># replaces foo with bar in $a</span>
<span class="variable">$a</span> <span class="operator">=~</span> <span class="regex">s/foo/bar/g</span><span class="operator">;</span> <span class="comment"># replaces ALL INSTANCES of foo with bar in $a</span>
</pre>
</dd>
<dd>
<p>The <a href="../../lib/Pod/perlfunc.html#item_s_"><code>s///</code></a> substitution operator is documented in <a href="../../lib/Pod/perlop.html">the perlop manpage</a>.</p>
</dd>
<dt><strong><a name="item_more_complex_regular_expressions">More complex regular expressions</a></strong>
<dd>
<p>You don't just have to match on fixed strings. In fact, you can match
on just about anything you could dream of by using more complex regular
expressions. These are documented at great length in <a href="../../lib/Pod/perlre.html">the perlre manpage</a>, but for
the meantime, here's a quick cheat sheet:</p>
</dd>
<dd>
<pre>
. a single character
\s a whitespace character (space, tab, newline)
\S non-whitespace character
\d a digit (0-9)
\D a non-digit
\w a word character (a-z, A-Z, 0-9, _)
\W a non-word character
[aeiou] matches a single character in the given set
[^aeiou] matches a single character outside the given set
(foo|bar|baz) matches any of the alternatives specified</pre>
</dd>
<dd>
<pre>
^ start of string
$ end of string</pre>
</dd>
<dd>
<p>Quantifiers can be used to specify how many of the previous thing you
want to match on, where "thing" means either a literal character, one
of the metacharacters listed above, or a group of characters or
metacharacters in parentheses.</p>
</dd>
<dd>
<pre>
<span class="operator">*</span> <span class="variable">zero</span> <span class="keyword">or</span> <span class="variable">more</span> <span class="variable">of</span> <span class="variable">the</span> <span class="variable">previous</span> <span class="variable">thing</span>
<span class="operator">+</span> <span class="variable">one</span> <span class="keyword">or</span> <span class="variable">more</span> <span class="variable">of</span> <span class="variable">the</span> <span class="variable">previous</span> <span class="variable">thing</span>
<span class="operator">?</span> <span class="variable">zero</span> <span class="keyword">or</span> <span class="variable">one</span> <span class="variable">of</span> <span class="variable">the</span> <span class="variable">previous</span> <span class="variable">thing</span>
<span class="operator">{</span><span class="number">3</span><span class="operator">}</span> <span class="variable">matches</span> <span class="variable">exactly</span> <span class="number">3</span> <span class="variable">of</span> <span class="variable">the</span> <span class="variable">previous</span> <span class="variable">thing</span>
<span class="operator">{</span><span class="number">3</span><span class="operator">,</span><span class="number">6</span><span class="operator">}</span> <span class="variable">matches</span> <span class="variable">between</span> <span class="number">3</span> <span class="keyword">and</span> <span class="number">6</span> <span class="variable">of</span> <span class="variable">the</span> <span class="variable">previous</span> <span class="variable">thing</span>
<span class="operator">{</span><span class="number">3</span><span class="operator">,}</span> <span class="variable">matches</span> <span class="number">3</span> <span class="keyword">or</span> <span class="variable">more</span> <span class="variable">of</span> <span class="variable">the</span> <span class="variable">previous</span> <span class="variable">thing</span>
</pre>
</dd>
<dd>
<p>Some brief examples:</p>
</dd>
<dd>
<pre>
<span class="regex">/^\d+/</span> <span class="variable">string</span> <span class="variable">starts</span> <span class="variable">with</span> <span class="variable">one</span> <span class="keyword">or</span> <span class="variable">more</span> <span class="variable">digits</span>
<span class="operator">/^</span><span class="variable">$/</span> <span class="variable">nothing</span> <span class="variable">in</span> <span class="variable">the</span> <span class="variable">string</span> <span class="operator">(</span><span class="variable">start</span> <span class="keyword">and</span> <span class="variable">end</span> <span class="variable">are</span> <span class="variable">adjacent</span><span class="operator">)</span>
<span class="operator">/(\</span><span class="variable">d</span><span class="operator">\</span><span class="regex">s){3}/ a three digits, each followed by a whitespace
character (eg "3 4 5 ")
/(a.)</span><span class="operator">+</span><span class="regex">/ matches a string in which every odd-numbered letter
is a (eg "abacadaf")
</span>
</pre>
</dd>
<dd>
<pre>
<span class="comment"># This loop reads from STDIN, and prints non-blank lines:</span>
<span class="keyword">while</span> <span class="operator">(<>)</span> <span class="operator">{</span>
<span class="keyword">next</span> <span class="keyword">if</span> <span class="regex">/^$/</span><span class="operator">;</span>
<span class="keyword">print</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_parentheses_for_capturing">Parentheses for capturing</a></strong>
<dd>
<p>As well as grouping, parentheses serve a second purpose. They can be
used to capture the results of parts of the regexp match for later use.
The results end up in <code>$1</code>, <code>$2</code> and so on.</p>
</dd>
<dd>
<pre>
<span class="comment"># a cheap and nasty way to break an email address up into parts</span>
</pre>
</dd>
<dd>
<pre>
<span class="keyword">if</span> <span class="operator">(</span><span class="variable">$email</span> <span class="operator">=~</span> <span class="regex">/([^@]+)@(.+)/</span><span class="operator">)</span> <span class="operator">{</span>
<span class="keyword">print</span> <span class="string">"Username is $1\n"</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">"Hostname is $2\n"</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_other_regexp_features">Other regexp features</a></strong>
<dd>
<p>Perl regexps also support backreferences, lookaheads, and all kinds of
other complex details. Read all about them in <a href="../../lib/Pod/perlrequick.html">the perlrequick manpage</a>,
<a href="../../lib/Pod/perlretut.html">the perlretut manpage</a>, and <a href="../../lib/Pod/perlre.html">the perlre manpage</a>.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="writing_subroutines">Writing subroutines</a></h2>
<p>Writing subroutines is easy:</p>
<pre>
<span class="keyword">sub</span><span class="variable"> log </span><span class="operator">{</span>
<span class="keyword">my</span> <span class="variable">$logmessage</span> <span class="operator">=</span> <span class="keyword">shift</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="variable">LOGFILE</span> <span class="variable">$logmessage</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>What's that <a href="../../lib/Pod/perlfunc.html#item_shift"><code>shift</code></a>? Well, the arguments to a subroutine are available
to us as a special array called <a href="../../lib/Pod/perlvar.html#item___"><code>@_</code></a> (see <a href="../../lib/Pod/perlvar.html">the perlvar manpage</a> for more on that).
The default argument to the <a href="../../lib/Pod/perlfunc.html#item_shift"><code>shift</code></a> function just happens to be <a href="../../lib/Pod/perlvar.html#item___"><code>@_</code></a>.
So <code>my $logmessage = shift;</code> shifts the first item off the list of
arguments and assigns it to <code>$logmessage</code>.</p>
<p>We can manipulate <a href="../../lib/Pod/perlvar.html#item___"><code>@_</code></a> in other ways too:</p>
<pre>
<span class="keyword">my</span> <span class="operator">(</span><span class="variable">$logmessage</span><span class="operator">,</span> <span class="variable">$priority</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">@_</span><span class="operator">;</span> <span class="comment"># common</span>
<span class="keyword">my</span> <span class="variable">$logmessage</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="comment"># uncommon, and ugly</span>
</pre>
<p>Subroutines can also return values:</p>
<pre>
<span class="keyword">sub</span><span class="variable"> square </span><span class="operator">{</span>
<span class="keyword">my</span> <span class="variable">$num</span> <span class="operator">=</span> <span class="keyword">shift</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$result</span> <span class="operator">=</span> <span class="variable">$num</span> <span class="operator">*</span> <span class="variable">$num</span><span class="operator">;</span>
<span class="keyword">return</span> <span class="variable">$result</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>For more information on writing subroutines, see <a href="../../lib/Pod/perlsub.html">the perlsub manpage</a>.</p>
<p>
</p>
<h2><a name="oo_perl">OO Perl</a></h2>
<p>OO Perl is relatively simple and is implemented using references which
know what sort of object they are based on Perl's concept of packages.
However, OO Perl is largely beyond the scope of this document.
Read <a href="../../lib/Pod/perlboot.html">the perlboot manpage</a>, <a href="../../lib/Pod/perltoot.html">the perltoot manpage</a>, <a href="../../lib/Pod/perltooc.html">the perltooc manpage</a> and <a href="../../lib/Pod/perlobj.html">the perlobj manpage</a>.</p>
<p>As a beginning Perl programmer, your most common use of OO Perl will be
in using third-party modules, which are documented below.</p>
<p>
</p>
<h2><a name="using_perl_modules">Using Perl modules</a></h2>
<p>Perl modules provide a range of features to help you avoid reinventing
the wheel, and can be downloaded from CPAN ( <a href="http://www.cpan.org/">http://www.cpan.org/</a> ). A
number of popular modules are included with the Perl distribution
itself.</p>
<p>Categories of modules range from text manipulation to network protocols
to database integration to graphics. A categorized list of modules is
also available from CPAN.</p>
<p>To learn how to install modules you download from CPAN, read
<a href="../../lib/Pod/perlmodinstall.html">the perlmodinstall manpage</a></p>
<p>To learn how to use a particular module, use <code>perldoc Module::Name</code>.
Typically you will want to <code>use Module::Name</code>, which will then give
you access to exported functions or an OO interface to the module.</p>
<p><a href="../../lib/Pod/perlfaq.html">the perlfaq manpage</a> contains questions and answers related to many common
tasks, and often provides suggestions for good CPAN modules to use.</p>
<p><a href="../../lib/Pod/perlmod.html">the perlmod manpage</a> describes Perl modules in general. <a href="../../lib/Pod/perlmodlib.html">the perlmodlib manpage</a> lists the
modules which came with your Perl installation.</p>
<p>If you feel the urge to write Perl modules, <a href="../../lib/Pod/perlnewmod.html">the perlnewmod manpage</a> will give you
good advice.</p>
<p>
</p>
<hr />
<h1><a name="author">AUTHOR</a></h1>
<p>Kirrily "Skud" Robert <<a href="mailto:skud@cpan.org">skud@cpan.org</a>></p>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?