unx16.htm

来自「Unix Unleashed, Third Edition is written」· HTM 代码 · 共 2,814 行 · 第 1/5 页

HTM
2,814
字号

<BR></LI>

<LI>Run the program by entering the command foo.

<BR>

<BR></LI></OL>

<P>If you receive the error message foo not found or some equivalent, either enter the command ./foo or add the current directory . to your PATH environment variable.

<BR></P>

<P>At this point, the program waits for you to type in an input line. Once you have done so, it echoes your input line and exits.

<BR></P>

<P>The following sections describe each of the components of this simple program in a little more detail.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I9" NAME="I9">

<FONT SIZE=3><B>Using Comments</B>

<BR></FONT></A></CENTER></H4>

<P>The first line of this program is an example of a Perl comment. In Perl, anytime a # character is recognized, the rest of the line is treated as a comment:

<BR></P>

<PRE># this is a comment that takes up the whole line

$count = 0;     # this part of the line is a comment </PRE>

<P>A comment appearing as the first line of a program is special. This header comment indicates the location of the program interpreter to use. In this example, the string !/usr/bin/perl indicates that this file is a Perl program.

<BR></P>

<P>The Perl interpreter should be located in /usr/bin/perl on your system. If it is not, replace /usr/bin/perl in the header comment with the location of the Perl interpreter on your system.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I10" NAME="I10">

<FONT SIZE=3><B>Reading from Standard Input</B>

<BR></FONT></A></CENTER></H4>

<P>Like C, Perl recognizes the existence of the UNIX standard input file, standard output file, and standard error file. In C, these files are called stdin, stdout and stderr; in Perl, they are called STDIN, STDOUT and STDERR.

<BR></P>

<P>The Perl construct &lt;STDIN&gt; refers to a line of text read in from the standard input file. This line of text includes the closing newline character.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I11" NAME="I11">

<FONT SIZE=3><B>Storing Values The Scalar Variable</B>

<BR></FONT></A></CENTER></H4>

<P>The construct $inputline is an example of a scalar variable. A scalar variable is a variable that holds exactly one value. This value can be a string, integer, or floating point number.

<BR></P>

<P>All scalar variables start with a dollar sign, $. This distinguishes them from other Perl variables. In a scalar variable, the character immediately following the dollar sign must be a letter. Subsequent characters can be letters, digits, or 
underscores. Scalar variable names can be as long as you like.

<BR></P>

<P>For more information on scalar variables and their values, see the section &quot;Working with Scalar Variables&quot; later in this chapter.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I12" NAME="I12">

<FONT SIZE=3><B>Assigning a Value to a Scalar Variable</B>

<BR></FONT></A></CENTER></H4>

<P>The statement $inputline = &lt;STDIN&gt;; contains the = character, which is the Perl assignment operator. This statement tells Perl that the line of text read from standard input, represented by &lt;STDIN&gt;, is to become the new value of the scalar 
variable $inputline.

<BR></P>

<P>Perl provides a full set of useful arithmetic, logical, and string operators. For details, refer to the sections &quot;Working with Scalar Variables&quot; and &quot;Using Lists and Array Variables&quot; later in this chapter.

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="caution.gif" WIDTH = 37 HEIGHT = 35><B>CAUTION: </B>All scalar variables are given an initial value of the null string, &quot;&quot;. Therefore, a Perl program can be run even when a scalar variable is used before a value has been assigned to it. 

Consider the statement

<BR>

<BR>$b = $a;

<BR>

<BR>This statement assigns the value of the variable $a to $b. If $a has not been seen before, it is assumed to have the value &quot;&quot;, and &quot;&quot; is assigned to $b. Since this behavior is legal in Perl, you must check your programs for 
&quot;undefined&quot; variables yourself.

<BR></NOTE>

<HR ALIGN=CENTER>

<H4 ALIGN="CENTER">

<CENTER><A ID="I13" NAME="I13">

<FONT SIZE=3><B>Scalar Variables Inside Character Strings</B>

<BR></FONT></A></CENTER></H4>

<P>The final statement of the program, print (&quot;$inputline&quot;);, contains a character string, which is a sequence of characters enclosed in double quotes. In this case, the character string is &quot;$inputline&quot;.

<BR></P>

<P>The string &quot;$inputline&quot; contains the name of a scalar variable, $inputline. When Perl sees a variable inside a character string, it replaces the variable with its value. In this example, the string &quot;$inputline&quot; is replaced with the 
line of text read from the standard input file.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I14" NAME="I14">

<FONT SIZE=3><B>Writing to Standard Output</B>

<BR></FONT></A></CENTER></H4>

<P>The built-in function print() writes its arguments (the items enclosed in parentheses) to the standard output file. In this example, the statement print (&quot;$inputline&quot;); sends the contents of the scalar variable $inputline to the standard 
output file.

<BR></P>

<P>The print() function can also be told to write to the standard error file or to any other specified file. See the section &quot;Reading from and Writing to Files&quot; later in this chapter for more details.

<BR></P>

<H3 ALIGN="CENTER">

<CENTER><A ID="I15" NAME="I15">

<FONT SIZE=4><B>Working with Scalar Variables</B>

<BR></FONT></A></CENTER></H3>

<P>Now that you know a little about Perl, it's time to describe the language in a little more detail. This section starts you off by discussing scalar variables and the values that can be stored in them.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I16" NAME="I16">

<FONT SIZE=3><B>Understanding Scalar Values</B>

<BR></FONT></A></CENTER></H4>

<P>In Perl, a scalar value is any value that can be stored in a scalar variable. The following are scalar values:

<BR></P>

<UL>

<LI>Integers

<BR>

<BR></LI>

<LI>Double and single-quoted character strings

<BR>

<BR></LI>

<LI>Floating-point values

<BR>

<BR></LI></UL>

<P>The following assignments are all legal in Perl:

<BR></P>

<PRE>$variable = 1;

$variable = &quot;this is a string&quot;;

$variable = 3.14159;</PRE>

<P>The following assignments are not legal:

<BR></P>

<PRE>$variable = 67M;

$variable = ^803;

$variable = $%$%!;</PRE>

<H5 ALIGN="CENTER">

<CENTER><A ID="I17" NAME="I17">

<FONT SIZE=3><B>Using Octal and Hexadecimal Representation</B>

<BR></FONT></A></CENTER></H5>

<P>Normally, integers are assumed to be in standard base 10 notation. Perl also supports base 8 (octal) and base 16 (hexadecimal) notation.

<BR></P>

<P>To indicate that a number is in base 8, put a zero in front of the number:

<BR></P>

<PRE>$a = 0151;          # 0151 octal is 105</PRE>

<P>To indicate base 16, put 0x (or 0X) in front of the number:

<BR></P>

<PRE>$a = 0x69;          # 69 hex is also 105</PRE>

<P>The letters A through F (in either upper- or lowercase) represent the values 10 through 15:

<BR></P>

<PRE>$a = 0xFE;          # equals 16 * 15 + 1 * 14, or 254</PRE>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> Strings containing a leading 0 or 0x are not treated as base 8 or base 16:

<BR>

<BR>$a = &quot;0151&quot;;

<BR>$a = &quot;0x69&quot;;

<BR>

<BR>These strings are treated as character strings whose first character is &quot;0.&quot;

<BR></NOTE>

<HR ALIGN=CENTER>

<H5 ALIGN="CENTER">

<CENTER><A ID="I18" NAME="I18">

<FONT SIZE=3><B>Using Double- and Single-Quoted Strings</B>

<BR></FONT></A></CENTER></H5>

<P>So far, all of the strings you have seen have been enclosed by the &quot; (double quotation mark) characters:

<BR></P>

<PRE>$a = &quot;This is a string in double quotes&quot;;</PRE>

<P>Perl also allows you to enclose strings using the ' (single quotation mark) character:

<BR></P>

<PRE>$a = 'This is a string in single quotes';</PRE>

<P>There are two differences between double-quoted strings and single-quoted strings. The first difference is that variables are replaced by their values in double-quoted strings, but not in single-quoted strings:

<BR></P>

<PRE>$x = &quot;a string&quot;;

$y = &quot;This is $x&quot;;  # becomes &quot;This is a string&quot;

$z = 'This is $x';  # remains 'This is $x'</PRE>

<P>Also, double-quoted strings recognize escape sequences for special characters. These escape sequences consist of a backslash (\) followed by one or more characters. The most common escape sequence is \n, representing the newline character:

<BR></P>

<PRE>$a = &quot;This is a string terminated by a newline\n&quot;;</PRE>

<P>Table 16.1 lists the escape sequences recognized in double-quoted strings.

<BR></P>

<UL>

<LH><B>Table 16.1. Escape Sequences in Double-Quoted Strings.</B>

<BR></LH></UL>

<TABLE BORDER>

<TR>

<TD>

<P>\a</P>

<TD>

<P>bell (beep)</P>

<TR>

<TD>

<P>\b</P>

<TD>

<P>backspace</P>

<TR>

<TD>

<P>\cn</P>

<TD>

<P>the control-n character</P>

<TR>

<TD>

<P>\e</P>

<TD>

<P>escape</P>

<TR>

<TD>

<P>\f</P>

<TD>

<P>form feed</P>

<TR>

<TD>

<P>\l</P>

<TD>

<P>force next letter into lowercase</P>

<TR>

<TD>

<P>\L</P>

<TD>

<P>all following letters are lowercase</P>

<TR>

<TD>

<P>\n</P>

<TD>

<P>newline</P>

<TR>

<TD>

<P>\r</P>

<TD>

<P>carriage return</P>

<TR>

<TD>

<P>\t</P>

<TD>

<P>tab</P>

<TR>

<TD>

<P>\u</P>

<TD>

<P>force next letter into uppercase</P>

<TR>

<TD>

<P>\U</P>

<TD>

<P>all following letters are uppercase</P>

<TR>

<TD>

<P>\v</P>

<TD>

<P>vertical tab</P></TABLE>

<P>\L and \U can be turned off by \E:

<BR></P>

<PRE>$a = &quot;T\LHIS IS A \ESTRING&quot;;  # same as &quot;This is a STRING&quot;</PRE>

<P>To include a backslash or double quote in a double-quoted string, precede it with another backslash:

<BR></P>

<PRE>$a = &quot;A quote \&quot; in a string&quot;;

$a = &quot;A backslash \\ in a string&quot;;</PRE>

<P>You can specify the ASCII value for a character in base 8 or octal notation using \<I>nnn</I>, where each <I>n</I> is an octal digit:

<BR></P>

<PRE>$a = &quot;\377&quot;;        # this is the character 255, or EOF</PRE>

<P>You can also use hexadecimal to specify the ASCII value for a character. To do this, use the sequence \x<I>nn</I>, where each <I>n</I> is a hexadecimal digit:

<BR></P>

<PRE>$a = &quot;\xff&quot;;        # this is also 255</PRE>

<P>None of these escape sequences is supported in single-quoted strings, except for \' and \\, which represent the single quote character and the backslash, respectively:

<BR></P>

<PRE>$a = '\b is not a bell'

$a = 'a single quote \' in a string'

$a = 'a backslash \\ in a string'</PRE>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> In Perl, strings are not terminated by a null character (ASCII 0), as they are in C. In Perl, the null character can appear anywhere in a string:

<BR>

<BR>$a = &quot;This string \000 has a null character in it&quot;;

<BR></NOTE>

<HR ALIGN=CENTER>

<H5 ALIGN="CENTER">

<CENTER><A ID="I19" NAME="I19">

<FONT SIZE=3><B>Using Floating-Point Values</B>

<BR></FONT></A></CENTER></H5>

<P>Perl supports floating-point numbers in both conventional and scientific notation. The letter E (or e) represents the power of ten to which a number in scientific notation is to be raised.

<BR></P>

<PRE>$a = 11.3;          # conventional notation

$a = 1.13E01;       # 11.3 in scientific notation

$a = -1.13e-01;     # the above divided by -10</PRE>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="caution.gif" WIDTH = 37 HEIGHT = 35><B>CAUTION: </B>Note that Perl uses your machine's floating point representation. This means that only a certain number of digits (in mathematical terms, a certain precision) are supported. For example, 
consider the following very short program:

<BR>

<BR>#!/usr/bin/perl

<BR>$pi = 3.14159265358979233;

<BR>print (&quot;pi is $pi\n&quot;);

<BR>

<BR>This program prints the following:

<BR>

<BR>pi = 3.1415926535897922

<BR>

<BR>This is because there just isn't room to keep track of all of the digits of pi specified by the program.

<BR>

<BR>This problem is made worse when arithmetic operations are performed on floating point numbers; see &quot;Performing Comparisons&quot; for more information on this problem.

<BR>

<BR>Note that most programming languages, including C, have this problem.

<BR></NOTE>

<HR ALIGN=CENTER>

<H5 ALIGN="CENTER">

<CENTER><A ID="I20" NAME="I20">

<FONT SIZE=3><B>Interchangeability of Strings and Numeric Values</B>

<BR></FONT></A></CENTER></H5>

<P>In Perl, as you have seen, a scalar variable can be used to store a character string, an integer, or a floating point value. In scalar variables, a value that was assigned as a string can be used as an integer whenever it makes sense to do so, and vice 

⌨️ 快捷键说明

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