📄 ch2.htm
字号:
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.
<H3><A NAME="QuotingRules">Quoting Rules</A></H3>
<P>
Three different types of quotes can be used in Perl. Double quotes
(<TT><FONT FACE="Courier">"</FONT></TT>) 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'll
have to use single quotes (<TT><FONT FACE="Courier">'</FONT></TT>).
Anything that looks like code and is not quoted is interpreted
as code by the Perl interpreter, which attempts to evaluate the
code as an expression or a set of executable code statements.
Finally, to run some values in a shell program and get its return
value back, use the back quote (<TT><FONT FACE="Courier">`</FONT></TT>)
symbol. See the Perl script in Listing 2.1 for an example.
<HR>
<BLOCKQUOTE>
<B>Listing 2.1. Quoting in a Perl script.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">1 #!/usr/bin/perl<BR>
2 $folks="100";<BR>
3 print "\$folks = $folks \n";<BR>
4 print '\$folks = $folks \n';<BR>
5 print "\n\n BEEP! \a \LSOME BLANK \ELINES HERE
\n\n";<BR>
6 $date = `date +%D`;<BR>
7 print "Today is [$date] \n";<BR>
8 chop $date;<BR>
9 print "Date after chopping off carriage return: [".$date."]\n";</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
The output from the code in Listing 2.1 is as follows:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$folks = 100<BR>
\$folks = $folks \n<BR>
<BR>
BEEP! some blank LINES HERE<BR>
<BR>
Today is [03/29/96<BR>
]<BR>
Date after chopping off carriage return: [03/29/96]</FONT></TT>
</BLOCKQUOTE>
<P>
Let's go over the code shown in Listing 2.1. First of all, note
that the actual listing did not have line numbers. The line numbers
in this and subsequent scripts are used to identify specific lines
of code.
<P>
Line 1 is the mandatory first line of the Perl script. Change
the path shown in Listing 2.1 to where your Perl interpreter is
located if the script does not run. Be sure to make a similar
change to the rest of the source listings in this book.
<P>
Line 2 assigns a string value to the<B> </B><TT><FONT FACE="Courier">$folks</FONT></TT>
variable. Note that you did not have to declare the variable <TT><FONT FACE="Courier">$folks</FONT></TT>
because it was created when used for the first time.
<P>
Line 3 prints the value of <TT><FONT FACE="Courier">$folks</FONT></TT>
in between double quotes. The <TT><FONT FACE="Courier">$</FONT></TT>
sign in <TT><FONT FACE="Courier">$folks</FONT></TT> has to be
escaped with a backslash to prevent Perl from evaluating the value
of <TT><FONT FACE="Courier">$folks</FONT></TT> instead of printing
the following line:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$folks = 100</FONT></TT>
</BLOCKQUOTE>
<P>
In line 4, Perl does not evaluate anything between the single
quotes. Therefore, the entire contents of the line are left untouched
and printed here:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">\$folks = $folks \n</FONT></TT>
</BLOCKQUOTE>
<P>
Perl has several special characters to format text data for you.
Line 5 prints multiple blank lines with the <TT><FONT FACE="Courier">\n</FONT></TT>
character and beeps at the terminal. Notice how the words <TT><FONT FACE="Courier">SOME
BLANK</FONT></TT> are printed in lowercase letters. This is because
they are encased between the <TT><FONT FACE="Courier">\L</FONT></TT>
and <TT><FONT FACE="Courier">\E</FONT></TT> special characters,
which force all characters to be lowercase. Some of these special
characters are listed in Table 2.2.<BR>
<BLOCKQUOTE>
<CENTER><B>Table 2.2. Special characters in Perl.</B></CENTER>
</BLOCKQUOTE>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=83><I>Character</I></TD><TD WIDTH=401><I>Meaning</I>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=83><TT><FONT FACE="Courier">\\</FONT></TT> </TD>
<TD WIDTH=401>Backslash.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=83><TT><FONT FACE="Courier">\0ooo</FONT></TT></TD>
<TD WIDTH=401>Octal number in <TT><FONT FACE="Courier">ooo</FONT></TT> (for example, <TT><FONT FACE="Courier">\0213</FONT></TT>).
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=83><TT><FONT FACE="Courier">\a</FONT></TT> </TD>
<TD WIDTH=401>Beep.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=83><TT><FONT FACE="Courier">\b</FONT></TT> </TD>
<TD WIDTH=401>Backspace.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=83><TT><FONT FACE="Courier">\c</FONT></TT></TD>
<TD WIDTH=401>Inserts the next character literally (for example, <TT><FONT FACE="Courier">\$</FONT></TT> puts <TT><FONT FACE="Courier">$</FONT></TT>).
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=83><TT><FONT FACE="Courier">\cC</FONT></TT></TD>
<TD WIDTH=401>Inserts control character <TT><FONT FACE="Courier">C</FONT></TT>.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=83><TT><FONT FACE="Courier">\l</FONT></TT> </TD>
<TD WIDTH=401>Next character is lowercase.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=83><TT><FONT FACE="Courier">\L</FONT></TT> <TT><FONT FACE="Courier">\E</FONT></TT>
</TD><TD WIDTH=401>All characters between <TT><FONT FACE="Courier">\L</FONT></TT> and <TT><FONT FACE="Courier">\E</FONT></TT> are lowercase.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=83><TT><FONT FACE="Courier">\n</FONT></TT> </TD>
<TD WIDTH=401>New line (line feed).</TD></TR>
<TR VALIGN=TOP><TD WIDTH=83><TT><FONT FACE="Courier">\r</FONT></TT> </TD>
<TD WIDTH=401>Carriage return (MS-DOS).</TD></TR>
<TR VALIGN=TOP><TD WIDTH=83><TT><FONT FACE="Courier">\t</FONT></TT> </TD>
<TD WIDTH=401>Tab.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=83><TT><FONT FACE="Courier">\u</FONT></TT> </TD>
<TD WIDTH=401>Next character is uppercase.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=83><TT><FONT FACE="Courier">\U</FONT></TT> <TT><FONT FACE="Courier">\E</FONT></TT>
</TD><TD WIDTH=401>All characters between <TT><FONT FACE="Courier">\U</FONT></TT> and <TT><FONT FACE="Courier">\E</FONT></TT> are uppercase.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=83><TT><FONT FACE="Courier">\x##</FONT></TT> </TD>
<TD WIDTH=401>Hex number in <TT><FONT FACE="Courier">##</FONT></TT> (for example, <TT><FONT FACE="Courier">\x1d</FONT></TT>).
</TD></TR>
</TABLE></CENTER>
<P>
<P>
In line 6, the script uses the back quotes (<TT><FONT FACE="Courier">`</FONT></TT>)
to execute a command and return the results in the <TT><FONT FACE="Courier">$date</FONT></TT>
variable. The string in between the two back quotes is what you
would type at the command line, with one exception: if you use
Perl variables in the command line for the back quotes, Perl evaluates
these variables before passing them off to the shell for execution.
For example, line 6 could be rewritten as this:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$parm = "+%D";<BR>
$date = `$date $parm`;</FONT></TT>
</BLOCKQUOTE>
<P>
The returned value in <TT><FONT FACE="Courier">$date</FONT></TT>
is printed out in line 7. Note that there is an extra carriage
return in the text for data. To remove it, use the <TT><FONT FACE="Courier">chop</FONT></TT>
command as shown in line 8.
<P>
Then in line 9 the <TT><FONT FACE="Courier">$date</FONT></TT>
output is shown to print correctly. Note how the period (<TT><FONT FACE="Courier">.</FONT></TT>)
is used to concatenate three strings together for the output.
<P>
It's easy to construct strings in Perl with the period (<TT><FONT FACE="Courier">.</FONT></TT>)
operator. Given two strings, <TT><FONT FACE="Courier">$first</FONT></TT>
and <TT><FONT FACE="Courier">$last</FONT></TT>, you can construct
the string <TT><FONT FACE="Courier">$fullname</FONT></TT> like
this to get <TT><FONT FACE="Courier">"Jim Smith"</FONT></TT>:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$first = "Jim";<BR>
$last = "Smith";<BR>
$fullname = $first . " " . $last;</FONT></TT>
</BLOCKQUOTE>
<P>
Numbers in Perl are stored as floating-point numbers; even variables
used as integers are really stored as floating point numbers.
There are a set of operations you can do with numbers. These operations
are listed in Table 2.3. The table also lists Boolean operators.
<BR>
<P>
<CENTER><B>Table 2.3. Numeric operations with Perl.</B></CENTER>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=167><I>Operation</I></TD><TD WIDTH=405><I>Description</I>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r = $x + $y</FONT></TT>
</TD><TD WIDTH=405>Adds <TT><FONT FACE="Courier">$x</FONT></TT> to <TT><FONT FACE="Courier">$y</FONT></TT> and assigns the result to <TT><FONT FACE="Courier">$r</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r = $x - $y</FONT></TT>
</TD><TD WIDTH=405>Subtracts <TT><FONT FACE="Courier">$y</FONT></TT> from <TT><FONT FACE="Courier">$x</FONT></TT> and assigns the result to <TT><FONT FACE="Courier">$r</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r = $x * $y </FONT></TT>
</TD><TD WIDTH=405>Multiplies <TT><FONT FACE="Courier">$y</FONT></TT> and <TT><FONT FACE="Courier">$x</FONT></TT> and assigns the result to <TT><FONT FACE="Courier">$r</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r = $x / $y </FONT></TT>
</TD><TD WIDTH=405>Divides <TT><FONT FACE="Courier">$x</FONT></TT> by <TT><FONT FACE="Courier">$y</FONT></TT> and assigns the result to <TT><FONT FACE="Courier">$r</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r = $x % $y </FONT></TT>
</TD><TD WIDTH=405>Modulo; divides <TT><FONT FACE="Courier">$x</FONT></TT> by <TT><FONT FACE="Courier">$y</FONT></TT> and assigns the remainder to <TT><FONT FACE="Courier">$r</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r = $x ** $y </FONT></TT>
</TD><TD WIDTH=405>Raises <TT><FONT FACE="Courier">$x</FONT></TT> to the power of <TT><FONT FACE="Courier">$y</FONT></TT> and assigns the result to <TT><FONT FACE="Courier">$r</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r = $x << $n</FONT></TT>
</TD><TD WIDTH=405>Shifts bits in <TT><FONT FACE="Courier">$x</FONT></TT> left <TT><FONT FACE="Courier">$n</FONT></TT> times and assigns to <TT><FONT FACE="Courier">$r</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r = $x >> $n</FONT></TT>
</TD><TD WIDTH=405>Shifts bits in <TT><FONT FACE="Courier">$x</FONT></TT> right <TT><FONT FACE="Courier">$n</FONT></TT> times and assigns to <TT><FONT FACE="Courier">$r</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r = ++$x </FONT></TT>
</TD><TD WIDTH=405>Increments <TT><FONT FACE="Courier">$x</FONT></TT> and assigns <TT><FONT FACE="Courier">$x</FONT></TT> to <TT><FONT FACE="Courier">$r</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r = $x++ </FONT></TT>
</TD><TD WIDTH=405>Assigns <TT><FONT FACE="Courier">$x</FONT></TT> to <TT><FONT FACE="Courier">$r</FONT></TT> and then increments <TT><FONT FACE="Courier">$x</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r += $x;</FONT></TT>
</TD><TD WIDTH=405>Adds <TT><FONT FACE="Courier">$x</FONT></TT> to <TT><FONT FACE="Courier">$r</FONT></TT> and then assigns to <TT><FONT FACE="Courier">$r</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r = --$x </FONT></TT>
</TD><TD WIDTH=405>Decrements <TT><FONT FACE="Courier">$x</FONT></TT> and assigns <TT><FONT FACE="Courier">$x</FONT></TT> to <TT><FONT FACE="Courier">$r</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r = $x-- </FONT></TT>
</TD><TD WIDTH=405>Assigns <TT><FONT FACE="Courier">$x</FONT></TT> to <TT><FONT FACE="Courier">$r</FONT></TT> and then decrements <TT><FONT FACE="Courier">$x</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r -= $x;</FONT></TT>
</TD><TD WIDTH=405>Subtracts <TT><FONT FACE="Courier">$x</FONT></TT> from <TT><FONT FACE="Courier">$r</FONT></TT> and then assigns to <TT><FONT FACE="Courier">$r</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r /= $x;</FONT></TT>
</TD><TD WIDTH=405>Divides <TT><FONT FACE="Courier">$r</FONT></TT> by <TT><FONT FACE="Courier">$x</FONT></TT> and then assigns to <TT><FONT FACE="Courier">$r</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r *= $x;</FONT></TT>
</TD><TD WIDTH=405>Multiplies <TT><FONT FACE="Courier">$r</FONT></TT> by <TT><FONT FACE="Courier">$x</FONT></TT> and then assigns to <TT><FONT FACE="Courier">$r</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r = $x <=> $y </FONT></TT>
</TD><TD WIDTH=405><TT><FONT FACE="Courier">$r</FONT></TT> is <TT><FONT FACE="Courier">1</FONT></TT> if <TT><FONT FACE="Courier">$x > $y</FONT></TT>; <TT><FONT FACE="Courier">0</FONT></TT> if <TT><FONT FACE="Courier">$x == $y</FONT></TT>; -<TT><FONT
FACE="Courier">1</FONT></TT> if <TT><FONT FACE="Courier">$x < $y</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r = $x || $y</FONT></TT>
</TD><TD WIDTH=405><TT><FONT FACE="Courier">$r</FONT></TT> is the logical <TT><FONT FACE="Courier">OR</FONT></TT> of variables <TT><FONT FACE="Courier">$x</FONT></TT> and <TT><FONT FACE="Courier">$y</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r = $x && $y</FONT></TT>
</TD><TD WIDTH=405><TT><FONT FACE="Courier">$r</FONT></TT> is the logical <TT><FONT FACE="Courier">AND</FONT></TT> of variables <TT><FONT FACE="Courier">$x</FONT></TT> and <TT><FONT FACE="Courier">$y</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=167><TT><FONT FACE="Courier">$r = ! $x </FONT></TT>
</TD><TD WIDTH=405><TT><FONT FACE="Courier">$r</FONT></TT> is the opposite Boolean value of <TT><FONT FACE="Courier">$x</FONT></TT>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
You can compare values of variables to check results of operations.
Table 2.4 lists the comparison operators for numbers and strings.
<BR>
<P>
<CENTER><B>Table 2.4. Comparison operations with Perl.</B></CENTER>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=138><I>Operation</I></TD><TD WIDTH=416><I>Description</I>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">$x == $y</FONT></TT>
</TD><TD WIDTH=416><TT><FONT FACE="Courier">True</FONT></TT> if <TT><FONT FACE="Courier">$x</FONT></TT> is equal to <TT><FONT FACE="Courier">$y</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">$x != $y</FONT></TT>
</TD><TD WIDTH=416><TT><FONT FACE="Courier">True</FONT></TT> if <TT><FONT FACE="Courier">$x</FONT></TT> is not equal to <TT><FONT FACE="Courier">$y</FONT></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">$x < $y</FONT></TT>
</TD><TD WIDTH=416><TT><FONT FACE="Courier">True</FONT></TT> if <TT><FONT FACE="Courier">$x</FONT></TT> is less than <TT><FONT FACE="Courier">$y</FONT></TT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -