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

📄 ch29.htm

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTM
📖 第 1 页 / 共 3 页
字号:






</DL>







<CENTER>



<H4><A NAME="Heading13<FONT COLOR="#000077">Comments</FONT></H4>



</CENTER>



<P>As you have just seen, the first character of the line</P>



<PRE><FONT COLOR="#0066FF">#!/usr/local/bin/perl



</FONT></PRE>



<P>is the comment character, <TT>#</TT>. When the Perl interpreter sees the <TT>#</TT>,



it ignores the rest of that line.</P>



<P>Comments can be appended to lines containing code, or they can be lines of their



own:</P>



<PRE><FONT COLOR="#0066FF">$inputline = &lt;STDIN&gt;;    # this line contains an appended comment



# this entire line is a comment



</FONT></PRE>



<P>You can--and should--use comments to make your programs easier to understand.



Listing 29.2 is the simple program you saw earlier, but it has been modified to include



comments explaining what the program does.







<DL>



	<DT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading14<FONT COLOR="#000077"><B>NOTE:</B> </FONT>As you create your



	own programs--such as the one in Listing 29.2--you can, of course, name them anything



	you want. For illustration and discussion purposes, I've adopted the convention of



	using a name that corresponds to the listing number. For example, the program in



	Listing 29.2 is called <TT>program29_2</TT>. The program name is used in the input



	and output examples such as the one following this listing, as well as in the following



	analysis, where the listing is discussed in detail. When you follow the input and



	output examples, just remember to substitute your program's name for the one shown



	in the example. 



<HR>







</DL>







<CENTER>



<H3><A NAME="Heading15<FONT COLOR="#000077">Listing 29.2. A simple Perl program



with comments.</FONT></H3>



</CENTER>



<PRE><FONT COLOR="#0066FF">1: #!/usr/local/bin/perl



2: # this program reads a line of input and writes the line



3: # back out



4: $inputline = &lt;STDIN&gt;;    # read a line of input



5: print( $inputline );     # write the line out



</FONT></PRE>



<P>This is the sample input and output of this program:</P>



<PRE><FONT COLOR="#0066FF">$ program29_2



This is a line of input.



This is a line of input.



$



</FONT></PRE>



<P>The behavior of the program in Listing 29.2 is identical to that of Listing 29.1



because the code is the same. The only difference is that Listing 29.2 has comments



in it.</P>



<P>Note that in an actual program, comments normally are used only to explain complicated



code or to indicate that the following lines of code perform a specific task. Because



Perl instructions usually are pretty straightforward, Perl programs don't need to



have a lot of comments.







<DL>



	<DT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading16<FONT COLOR="#000077"><B>NOTE:</B> </FONT>Do use comments



	whenever you think that a line of code is not easy to understand. Don't clutter your



	code with unnecessary comments. The goal is readability. If a comment makes a program



	easier to read, include it. Otherwise, don't bother. Don't put anything else after



	<TT>/usr/local/bin/perl</TT> in the first line:



</DL>











<DL>



	<DD><FONT COLOR="#0066FF">#!/usr/local/bin/perl</FONT>



</DL>







<PRE></PRE>











<BLOCKQUOTE>



	<P>This line is a special comment line, and it is not treated like the others. 



<HR>











</BLOCKQUOTE>







<CENTER>



<H3><A NAME="Heading17<FONT COLOR="#000077">Line 2: Statements, Tokens, and



&lt;STDIN&gt;</FONT></H3>



</CENTER>



<P>Now that you've learned what the first line of Listing 29.1 does, let's take a



look at line 2:</P>



<PRE><FONT COLOR="#0066FF">$inputline = &lt;STDIN&gt;;



</FONT></PRE>



<P>This is the first line of code that actually does any work. To understand what



this line does, you need to know what a Perl statement is and what its components



are.



<CENTER>



<H4><A NAME="Heading18<FONT COLOR="#000077">Statements and Tokens</FONT></H4>



</CENTER>



<P>The line of code you have just seen is an example of a Perl statement. Basically,



a statement is one task for the Perl interpreter to perform. A Perl program can be



