• Aucun résultat trouvé

Fundamental Programming Syntax

Dans le document This book is dedicated to its readers. (Page 55-61)

Among the most elementary items of syntax you use when programming with Perl are those you use to create comments, make statements, force new lines, and escape from regular program interpretation so that you can display special characters. This section reviews these items.

Comments Using #

The pound sign (#) provides a way that you can tell the Perl interpreter to skip over notes you include in a program. Such notes are referred to as comments. Comments are not part of the code.

Any line that begins with a pound sign is considered a comment.

The interpreter reads from left to right. For this reason, if you want to make it so that the inter-preter does not read a line of text you have included in your program, you type the pound sign as the first character at which you want the interpreter to stop reading. From that point on, until the interpreter reaches the end-of-line character, it does not try to interpret anything you have typed as Perl code. Consider the following sample of code:

#Listing03_04

#Comments proceeded to the end-of-line character

#You press Enter to insert an EOL character

print "\nListing03_04.pl works."; #Comments can follow code print "\nReady to add lines!";

CHAPTER 3

}

Scalars and Strings

Q Q Q

34

TEAM LinG

The first three lines of Listing03_04.pl provide standard comments. Each line begins with a pound sign. On the fourth line, beginning “Comments can …,” you see a comment to the right of the program statement. Such a comment creates no problem. The interpreter knows not to read it. If you were to reverse the position of the comment and the statement, however, the in-terpreter would immediately have a problem. You must place the comment on a separate line or, if on the same line, after your program statements. Figure 3.4 illustrates the output of Listing03_04.

Figure 3.4

Comments to the left of code do not create problems.

How to Make a Statement

The semicolon (;) indicates that the interpreter should read the words to the left of the semicolon as a complete unit. A complete unit is a statement. The shortest statement you can make in Perl consists of a semicolon. Consider the following sample of code:

#Listing03_05

print "\nListing03_05.pl works."; #Comments can follow code print "\nReady to add lines!";

;;;

;

;

print "\nReady to add more lines!";

The first two lines of Listing03_05 provide standard statements. For example, interpolated strings (strings within double quotation marks) follow the print() function. The next three lines provide only semicolons. On the third line, you see three semicolons in a row. On the following two lines, one semicolon occupies each line. For the final line, you see a standard statement beginning with the print() function.

The interpreter reads the semicolons as statements even if they do nothing more than cause the interpreter to continue on the next statement. As Figure 3.5 illustrates, the semicolons cause no extra line spaces to appear in the program output.

Q Preliminary Work

Leaving Out Semicolons

If you delete one of the semicolons following a statement, you can create an interpreter error.

Consider the following code sample:

#Listing03_06

#Missing semicolons result in interpreter error messages

# To correct the problem, add a semicolon on each line

# right after the closing quotation marks

print "\nListing03_06.pl works." #1 semicolon left off print "\nReady to add lines!" #2 semicolon left off

;;;

;

;

print "\nReady to add more lines!";

The lines identified in the comments as #1 and #2 lack semicolons. When you submit your code to the interpreter, you might see a message along the following lines:

syntax error at Listing03_06.pl line 8, near "print"

Execution of Listing03_06.pl aborted due to compilation errors

The Perl interpreter might not object every time you forget to type a semicolon. Consider the lines identified with the #2 comment in Listing03_06. The first semicolon on the following line (the line of three semicolons) replaces the missing semicolon. In such a situation, the interpreter does not object. However, if you leave out the semicolon for #1, the interpreter invariably objects.

Multiple Statements on One Line

You saw in the previous example a succession of semicolons (statements) on one line. What applies to statements consisting of one semicolon applies to statements consisting of full ex-pressions. Consider the following code sample:

Figure 3.5

Semicolons alone consti-tute statements but pro-duce no visible change.

CHAPTER 3

}

Scalars and Strings

Q Q Q

36

TEAM LinG

#Listing03_07.pl

#Multiple statements on one line

print "\nListing03_07.pl works."; #single statement print "\nMultiple statements on one line."; #single statement

#Mutliple statements on one line follow

;;;

print "\nReady to add more lines!"; print "\nAnd more lines...";!";

Figure 3.6 illustrates the output. Notice that the interpreter reads the statements on the final line without problems. The output appears on separate lines because the \n forces the string fol-lowing it onto a new line.

Figure 3.6

Successive statements on a single line cause no errors.

Q Although composing code with several statements on one line creates no syntax errors, consider that many experienced programmers regard this as poor programming style. Experienced programmers often insist that you should place no more than one statement on each line of code.

Escape Sequences

In most of the code samples shown so far, the quoted text used with the print() function begins with a slash character followed by an “n” character (\n). This combination of characters causes a line return to be inserted into the output of the print() function. The slash can be used with several other characters as well. The slash and the character that accompanies it are together called an escape sequence. An escape sequence escapes the normal execution of your program. It makes it so that the Perl interpreter interprets a character like any other text character rather than trying to process it as a language feature. To see how this works, consider the fol-lowing code sample:

#Listing03_08.pl

#Different uses of the escape sequence

Q Preliminary Work

print "\nListing03_08.pl works.";

print "\nEscape characters:";

print "\nNew\nNew\nNew";

print "\nHe said, \"That's new.\"";

print "\nShe said \'vlap\' twice.";

print "\nThe bill was \$25.00.\b\b";

print "\nHere's a \uv\la\ur\li\ue\ld word.";

print "\nThe lines read: \nLife teaches \n\t\tof time \n\tand sorrow.";

Figure 3.7 illustrates the appearance of the command line output after the use of the escape sequences in Listing03_08.

Figure 3.7

Escape sequences allow you to format interpolated strings.

As Figure 3.7 illustrates, using a slash and double quotes (\”) displays double quotes. A slash with a single quote (\’) shows a single quote. A slash with a u (\u) forces the character that follows to uppercase. In each instance, the escape sequence allows you to perform a formatting procedure or some other type of operation that the characters on the keyboard do not alone allow you to perform. Table 3.1 provides a summary and discussion of escape sequences.

Table 3.1 Escape Sequences

Sequence Discussion

\n Forces a new line.

\t Forces a tab. What you see depends on the tab setting of your display.

\$ Creates a dollar sign. Otherwise, the next term is read as a scalar.

\\ Allows you to show a backslash. Otherwise, the next character is likely to be read as a part of an escape sequence.

CHAPTER 3

}

Scalars and Strings

Q Q Q

38

TEAM LinG

Sequence Discussion

\* Allows you to show an asterisk.

\” Allows you to show double quotation marks within quotation marks that create an interpolated string. Otherwise, the quotation marks end the string and in all likelihood cause an error.

\@ Allows you to show the “at” sign.

\b Forces a backspace.

\u Sets the character that follows to uppercase.

\U Sets all of the characters that follow to uppercase.

\l Sets the character that follows to lowercase.

\L Sets all of the characters that follow to lowercase.

\’ Same as with double quotes, except here you deal with a literal string.

Scalars and Strings

When you write programs in Perl, you deal with categories of data rather than data types. As mentioned in Chapter 1, these data categories fall under the headings of scalar,hash,array, andhandle. Of these four categories of data, the most basic has to do with identifiers that pertain to isolated or individualized strings and numbers. In Perl, such individualized strings and num-bers fall under the heading of scalar values. Figure 3.8 abstractly illustrates the way that you can visualize the different forms of scalar values. (Figure 3.9 provides a concrete version of scalars.)

Figure 3.8

Scalar values in Perl con-sist of strings and numbers.

As Figure 3.8 illustrates, when you program with Perl, you identify scalars with a currency sign ($). Any value that you store as a scalar exists as a single retrievable value, even if it is a collection of words (a sentence or longer unit of composition). When you store text in a

Q Scalars and Strings

scalar, you store it as an interpolated string or as a literal string. An interpolated string allows you to place variables in the string and retrieve values from the variables. A literal string makes it so that you see exactly what you include in the string, regardless of how you use it.

When you store a number, such as 2 or 2.2 in a scalar, the value 2 is known as an integer, a whole number. The value 2.2 is known as a rational or real number or floating decimal. With Perl you can assign interpolated strings, literal strings, integers, and floating decimals to scalars.

Dans le document This book is dedicated to its readers. (Page 55-61)

Documents relatifs