• Aucun résultat trouvé

Initial Values of Scalar Variables

Dans le document Teach Yourself Perl 5 in 21 days (Page 112-116)

In Perl, all scalar variables have an initial value of the null string, "". This means that you do not need to define a value for a scalar variable.

#!/usr/local/bin/perl

$result = $undefined + 2; # $undefined is not defined print ("The value of \$result is $result.\n");

This short program is perfectly legal Perl. The output is

The value of $result is 2.

Because $undefined is not defined, the Perl interpreter assumes that its value is the null string. This null string is then converted to 0, because it is being used in an addition operation. The result of the addition, 2, is assigned to $result.

TIP

Although you can use uninitialized variables in your Perl programs, you shouldn't. If your Perl program gets to be large (as many complicated programs do), it might be difficult to determine whether a particular variable is supposed to be appearing for the first time or whether it is a spelling mistake that should be fixed. To avoid

ambiguity and to make life easier for yourself, initialize every scalar variable before using it

Summary

Perl supports three kinds of scalar values: integers, floating-point numbers, and character strings.

Integers can be in three notations: standard (decimal) notation, octal notation, and hexadecimal notation. Octal notation is indicated by a leading 0, and hexadecimal notation is indicated by a leading 0x. Integers are stored as floating-point values and can be as long as the machine's floating-point precision (usually 16 digits or so).

Floating-point numbers can consist of a string of digits that contain a decimal point and an optional exponent. The exponent's range can be anywhere from about e-309 to e+308. (This value might be different on some machines.) When possible, floating-point numbers are displayed without the exponent; failing that, they are displayed in scientific

notation (one digit before the decimal point).

When you use floating-point arithmetic, be alert for round-off errors. Performing arithmetic operations in the proper order-operating on large numbers first-might yield better results.

You can enclose character strings in either double quotes (") or single quotes ('). If a scalar variable name appears in a character string enclosed in double quotes, the value of the variable is substituted for its name. Escape characters are recognized in strings enclosed in double quotes; these characters are indicated by a backslash (\).

Character strings in single quotes do not support escape characters, with the exception of \\ and \'. Scalar variable names are not replaced by their values.

Strings and integers are freely interchangeable in Perl whenever it is logically possible to do so.

Q&A

Q: If Perl character strings are not terminated by null characters, how does the Perl interpreter know the length of a string?

A: The Perl interpreter keeps track of the length of a string as well as its contents.

In Perl, you do not need to use a null character to indicate "end of string."

Q: Why does Perl use floating-point registers for floating-point arithmetic even though they cause round-off errors?

A: Basically, it's a performance issue. It's possible to write routines that store floating-point numbers as strings and convert parts of these strings to numbers as necessary; however, you often don't need more than 16 or so digits of precision anyway.

Applications that need to do high-speed arithmetic calculations of great precision usually run on special computers designed for that purpose.

Q: What happens if I forget to call chop when reading a number from the standard input file?

A: As it happens, nothing. Perl is smart enough to ignore white space at the end of a line that consists only of a number. However, it's a good idea to get into the habit of using chop to get rid of a trailing newline at all times, because the trailing newline becomes significant when you start doing string comparisons.

(You'll learn about string comparisons on Day 4, "More Operators.")

Workshop

The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.

Quiz

1. Define the following terms:

a round-off error b octal notation c precision

d scientific notation

2. Convert the following numbers from octal notation to decimal:

a 0377 b 06 c 01131

3. Convert the following numbers from hexadecimal notation to decimal notation:

a 0xff b 0x11 c 0xbead

4. What does the following line print?

print ("I am bored\b\b\b\b\bhappy!\n");

5. Suppose the value of $num is 21. What string is assigned to $text in each of the following cases?

a $text = "This string contains $num.";

b $text = "\\$num is my favorite number.";

c $text = 'Assign \$num to this string.';

6. Convert the following numbers to scientific notation:

a 43.71

b 0.000006e-02 c 3

d -1.04

Exercises

1. Write a program that prints every number from 0 to 1 that has a single digit after the decimal place (that is, 0.1, 0.2, and so on).

2. Write a program that reads a line of input and prints out the following:

1 if the line consists of a non-zero integer

0 if the line consists of 0 or a string

(Hint: Remember that character strings are converted to 0 when they are converted to integers.)

3. Write a program that asks for a number and keeps trying until you enter the number 47. At that point, it prints Correct! and rings a bell.

4. BUG BUSTER: What is wrong with the following program?

#!/usr/local/bin/perl

$inputline = <STDIN>;

print ('here is the value of \$inputline\', ": $inputline");

5. BUG BUSTER: What is wrong with the following code fragment?

$num1 = 6.02e+23;

$num2 = 11.4;

$num3 = 5.171e+22;

$num4 = -2.5;

$result = $num1 + $num2 - $num3 + $num4;

6. BUG BUSTER: What is wrong with the following statement?

$result = "26" + "0xce" + "1";

Chapter 4

String Comparison Versus Integer Comparison

Comparison and Floating-Point Numbers

Using Logical Operators

Assignment Operators as Subexpressions

Using Autoincrement and Autodecrement

The Autoincrement Operator Pre-Increment

The Autoincrement Operator Post-Increment

The Autodecrement Operator

Using Autoincrement With Strings

The String Concatenation and Repetition Operators

The String-Concatenation Operator

The String-Repetition Operator

Dans le document Teach Yourself Perl 5 in 21 days (Page 112-116)

Documents relatifs