thought of as a collection of statements performed one at a time.</P>



<P>When the Perl interpreter sees a statement, it breaks the statement into smaller



units of information. In this example, the smaller units of information are <TT>$inputline</TT>,



<TT>=</TT>, <TT>&lt;STDIN&gt;</TT>, and <TT>;</TT>. Each of these smaller units of



information is called a token.



<CENTER>



<H4><A NAME="Heading19<FONT COLOR="#000077">Tokens and White Space</FONT></H4>



</CENTER>



<P>Tokens can normally be separated by as many spaces and tabs as you like. For example,



the following statements are identical in Perl:</P>



<PRE><FONT COLOR="#0066FF">$inputline = &lt;STDIN&gt;;



$inputline=&lt;STDIN&gt;;



$inputline      =     &lt;STDIN&gt;;



</FONT></PRE>



<P>Your statements can take up as many lines of code as you like. For example, the



following statement is equivalent to the preceding ones:</P>



<PRE><FONT COLOR="#0066FF">$inputline



=



&lt;STDIN&gt;



;



</FONT></PRE>



<P>The collection of spaces, tabs, and new lines separating one token from another



is known as white space.</P>



<P>When programming in Perl, you should use white space to make your programs more



readable. The examples in this book use white space in the following ways:







<UL>



	<LI>New statements always start on a new line.



	<P>



	<LI>One blank space is used to separate one token from another (except in special



	cases, some of which you'll see in this chapter).



</UL>







<CENTER>



<H4><A NAME="Heading20<FONT COLOR="#000077">What the Tokens Do When Reading



from Standard Input</FONT></H4>



</CENTER>



<P>As you've seen already, the statement</P>



<PRE><FONT COLOR="#0066FF">$inputline = &lt;STDIN&gt;;



</FONT></PRE>



<P>consists of four tokens: <TT>$inputline</TT>, <TT>=</TT>, <TT>&lt;STDIN&gt;</TT>,



and <TT>;</TT>. The following subsections explain what each of these tokens does.



The $inputline and = Tokens The first token in line 1, <TT>$inputline</TT> (at the



left of the statement), is an example of a scalar variable. In Perl, a scalar variable



can store one piece of information.</P>



<P>The <TT>=</TT> token, called the assignment operator, tells the Perl interpreter



to store the item specified by the token to the right of the <TT>=</TT> in the place



specified by the token to the left of the <TT>=</TT>. In this example, the item on



the right of the assignment operator is the <TT>&lt;STDIN&gt;</TT> token, and the



item to the left of the assignment operator is the <TT>$inputline</TT> token. Thus,



<TT>&lt;STDIN&gt;</TT> is stored in the scalar variable <TT>$inputline</TT>.</P>



<P>Scalar variables and assignment operators are covered in more detail in Teach



Yourself Perl 5 in 21 Days. The &lt;STDIN&gt; Token and the Standard Input File The



next token, <TT>&lt;STDIN&gt;</TT>, represents a line of input from the standard



input file. The standard input file, or STDIN for short, typically contains everything



you enter when running a program.</P>



<P>For example, when you run <TT>program29_1</TT> and enter</P>



<PRE><FONT COLOR="#0066FF">This is a line of input.



</FONT></PRE>



<P>the line you enter is stored in the standard input file.</P>



<P>The <TT>&lt;STDIN&gt;</TT> token tells the Perl interpreter to read one line from



the standard input file, where a line is defined to be a set of characters terminated



by a new line. In this example, when the Perl interpreter sees <TT>&lt;STDIN&gt;</TT>,



it reads</P>



<PRE><FONT COLOR="#0066FF">This is a line of input.



</FONT></PRE>



<P>If the Perl interpreter then sees another <TT>&lt;STDIN&gt;</TT> in a different



statement, it reads another line of data from the standard input file. The line of



data you read earlier is destroyed unless it has been copied somewhere else.







<DL>



	<DT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading21<FONT COLOR="#000077"><B>NOTE:</B> </FONT>If there are more



	lines of input than there are <TT>&lt;STDIN&gt;</TT> tokens, the extra lines of input



	are ignored. 



<HR>







</DL>







<P>Because the <TT>&lt;STDIN&gt;</TT> token is to the right of the assignment operator



<TT>=</TT>, the line</P>



<PRE><FONT COLOR="#0066FF">This is a line of input.



</FONT></PRE>



<P>is assigned to the scalar variable <TT>$inputline</TT>. The ; Token The <TT>;</TT>



token at the end of the statement is a special token that tells Perl that the statement



is complete. You can think of it as a punctuation mark that is like a period in English.



<CENTER>



<H3><A NAME="Heading22<FONT COLOR="#000077">Line 3: Writing to Standard Output</FONT></H3>



</CENTER>



<P>Now that you understand what statements and tokens are, consider line 3 of Listing



29.1:</P>



<PRE><FONT COLOR="#0066FF">print ($inputline);



</FONT></PRE>



<P>This statement refers to the library function that is called <TT>print</TT>. Library



functions, such as <TT>print</TT>, are provided as part of the Perl interpreter;



each library function performs a useful task.</P>



<P>The <TT>print</TT> function's task is to send data to the standard output file.



The standard output file stores data that is to be written to your screen. The standard



output file sometimes appears in Perl programs under the name <TT>STDOUT</TT>.</P>



<P>In this example, <TT>print</TT> sends <TT>$inputline</TT> to the standard output



file. Because the second line of the Perl program assigns the line</P>



<PRE><FONT COLOR="#0066FF">This is a line of input.



</FONT></PRE>



<P>to <TT>$inputline</TT>, this is what <TT>print</TT> sends to the standard output



file and what appears on your screen.



<CENTER>



<H4><A NAME="Heading23<FONT COLOR="#000077">Function Invocations and Arguments</FONT></H4>



</CENTER>



<P>When a reference to <TT>print</TT> appears in a Perl program, the Perl interpreter



calls, or invokes, the <TT>print</TT> library function. This function invocation



is similar to a function invocation in C, a <TT>GOSUB</TT> statement in BASIC, or



a <TT>PERFORM</TT> statement in COBOL. When the Perl interpreter sees the <TT>print</TT>



function invocation, it executes the code contained in <TT>print</TT> and returns



to the program when <TT>print</TT> is finished.</P>



<P>Most library functions require information to tell them what to do. For example,



the <TT>print</TT> function needs to know what you want to print. In Perl, this information



is supplied as a sequence of comma-separated items located between the parentheses



of the function invocation. For example, the statement you've just seen</P>



<PRE><FONT COLOR="#0066FF">print ($inputline);



</FONT></PRE>



<P>supplies one piece of information that is passed to <TT>print</TT>: the variable



<TT>$inputline</TT>. This piece of information commonly is called an argument.</P>



<P>The following call to <TT>print</TT> supplies two arguments:</P>



<PRE><FONT COLOR="#0066FF">print ($inputline, $inputline);



</FONT></PRE>



<P>You can supply <TT>print</TT> with as many arguments as you like; it prints each



argument starting with the first one (the one on the left). In this case, <TT>print</TT>



writes two copies of <TT>$inputline</TT> to the standard output file.</P>



<P>You also can tell <TT>print</TT> to write to any other specified file.



<CENTER>



<H3><A NAME="Heading24<FONT COLOR="#000077">Error Messages</FONT></H3>



</CENTER>



<P>If you incorrectly type a statement when creating a Perl program, the Perl interpreter



detects the error and tells you where the error is located.</P>



<P>For example, look at Listing 29.3. This program is identical to the program you've



been seeing all along, except that it contains one small error. Can you spot it?



<CENTER>



<H3><A NAME="Heading25<FONT COLOR="#000077">Listing 29.3. A program containing



an error.</FONT></H3>



</CENTER>



<PRE><FONT COLOR="#0066FF">1: #!/usr/local/bin/perl



2: $inputline = &lt;STDIN&gt;



3: print ($inputline);



</FONT></PRE>



<P>The output should give you a clue.</P>



<PRE><FONT COLOR="#0066FF">$ program29_3



Syntax error in file program29_3 at line 3, next char (



Execution of program29_3 aborted due to compilation errors.



$



</FONT></PRE>



<P>When you try to run this program, an error message appears. The Perl interpreter



has detected that line 2 of the program is missing its closing <TT>;</TT> character.



The error message from the interpreter tells you what the problem is and identifies



the line on which the problem is located.







<DL>



	<DT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading26<FONT COLOR="#000077"><B>TIP:</B> </FONT>You should fix errors



	starting from the beginning of your program and working down. When the Perl interpreter



	detects an error, it tries to figure out what you meant to say and carries on from



	there; this feature is known as error recovery. Error recovery enables the interpreter



	to detect as many errors as possible at one time, which speeds up the development



	process. Sometimes, however, the Perl interpreter can get confused and think you



	meant to do one thing when you really meant to do another. In this situation, the



	interpreter might start trying to detect errors that don't really exist. This problem



	is known as error cascading. It's usually pretty easy to spot error cascading. If



	the interpreter is telling you that errors exist on several consecutive lines, it



	usually means that the interpreter is confused. Fix the first error, and the others



	might very well go away. 



<HR>







</DL>







<CENTER>



<H3><A NAME="Heading27<FONT COLOR="#000077">Interpretive Languages Versus Compiled



Languages</FONT></H3>



</CENTER>



<P>As you've seen, running a Perl program is easy. All you need to do is create the



program, mark it as executable, and run it. The Perl interpreter takes care of the



rest. Languages such as Perl that are processed by an interpreter are known as interpretive



languages.</P>



<P>Some programming languages require more complicated processing. If a language



is a compiled language, the program you write must be translated into machine-readable



code by a special program known as a compiler. In addition, library code might need



to be added by another special program known as a linker. After the compiler and



linker have done their jobs, the result is a program that can be executed on your



machine--assuming, of course, that you have written the program correctly. If not,



you have to compile and link the program all over again.</P>



<P>Interpretive languages and compiled languages both have advantages and disadvantages,



as mentioned here:







<UL>



	<LI>As you've seen with Perl, it takes very little time to run a program in an interpretive



	language.



	<P>



	<LI>Interpretive languages, however, cannot run unless the interpreter is available.



	Compiled programs, on the other hand, can be transferred to any machine that understands



	them.



</UL>







<P>As you'll see, Perl is as powerful as a compiled language. This means that you



can do a lot of work quickly and easily.



<CENTER>



<H3><A NAME="Heading28<FONT COLOR="#000077">Summary</FONT></H3>



</CENTER>



<P>In this chapter you learned that Perl is a programming language that provides



many of the capabilities of a high-level programming language such as C. You also



learned that Perl is easy to use; basically, you just write the program and run it.</P>



<P>You saw a very simple Perl program that reads a line of input from the standard



input file and writes the line to the standard output file. The standard input file



stores everything you type from your keyboard, and the standard output file stores



everything your Perl program sends to your screen.</P>



<P>You learned that Perl programs contain a header comment, which indicates to the



system that your program is written in Perl. Perl programs also can contain other



comments, each of which must be preceded by a <TT>#</TT>.</P>



<P>Perl programs consist of a series of statements, which are executed one at a time.



Each statement consists of a collection of tokens, which can be separated by white



space.</P>



<P>Perl programs call library functions to perform certain predefined tasks. One



example of a library function is <TT>print</TT>, which writes to the standard output



file. Library functions are passed chunks of information called arguments; these



arguments tell a function what to do.</P>



<P>The Perl interpreter executes the Perl programs you write. If it detects an error



in your program, it displays an error message and uses the error-recovery process



to try to continue processing your program. If Perl gets confused, error cascading



can occur, and the Perl interpreter might display inappropriate error messages.</P>







<P>Finally, you learned about the differences between interpretive languages and



compiled languages, and that Perl is an example of an interpretive language.



















</td>
</tr>
</table>

<!-- begin footer information -->



</body></html>

⌨️ 快捷键说明

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