?? rhl29.htm
字號:
<HTML>
<HEAD>
<TITLE>Red Hat Linux Unleashed rhl29.htm </TITLE>
<LINK REL="ToC" HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm">
<LINK REL="Index" HREF="htindex.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/htindex.htm">
<LINK REL="Next" HREF="rhl30.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl30.htm">
<LINK REL="Previous" HREF="rhl28.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl28.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<A NAME="I0"></A>
<H2>Red Hat Linux Unleashed rhl29.htm</H2>
<P ALIGN=LEFT>
<A HREF="rhl28.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl28.htm" TARGET="_self"><IMG SRC="purprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm" TARGET="_self"><IMG SRC="purtoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="rhl30.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl30.htm" TARGET="_self"><IMG SRC="purnext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<HR ALIGN=CENTER>
<P>
<UL>
<UL>
<UL>
<LI>
<A HREF="#E68E230" >Variables in Perl</A>
<UL>
<UL>
<LI>
<A HREF="#E70E9" >Code Blocks in Loops</A></UL>
<LI>
<A HREF="#E69E362" >Quoting Rules</A></UL>
<LI>
<A HREF="#E68E231" >Arrays and Associative Arrays</A>
<UL>
<LI>
<A HREF="#E69E363" >Associative Arrays</A></UL>
<LI>
<A HREF="#E68E232" >Array Operations</A>
<LI>
<A HREF="#E68E233" >File Handles and Operators</A>
<LI>
<A HREF="#E68E234" >Working with Patterns</A>
<LI>
<A HREF="#E68E235" >Subroutines</A>
<LI>
<A HREF="#E68E236" >More Features in Perl 5</A>
<LI>
<A HREF="#E68E237" >Summary</A></UL></UL></UL>
<HR ALIGN=CENTER>
<A NAME="E66E29"></A>
<H1 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>29</B></FONT></CENTER></H1>
<BR>
<A NAME="E67E29"></A>
<H2 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>Perl</B></FONT></CENTER></H2>
<BR>
<P>Perl stands for Practical Extraction and Report Language and is a free utility that comes with Linux versions. Perl was developed by Larry Wall. The -v option will print the version number of Perl you are running. This book is written for Perl 5.002,
and not for 5.001m which comes with your Linux system, since the later version has fewer bugs and more features. The full release of 5.002 is available at the FTP site ftp.mox. The latest release is available from the Web site <A
HREF="tppmsgs/msgs0.htm#99" tppabs="http://mox.perl.com/perl/info/software.html">http://mox.perl.com/perl/info/software.html</A> as perl5.002.tar.gz. Installation is very easy if you follow the read me directions.
<BR>
<P>Perl is a program just like any other program on your system, only it's more powerful than most other programs because it combines the features of awk, grep, sed, and C all in one language! To run Perl, you can simply type perl at the prompt and type in
your code. In almost all cases, though, you will want to keep your Perl code in files just like shell scripts. A Perl program is referred to as a script.
<BR>
<P>Perl programs are interpreted and compiled by the Perl program before executing the program. To create a Perl program you use the syntax of #!program_name as the first line of an executable file to create a Perl script. The following two lines are a
valid Perl script:
<BR>
<PRE>
<FONT COLOR="#000080">#!/usr/bin/perl
print "I be Perl\n";</FONT></PRE>
<P>On some Linux systems, the path to Perl is /sbin/perl, and on some it's /usr/local/bin/perl. The libraries for the Perl program on some other machines will be located in the /usr/bin/perl5, /usr/lib/perl5, or the /usr/local/lib/perl5 directory. Use a
find command to see if you can locate Perl on your system.
<BR>
<P>You can run programs via the command line with the -e switch to perl. For example, entering the following command at the prompt will print "Howdy!".
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">$ perl -e 'print "Howdy";\n'</FONT></PRE>
<P>In all but the shortest of Perl programs, you will use a file to store your Perl code as a script. Using a script file offers you the ease of not having to type commands interactively and thus not being able to correct typing errors easily. A script
file also provides a written record of which commands to use to do some task.
<BR>
<P>To fire off a command on all lines in the input, use the -p option. Thus,
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">$perl -p -e 's/old/new/g' test.txt</FONT></PRE>
<P>will run the command to substitute all strings old with new on each line from the file test.txt. If you use the -p option, it will print each line as it is read in.
<BR>
<P>Let's start with an introduction to the Perl language.
<BR>
<BR>
<A NAME="E68E230"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Variables in Perl</B></FONT></CENTER></H3>
<BR>
<P>Perl has three basic types of variables: scalars, arrays, and associative arrays. A scalar variable is anything that can hold one number or a string. An array stores many scalars in a sequence, where each scalar can be indexed using a number starting
from 0. An associative is like an array in that it stores strings in sequenceb but uses another string as an index to address individual items instead of a number.
<BR>
<P>Let's start with scalar variables.
<BR>
<P>The syntax for a scalar variable is $variable_name. A variable name is set up and addressed in the same way as Bourne shell variables. To assign values to a scalar, you use statements like these:
<BR>
<PRE>
<FONT COLOR="#000080">$name = "Kamran";
$number= 100;
$phone_Number = '555-1232';</FONT></PRE>
<P>A variable in Perl is evaluated at runtime to derive a value that is one of the following: a string, a number, or a pointer to scalar. It's important to place a $ sign in front of the variable name, or it will be interpreted as a file handle.
<BR>
<P>To print out the value of a variable, you would use a print statement. To print the value of $name, you would make a call:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">print $name;</FONT></PRE>
<P>The value of $name is printed to the screen. Perl scripts expect input from a standard input (the keyboard) and write to the standard output.
<BR>
<BR>
<A NAME="E70E9"></A>
<H5 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Code Blocks in Loops</B></FONT></CENTER></H5>
<BR>
<P>Variables and assignment statements exist in code blocks. Each code block is a section of code between two curly braces. The loop construct and conditional expressions used in Perl delimit code blocks with curly braces. The following are some examples
of code blocks available in Perl:
<BR>
<PRE>
<FONT COLOR="#000080">while(condition) {
... execute code here while condition is true;
}
until(condition) { # opposite of while statement.
... execute code here while condition is false;
}
do {
... do this at least once ...
... stop if condition is false ...
} while(condition);
do {
... do this at least once ...
... stop if condition is true ...
} until(condition);
if (condition1) {
condition1_code true;
} else {
...no condition1 up to conditionN is true;
}
if (condition1) {
...condition1_code true;
} elsif (condition2) {
condition1_code true;
....
} elsif (conditionN) {
conditionN_code true;
} else {
...no condition from 1 up to N is true;
}
unless (condition1) { # opposite of "if" statement.
do this if condition is false;
}</FONT></PRE>
<P>The condition in the preceding blocks of code is anything from a Perl variable to an expression that returns either a true or false value. A true value is a nonzero or a nonempty string.
<BR>
<P>Code blocks can be declared within code blocks to create levels of code blocks. Variables declared in one code block are usually global to the rest of the program. To keep the scope of the variable limited to the code block in which it is declared, use
the my $variableName syntax. If you declare with local $variableName syntax, the $variableName will be available to all lower levels but not outside the code block. So if your code calls another subroutine, any variables declared with the word local could
be modified by the called subroutine; however, those variables declared by the my keyword will not be visible to the called subroutine.
<BR>
<P>Variables in code blocks are also declared the first time they are assigned a value. This creation includes arrays and strings. Variables are then evaluated by the parser when they appear in code, and even in strings. There are times when you do not
want the variable to be evaluated. This is the time when you should be aware of quoting rules in Perl.
<BR>
<BR>
<A NAME="E69E362"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Quoting Rules</B></FONT></CENTER></H4>
<BR>
<P>Three different types of quotes can be used on Perl. Double quotes ("") are used to enclose strings. Any scalars in double quoted strings are evaluated by Perl. To force Perl not to evaluate anything in a quote, you will have to use single
quotes ('). Finally, to run some values in a shell program and get its return value back, use the back quote (`) symbol. To see an example of how it works, see the sample Perl script in Listing 29.1.
<BR>
<P>
<FONT COLOR="#000080"><B>Listing 29.1. Quoting in a Perl script.</B></FONT>
<BR>
<PRE>
<FONT COLOR="#000080">1 #!/usr/bin/perl
2 $folks="100";
3 print "\$folks = $folks \n";
4 print '\$folks = $folks \n';
5 print "\n\n BEEP! \a \LSOME BLANK \ELINES HERE \n\n";
6 $date = 'date +%D';
7 print "Today is [$date] \n";
8 chop $date;
9 print "Date after chopping off carriage return: [".$date."]\n";</FONT></PRE>
<P>The output from the code in Listing 29.1 is shown here. The line numbers shown in the listing are for the benefit of illustration only and are not present in the actual file.
<BR>
<PRE>
<FONT COLOR="#000080">$folks = 100
$folks = $folks \n
BEEP! some blank LINES HERE
Today is [03/29/96
]
Date after chopping off carriage return: [03/29/96]</FONT></PRE>
<P>Let's go over the code shown in Listing 29.1.
<BR>
<P>Line 1 is the mandatory first line of the Perl script.
<BR>
<P>Line 2 assigns a string value to $folks variable. Note that you did not have to declare the variable $folks; it was created when used for the first time.
<BR>
<P>Line 3 prints the value of $folks in between double quotes. The $ sign in $folks has to be escaped with the \ character to prevent Perl from evaluating the value of $folks instead of printing the word $folks verbatim in the output like this:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">$folks = 100</FONT></PRE>
<P>In line 4, Perl does not evaluate anything between the single quotes. The entire contents of the line are left untouched and printed out here:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">\$folks = $folks \n</FONT></PRE>
<P>Perl has several special characters to format text data for you. Line 5 prints multiple blank lines with the \n character and beeps at the terminal. Two \n characters are needed to proceed from the end of the current line, skip line and position the
cursor at the next line. Notice how the words SOME BLANK are printed in lowercase? This is because they are encased between the \L and \E special characters, which forces all characters to be lowercase. Some of these special characters are listed in Table
29.1.
<BR>
<BR>
<P ALIGN=CENTER>
<CENTER>
<FONT COLOR="#000080"><B>Table 29.1. Special characters in Perl.</B></FONT></CENTER>
<BR>
<TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Character</I>
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Meaning</I></FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\n
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
New line (line feed)</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\r
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Carriage return (MSDOS)</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\t
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Tab</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\a
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Beep</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\b
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Backspace</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\L \E
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Lowercase all characters in between \L and \E</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\l
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Lowercase next character</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\U \E
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Uppercase all characters in between \U and \E</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\u
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Uppercase next character</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\cC
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Insert control character "C"</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\x##
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Hex number in ##, such as \x1d</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\0ooo
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Octal number in ooo, such as \0213</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\\
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
A backslash</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -