📄 0487-0490.html
字号:
<HTML>
<HEAD>
<TITLE>Developer.com - Online Reference Library - 0672311739:RED HAT LINUX 2ND EDITION:Perl Programming</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!-- ISBN=0672311739 //-->
<!-- TITLE=RED HAT LINUX 2ND EDITION //-->
<!-- AUTHOR=DAVID PITTS ET AL //-->
<!-- PUBLISHER=MACMILLAN //-->
<!-- IMPRINT=SAMS PUBLISHING //-->
<!-- PUBLICATION DATE=1998 //-->
<!-- CHAPTER=24 //-->
<!-- PAGES=0487-0498 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="../ch23/0484-0486.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0491-0493.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-487"><P>Page 487</P></A>
<H3><A NAME="ch24_ 1">
CHAPTER 24
</A></H3>
<H2>
Perl Programming
</H2>
<P><B>by Rich Bowen
</B>
</P>
<H3><A NAME="ch24_ 2">
IN THIS CHAPTER
</A></H3>
<UL>
<LI> A Simple Perl Program 488
<LI> Perl Variables and Data Structures 489
<LI> Conditional Statements: if/else 489
<LI> Looping 490
<LI> Regular Expressions 491
<LI> Access to the Shell 492
<LI> Command-Line Mode 492
<LI> Automation Using Perl 493
<LI> For More Information 496
</UL>
<A NAME="PAGENUM-488"><P>Page 488</P></A>
<P>Perl (Practical Extraction and Report Language)
was developed in 1986 by Larry Wall. It has grown in popularity, and is now one
of the favorite scripting languages for UNIX platforms.
</P>
<P>Perl is similar in syntax to C, but also contains much of the style of UNIX shell scripting.
And, thrown in with that, it contains the best features of every other programming language
that you have ever used.
</P>
<P>Perl is an interpreted language rather than a compiled one, which is either an advantage or
a disadvantage, depending on how you look at it. Perl has been ported to virtually every
operating system out there, and most Perl programs will run without modifications on any
system that you move them to. That is certainly an advantage. In addition, for the small, almost
trivial, applications used in everyday server maintenance, you might not want to go to all the
trouble of writing the code in C and compiling it.
</P>
<P>Perl is very forgiving about such things as declaring variables, allocating and
deallocating memory, and variable types, so you can get down to the actual business of writing code.
In fact, those concepts really do not exist in Perl. This results in programs that are short and to
the point, while similar programs in C, for example, might spend half the code
declaring variables.
</P>
<H3><A NAME="ch24_ 3">
A Simple Perl Program
</A></H3>
<P>To introduce you to the absolute basics of Perl programming, Listing 24.1 illustrates a
trivial Perl program.
</P>
<P>Listing 24.1. A trivial Perl program.
</P>
<!-- CODE SNIP //-->
<PRE>
#!/usr/bin/perl
print "Red Hat Unleashed, 2nd edition\n";
</PRE>
<!-- END CODE SNIP //-->
<P>That's the whole thing. Type that in, save it to a file called
trivial.pl, chmod +x it, and exe-cute it.
</P>
<P>If you are at all familiar with shell scripting languages, this will look very familiar. Perl
combines the simplicity of shell scripting with the power of a full-fledged programming language.
</P>
<P>The first line of this program indicates to the operating system where to find the Perl
interpreter. This is standard procedure with shell scripts, and you have already seen this syntax
in Chapter 21, "Shell Programming."
</P>
<P>If /usr/bin/perl is not the correct location for Perl on your system, you can find out where
it is located by typing which perl at the command line. If you do not have Perl installed,
you might want to skip forward to the section titled "For More Information" to find out
where you can obtain the Perl interpreter.
</P>
<P>The second line does precisely what you would expect it to do—it prints the text enclosed
in quotes. The \n notation is used for a newline character.
</P>
<A NAME="PAGENUM-489"><P>Page 489</P></A>
<H3><A NAME="ch24_ 4">
Perl Variables and Data Structures
</A></H3>
<P>Although it does not have the concept of datatype
(integer, string, char, and so on), Perl has several kinds of variables.
</P>
<P>Scalar variables, indicated as $variable, are interpreted as numbers or strings, as the
context warrants. You can treat a variable as a number one moment and a string the next if the value
of the variable makes sense in that context.
</P>
<P>There is a large collection of special variables in Perl, such as
$_, $$, and $<, which Perl keeps track of, and you can use if you want to.
($_ is the default input variable, $$ is the process
ID, and $< is the user ID.) As you become more familiar with Perl, you will find yourself
using these variables, and people will accuse you of writing "read-only" code.
</P>
<P>Arrays, indicated as @array, contain one or more elements, which can be referred to by
index. For example, $names[12] gives me the 13th element in the array
@names. (It's important to remember that numbering starts with 0.)
</P>
<P>Associative arrays, indicated by %assoc_array, store values that can be referenced by key.
For example, $days{Feb} will give me the element in the associative array
%days that corresponds with Feb.
</P>
<P>The following line of Perl code lists all the elements in an associative array (the
foreach construct is covered later in this chapter):
</P>
<!-- CODE SNIP //-->
<PRE>
foreach $key (keys %assoc){
print "$key = $assoc{$key}\n"};
<!-- END CODE SNIP //-->
</PRE>
<CENTER>
<TABLE BGCOLOR="#FFFF99">
<TR><TD><B>
NOTE
</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
$_ is the "default" variable in Perl. In this example, the loop variable is
$_ because none was specified.
</BLOCKQUOTE></TD></TR>
</TABLE></CENTER>
<H3>
Conditional Statements: if/else
</H3>
<P>The syntax of the Perl if/else structure is as follows:
</P>
<!-- CODE //-->
<PRE>
if (condition) {
statement(s)
}
elsif (condition) {
statement(s)
}
else {
statement(s)
}
</PRE>
<!-- END CODE //-->
<A NAME="PAGENUM-490"><P>Page 490</P></A>
<P>condition can be any statement or comparison. If the statement returns any true value,
the statement(s) will be executed. Here, true is defined as
</P>
<UL>
<LI> Any nonzero number
<LI> Any nonzero string; that is, any string that is not 0 or empty
<LI> Any conditional that returns a true value
</UL>
<P>For example, the following piece of code uses the
if/else structure:
</P>
<!-- CODE //-->
<PRE>
if ($favorite eq "chocolate") {
print "I like chocolate too.\n"
}
elsif ($favorite eq "spinach") {
print "Oh, I don't like spinach.\n";
}
else {
print "Your favorite food is $favorite.\n"
}
</PRE>
<!-- END CODE //-->
<H3><A NAME="ch24_ 5">
Looping
</A></H3>
<P>Perl has four looping constructs: for, foreach,
while, and until.
</P>
<H5><A NAME="ch24_ 6">
for
</A></H5>
<P>The for construct performs a statement (or set of statements) for a set of conditions defined
as follows:
</P>
<!-- CODE SNIP //-->
<PRE>
for (start condition; end condition; increment function) {
statement(s)
}
</PRE>
<!-- END CODE SNIP //-->
<P>At the beginning of the loop, the start condition is set. Each time the loop is executed,
the increment function is performed until the end condition is achieved. This looks much like
the traditional for/next loop. The following code is an example of a
for loop:
</P>
<!-- CODE SNIP //-->
<PRE>
for ($i=1; $i<=10; $i++) {
print "$i\n"
}
</PRE>
<!-- END CODE SNIP //-->
<H5><A NAME="ch24_ 7">
foreach
</A></H5>
<P>The foreach construct performs a statement (or set of statements) for each element in a
set, such as a list or array:
</P>
<!-- CODE SNIP //-->
<PRE>
foreach $name (@names) {
print "$name\n"
}
</PRE>
<!-- END CODE SNIP //-->
<P><CENTER>
<a href="../ch23/0484-0486.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0491-0493.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -