⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch2.htm

📁 prrl 5 programs codes in the book
💻 HTM
📖 第 1 页 / 共 3 页
字号:

<I>Tell Perl to begin printing.<BR>

More Lines for Perl to display.<BR>

The single quote ends the string literal.</I>

</BLOCKQUOTE>

<HR>

<BLOCKQUOTE>

<B>Listing 2.1&nbsp;&nbsp;02LST01.PL-Using Embedded Line Breaks

to Skip to a New Line<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

print 'Bill of Goods

Bread:    $34 .45

Fruit:    $45.00

          ======

          $79.45';

</PRE>

</BLOCKQUOTE>

<HR>

<P>

Figure 2.1 shows a bill of goods displayed on one long, single-quoted

literal.

<P>

<A HREF="f2-1.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/f2-1.gif"><B>Figure 2.1 : </B><I>A bill of goods displayed one long single-quoted

literal</I>.</A>

<P>

You can see that with single-quoted literals, even the line breaks

in your source code are part of the string.

<H3><A NAME="ExampleDoubleQuotedStrings">

Example: Double-Quoted Strings</A></H3>

<P>

Double-quoted strings start out simple, then become a bit more

involved than single-quoted strings. With double-quoted strings,

you can use the backslash to add some special characters to your

string. <A HREF="ch3.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch3.htm" >Chapter 3</A> &quot;Variables,&quot; will talk about how

double-quoted strings and variables interact.<BR>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD><B>Note</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

<I>Variables</I>-which are described in <A HREF="ch3.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch3.htm" >Chapter 3</A> &quot;Variables&quot;-are simply locations in the computer's memory where Perl holds the various data types. They're called variables because the content of the memory can change as 
needed.

</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<P>

The basic double-quoted string is a series of characters surrounded

by double quotes. If you need to use the double quote inside the

string, you can use the backslash character.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT>

<BLOCKQUOTE>

<I>This literal is similar to one you've already seen. Just the

quotes are different.<BR>

Another literal that uses double quotes inside a double-quoted

string.</I>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

&quot;WasWaldo the Illusionist&quot;

&quot;Morganna said, \&quot;WasWaldo can't hit anything.\&quot;&quot;

</PRE>

</BLOCKQUOTE>

<P>

Notice how the backslash in the second line is used to escape

the double quote characters. And the single quote can be used

without a backslash.

<P>

One major differeNCe between double- and single-quoted strings

is that double-quoted strings have some special <I>escape sequeNCes</I>

that can be used. Escape sequeNCes represent characters that are

not easily entered using the keyboard or that are difficult to

see inside an editor window. Table 2.1 shows all of the escape

sequeNCes that Perl understands. The examples following the table

will illustrate some of them.<BR>

<P>

<CENTER><B>Table 2.1&nbsp;&nbsp;Escape SequeNCes</B></CENTER>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD WIDTH=127><CENTER><I>Escape SequeNCes</I></CENTER></TD>

<TD WIDTH=463><I>Description or Character</I></TD></TR>

<TR><TD WIDTH=127><CENTER>\a</CENTER></TD><TD WIDTH=463>Alarm\bell

</TD></TR>

<TR><TD WIDTH=127><CENTER>\b</CENTER></TD><TD WIDTH=463>Backspace

</TD></TR>

<TR><TD WIDTH=127><CENTER>\e</CENTER></TD><TD WIDTH=463>Escape

</TD></TR>

<TR><TD WIDTH=127><CENTER>\f</CENTER></TD><TD WIDTH=463>Form Feed

</TD></TR>

<TR><TD WIDTH=127><CENTER>\n</CENTER></TD><TD WIDTH=463>Newline

</TD></TR>

<TR><TD WIDTH=127><CENTER>\r</CENTER></TD><TD WIDTH=463>Carriage Return

</TD></TR>

<TR><TD WIDTH=127><CENTER>\t</CENTER></TD><TD WIDTH=463>Tab</TD>

</TR>

<TR><TD WIDTH=127><CENTER>\v</CENTER></TD><TD WIDTH=463>Vertical Tab

</TD></TR>

<TR><TD WIDTH=127><CENTER>\$</CENTER></TD><TD WIDTH=463>Dollar Sign

</TD></TR>

<TR><TD WIDTH=127><CENTER>\@</CENTER></TD><TD WIDTH=463>Ampersand

</TD></TR>

<TR><TD WIDTH=127><CENTER>\0nnn</CENTER></TD><TD WIDTH=463>Any Octal byte

</TD></TR>

<TR><TD WIDTH=127><CENTER>\xnn</CENTER></TD><TD WIDTH=463>Any Hexadecimal byte

</TD></TR>

<TR><TD WIDTH=127><CENTER>\cn</CENTER></TD><TD WIDTH=463>Any Control character

</TD></TR>

<TR><TD WIDTH=127><CENTER>\l</CENTER></TD><TD WIDTH=463>Change the next character to lowercase

</TD></TR>

