📄 507-510.html
字号:
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:Perl</TITLE>
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0672313723//-->
<!--TITLE=Linux Unleashed, Third Edition//-->
<!--AUTHOR=Tim Parker//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=28//-->
<!--PAGES=507-510//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="505-507.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="510-513.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading7"></A><FONT COLOR="#000077">Strings</FONT></H4>
<P>Strings are sequences of one or more characters. Usually strings contain alphanumeric characters (such as the numbers from 0 to 9), the upper- and lowercase alphabet, and a handful of punctuation characters. Perl does not distinguish between these characters and nonprintable characters. This means that you can actually use Perl to scan binary files.
</P>
<P>Strings are represented in a Perl program in one of two ways: You can enclose a string in double quotation marks or you can enclose a string in single quotation marks. When a string is enclosed in double quotation marks, it behaves in a similar way to strings that are in double quotation marks in C. A backslash can be used in a double quoted string to represent special control characters. A list of the backslash special control characters is shown in Table 28.2.</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 28.2.</B> Perl’s special control characters.
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="20%" ALIGN="LEFT">Character
<TH WIDTH="80%" ALIGN="LEFT">Description
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD><TT>\a</TT>
<TD>Causes an audible beep
<TR>
<TD><TT>\b</TT>
<TD>Inserts a backspace
<TR>
<TD><TT>\cD</TT>
<TD>Allows you to put control characters in a string (in this case Ctrl+D)
<TR>
<TD><TT>\f</TT>
<TD>Inserts a formfeed
<TR>
<TD><TT>\e</TT>
<TD>Inserts an escape character
<TR>
<TD><TT>\E</TT>
<TD>Terminates an <TT>\L</TT> or <TT>\U</TT> special control character
<TR>
<TD><TT>\l</TT>
<TD>Causes the next letter to be lowercase
<TR>
<TD><TT>\L</TT>
<TD>Causes all characters following it until the next <TT>\E</TT> to be lowercase
<TR>
<TD><TT>\n</TT>
<TD>Inserts a newline character
<TR>
<TD><TT>\r</TT>
<TD>Inserts a return character
<TR>
<TD><TT>\t</TT>
<TD>Inserts a tab character
<TR>
<TD><TT>\\</TT>
<TD>Inserts a backslash character
<TR>
<TD><TT>\”</TT>
<TD>Inserts a double quote character
<TR>
<TD><TT>\u</TT>
<TD>Causes the next letter to be uppercase
<TR>
<TD><TT>\U</TT>
<TD>Causes all characters following it until the next <TT>\E</TT> to be uppercase
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>Variable substitution can occur within a string that is enclosed in double quotation marks as well. For example, if you define a variable as
</P>
<!-- CODE SNIP //-->
<PRE>
$name=”Doreen”;
</PRE>
<!-- END CODE SNIP //-->
<P>that variable can be substituted into a greeting displayed on the screen by typing the following Perl command:
</P>
<!-- CODE SNIP //-->
<PRE>
print “Hello $name, how are you?\n”;
</PRE>
<!-- END CODE SNIP //-->
<P>This command results in the following output to be displayed on the screen:
</P>
<!-- CODE SNIP //-->
<PRE>
Hello Doreen, how are you?
</PRE>
<!-- END CODE SNIP //-->
<P>Strings enclosed in single quotation marks differ from strings enclosed in double quotation marks because the single quotation marks hide most special characters from the Perl interpreter. The only special characters that single quotes do not hide completely are the single quote character (<TT>’</TT>) and the backslash character (<TT>\</TT>). When Perl encounters a single quote character within a string that is enclosed in single quotation marks, it assumes that it is the end of the string. This means that if you want to put a single quote in a string that is enclosed in single quotes, you must precede it with a backslash. The same rule applies if you want to put a backslash into a string enclosed in single quotes.</P>
<P>For example, if you want to print the string <TT>Don’t do that!</TT> to the screen by enclosing it in single quotes, enter the following command:</P>
<!-- CODE SNIP //-->
<PRE>
print ’Don\’t do that!’,”\n”;
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Note: </B><BR>Remember that if you put a newline character into a single quoted string, it just prints <TT>\n</TT> and doesn’t insert a newline character.<HR></FONT>
</BLOCKQUOTE>
<P>Just as is the case with numbers, Perl provides several operators that can be used to manipulate and compare strings. Table 28.3 lists Perl’s string operators.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 28.3.</B> Perl’s string operators.
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="30%" ALIGN="LEFT">Operator
<TH WIDTH="70%" ALIGN="LEFT">Description
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD><TT>op1 . op2</TT>
<TD>Concatenates <TT>op1</TT> and <TT>op2</TT>
<TR>
<TD><TT>op1 x op2</TT>
<TD>Repeats <TT>op1</TT>, <TT>op2</TT> times
<TR>
<TD><TT>op1 eq op2</TT>
<TD>Returns True if <TT>op1</TT> equals <TT>op2</TT>
<TR>
<TD><TT>op1 ge op2</TT>
<TD>Returns True if <TT>op1</TT> is greater than or equal to <TT>op2</TT>
<TR>
<TD><TT>op1 gt op2</TT>
<TD>Returns True if <TT>op1</TT> is greater than <TT>op2</TT>
<TR>
<TD><TT>op1 le op2</TT>
<TD>Returns True if <TT>op1</TT> is less than or equal to <TT>op2</TT>
<TR>
<TD><TT>op1 lt op2</TT>
<TD>Returns True if <TT>op1</TT> is less than <TT>op2</TT>
<TR>
<TD><TT>op1 ne op2</TT>
<TD>Returns True if <TT>op1</TT> is not equal to <TT>op2</TT>
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<H4 ALIGN="LEFT"><A NAME="Heading8"></A><FONT COLOR="#000077">File Operators</FONT></H4>
<P>Just like the shell programming languages, Perl has a number of operators that exist only to test for certain conditions that exist in directories and files. The most useful of these operators are listed in Table 28.4.
</P>
<TABLE WIDTH="100%"><CAPTION ALIGN=LEFT><B>Table 28.4.</B> Perl file operators.
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TH WIDTH="30%" ALIGN="LEFT">Operator
<TH WIDTH="70%" ALIGN="LEFT">Description
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD><TT>-B</TT>
<TD>File is a binary file
<TR>
<TD><TT>-d</TT>
<TD>File is a directory
<TR>
<TD><TT>-e</TT>
<TD>File exists
<TR>
<TD><TT>-f</TT>
<TD>File is an ordinary file
<TR>
<TD><TT>-r</TT>
<TD>File is readable
<TR>
<TD><TT>-s</TT>
<TD>File has a nonzero size
<TR>
<TD><TT>-T</TT>
<TD>File is a text file
<TR>
<TD><TT>-w</TT>
<TD>File is writable
<TR>
<TD><TT>-x</TT>
<TD>File is executable
<TR>
<TD><TT>-z</TT>
<TD>File has zero size
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P>These file operators can be used in any Perl expression. For example, the following code uses an <TT>if</TT> statement to decide if the <TT>.profile</TT> file exists in the current directory.</P>
<!-- CODE SNIP //-->
<PRE>
if (-e “.profile”) {
print “.profile is there\n”;
}
</PRE>
<!-- END CODE SNIP //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="505-507.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="510-513.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -