perlintro.html
来自「perl教程」· HTML 代码 · 共 820 行 · 第 1/4 页
HTML
820 行
<?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>perlintro -- a brief introduction and overview of 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>perlintro -- a brief introduction and overview of 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>
<ul>
<li><a href="#what_is_perl">What is Perl?</a></li>
<li><a href="#running_perl_programs">Running Perl programs</a></li>
<li><a href="#basic_syntax_overview">Basic syntax overview</a></li>
<li><a href="#perl_variable_types">Perl variable types</a></li>
<li><a href="#variable_scoping">Variable scoping</a></li>
<li><a href="#conditional_and_looping_constructs">Conditional and looping constructs</a></li>
<li><a href="#builtin_operators_and_functions">Builtin operators and functions</a></li>
<li><a href="#files_and_i_o">Files and I/O</a></li>
<li><a href="#regular_expressions">Regular expressions</a></li>
<li><a href="#writing_subroutines">Writing subroutines</a></li>
<li><a href="#oo_perl">OO Perl</a></li>
<li><a href="#using_perl_modules">Using Perl modules</a></li>
</ul>
<li><a href="#author">AUTHOR</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlintro -- a brief introduction and overview of Perl</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>This document is intended to give you a quick overview of the Perl
programming language, along with pointers to further documentation. It
is intended as a "bootstrap" guide for those who are new to the
language, and provides just enough information for you to be able to
read other peoples' Perl and understand roughly what it's doing, or
write your own simple scripts.</p>
<p>This introductory document does not aim to be complete. It does not
even aim to be entirely accurate. In some cases perfection has been
sacrificed in the goal of getting the general idea across. You are
<em>strongly</em> advised to follow this introduction with more information
from the full Perl manual, the table of contents to which can be found
in <a href="../../lib/Pod/perltoc.html">the perltoc manpage</a>.</p>
<p>Throughout this document you'll see references to other parts of the
Perl documentation. You can read that documentation using the <code>perldoc</code>
command or whatever method you're using to read this document.</p>
<p>
</p>
<h2><a name="what_is_perl">What is Perl?</a></h2>
<p>Perl is a general-purpose programming language originally developed for
text manipulation and now used for a wide range of tasks including
system administration, web development, network programming, GUI
development, and more.</p>
<p>The language is intended to be practical (easy to use, efficient,
complete) rather than beautiful (tiny, elegant, minimal). Its major
features are that it's easy to use, supports both procedural and
object-oriented (OO) programming, has powerful built-in support for text
processing, and has one of the world's most impressive collections of
third-party modules.</p>
<p>Different definitions of Perl are given in <a href="../../lib/Pod/perl.html">the perl manpage</a>, <a href="../../lib/Pod/perlfaq1.html">the perlfaq1 manpage</a> and
no doubt other places. From this we can determine that Perl is different
things to different people, but that lots of people think it's at least
worth writing about.</p>
<p>
</p>
<h2><a name="running_perl_programs">Running Perl programs</a></h2>
<p>To run a Perl program from the Unix command line:</p>
<pre>
perl progname.pl</pre>
<p>Alternatively, put this as the first line of your script:</p>
<pre>
<span class="comment">#!/usr/bin/env perl</span>
</pre>
<p>... and run the script as <code>/path/to/script.pl</code>. Of course, it'll need
to be executable first, so <code>chmod 755 script.pl</code> (under Unix).</p>
<p>For more information, including instructions for other platforms such as
Windows and Mac OS, read <a href="../../lib/Pod/perlrun.html">the perlrun manpage</a>.</p>
<p>
</p>
<h2><a name="basic_syntax_overview">Basic syntax overview</a></h2>
<p>A Perl script or program consists of one or more statements. These
statements are simply written in the script in a straightforward
fashion. There is no need to have a <code>main()</code> function or anything of
that kind.</p>
<p>Perl statements end in a semi-colon:</p>
<pre>
<span class="keyword">print</span> <span class="string">"Hello, world"</span><span class="operator">;</span>
</pre>
<p>Comments start with a hash symbol and run to the end of the line</p>
<pre>
<span class="comment"># This is a comment</span>
</pre>
<p>Whitespace is irrelevant:</p>
<pre>
<span class="keyword">print</span>
<span class="string">"Hello, world"</span>
<span class="operator">;</span>
</pre>
<p>... except inside quoted strings:</p>
<pre>
<span class="comment"># this would print with a linebreak in the middle</span>
<span class="keyword">print</span> <span class="string">"Hello
world"</span><span class="operator">;</span>
</pre>
<p>Double quotes or single quotes may be used around literal strings:</p>
<pre>
<span class="keyword">print</span> <span class="string">"Hello, world"</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">'Hello, world'</span><span class="operator">;</span>
</pre>
<p>However, only double quotes "interpolate" variables and special
characters such as newlines (<code>\n</code>):</p>
<pre>
<span class="keyword">print</span> <span class="string">"Hello, $name\n"</span><span class="operator">;</span> <span class="comment"># works fine</span>
<span class="keyword">print</span> <span class="string">'Hello, $name\n'</span><span class="operator">;</span> <span class="comment"># prints $name\n literally</span>
</pre>
<p>Numbers don't need quotes around them:</p>
<pre>
<span class="keyword">print</span> <span class="number">42</span><span class="operator">;</span>
</pre>
<p>You can use parentheses for functions' arguments or omit them
according to your personal taste. They are only required
occasionally to clarify issues of precedence.</p>
<pre>
<span class="keyword">print</span><span class="operator">(</span><span class="string">"Hello, world\n"</span><span class="operator">);</span>
<span class="keyword">print</span> <span class="string">"Hello, world\n"</span><span class="operator">;</span>
</pre>
<p>More detailed information about Perl syntax can be found in <a href="../../lib/Pod/perlsyn.html">the perlsyn manpage</a>.</p>
<p>
</p>
<h2><a name="perl_variable_types">Perl variable types</a></h2>
<p>Perl has three main variable types: scalars, arrays, and hashes.</p>
<dl>
<dt><strong><a name="item_scalars">Scalars</a></strong>
<dd>
<p>A scalar represents a single value:</p>
</dd>
<dd>
<pre>
<span class="keyword">my</span> <span class="variable">$animal</span> <span class="operator">=</span> <span class="string">"camel"</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$answer</span> <span class="operator">=</span> <span class="number">42</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>Scalar values can be strings, integers or floating point numbers, and Perl
will automatically convert between them as required. There is no need
to pre-declare your variable types.</p>
</dd>
<dd>
<p>Scalar values can be used in various ways:</p>
</dd>
<dd>
<pre>
<span class="keyword">print</span> <span class="variable">$animal</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">"The animal is $animal\n"</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">"The square of $answer is "</span><span class="operator">,</span> <span class="variable">$answer</span> <span class="operator">*</span> <span class="variable">$answer</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>There are a number of "magic" scalars with names that look like
punctuation or line noise. These special variables are used for all
kinds of purposes, and are documented in <a href="../../lib/Pod/perlvar.html">the perlvar manpage</a>. The only one you
need to know about for now is <a href="../../lib/Pod/perlvar.html#item___"><code>$_</code></a> which is the "default variable".
It's used as the default argument to a number of functions in Perl, and
it's set implicitly by certain looping constructs.</p>
</dd>
<dd>
<pre>
<span class="keyword">print</span><span class="operator">;</span> <span class="comment"># prints contents of $_ by default</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_arrays">Arrays</a></strong>
<dd>
<p>An array represents a list of values:</p>
</dd>
<dd>
<pre>
<span class="keyword">my</span> <span class="variable">@animals</span> <span class="operator">=</span> <span class="operator">(</span><span class="string">"camel"</span><span class="operator">,</span> <span class="string">"llama"</span><span class="operator">,</span> <span class="string">"owl"</span><span class="operator">);</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?