<TR><TD WIDTH=127><CENTER>\u</CENTER></TD><TD WIDTH=463>Change the next character to uppercase

</TD></TR>

<TR><TD WIDTH=127><CENTER>\L</CENTER></TD><TD WIDTH=463>Change the following characters to lowercase until a \E sequeNCe is eNCountered. Note that you need to use an uppercase E here, lowercase will not work.

</TD></TR>

<TR><TD WIDTH=127><CENTER>\Q</CENTER></TD><TD WIDTH=463>Quote meta-characters as literals. See <A HREF="ch10.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch10.htm" >Chapter 10</A>, &quot;Regular Expressions,&quot; for more information on meta-characters.

</TD></TR>

<TR><TD WIDTH=127><CENTER>\U</CENTER></TD><TD WIDTH=463>Change the following characters to uppercase until a \E sequeNCe is eNCountered. Note that you need to use an uppercase E here, lowercase will not work.

</TD></TR>

<TR><TD WIDTH=127><CENTER>\E</CENTER></TD><TD WIDTH=463>Terminate the \L, \Q, or \U sequeNCe. Note that you need to use an uppercase E here, lowercase will not work.

</TD></TR>

<TR><TD WIDTH=127><CENTER>\\</CENTER></TD><TD WIDTH=463>Backslash

</TD></TR>

</TABLE>

</CENTER>

<P>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD><B>Note</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

In the next chapter, &quot;Variables,&quot; you'll see why you might need to use a backslash when using the $ and @ characters.</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT>

<BLOCKQUOTE>

<I>This literal represents the following: WasWaldo is 34 years

old. The \u is used twice in the first word to capitalize the

w characters. And the hexadecimal notation is used to represent

the age using the ASCII codes for 3 and 4.<BR>

This literal represents the following: The kettle was HOT!. The

\U capital-izes all characters until a \E sequeNCe is seen.</I>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

 &quot;\uwas\uwaldo is \x33\x34 years old.&quot;

&quot;The kettle was \Uhot\E!&quot;

</PRE>

</BLOCKQUOTE>

<P>

For more information about ASCII codes, see Appendix E, &quot;ASCII

Table.&quot; 

<P>

Actually, this example isn't too difficult, but it does involve

looking at more than one literal at oNCe and it's been a few pages

siNCe our last advaNCed example. Let's look at the \t and \n escape

sequeNCes. Listing 2.2-a program displaying a bill with several

items-will produce the output shown in Figure 2.2.

<P>

<A HREF="f2-2.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/f2-2.gif"><B>Figure 2.2 : </B><I>A bill of goods displayed using newline

and tab characters</I>.</A>

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT>

<BLOCKQUOTE>

<I>Display a literal as the first line, second and third of the

output.<BR>

Display literals that show what was purchased<BR>

Display a separator line.<BR>

Display the total.</I>

</BLOCKQUOTE>

<HR>

<BLOCKQUOTE>

<B>Listing 2.2&nbsp;&nbsp;02LST02.PL-Using Tabs and Newline Characters

to Print<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

print &quot;Bill of Goods



Bread:\t\$34.45\n&quot;;

print &quot;Fruit:\t&quot;;

print &quot;\$45.00\n&quot;;

print &quot;\t======\n&quot;;

print &quot;\t\$79.45\n&quot;;

</PRE>

</BLOCKQUOTE>

<HR>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD><B>Tip</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

Notice that Figure 2.1 and 2.2 look identical. This illustrates a cardinal rule of Perl-there's <I>always</I> more than one way to do something.

</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<P>

This program uses two methods to cause a line break.

<UL>

<LI>The first is simply to iNClude the line break in the source

code.

<LI>The second is to use the \n or newline character.

</UL>

<P>

I recommend using the \n character so that when looking at your

code in the future, you can be assured that you meant to cause

a line break and did not simply press the ENTER key by mistake.

<BR>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD><B>Caution</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

If you are a C/C++ programmer, this material is not new to you. However, Perl strings are <I>not identical</I> to C/C++ strings because they have no ending NULL character. If you are thinking of converting C/C++ programs to Perl, take care to modify any 
code that relies on the NULL character to end a string.

</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<H3><A NAME="ExampleBackQuotedStrings">

Example: Back-Quoted Strings</A></H3>

<P>

It might be argued that back-quoted strings are not really a data

type. That's because Perl uses back-quoted strings to execute

system commands. When Perl sees a back-quoted string, it passes

the contents to Windows, UNIX, or whatever operating system you

are using.

<P>

Let's see how to use the back-quoted string to display a directory

listing of all text files in the perl5 directory. 

<P>

Figure 2.3 shows what the output of such a program might look

like.

<P>

<A HREF="f2-3.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/f2-3.gif"><B>Figure 2.3 : </B><I>Using a back-quoted string to display a

directory</I>.</A>

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT>

<BLOCKQUOTE>

<I>Print the directory listing.</I>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

print &quot;dir *.txt&quot;;

</PRE>

</BLOCKQUOTE>

<P>

All of the escape sequeNCes used with double-quoted strings can

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -