Previous Table of Contents Next


Double Quotes and Single Quotes

Eagle-eyed readers might have noticed a small difference between the strings on the second and third lines of the program. The second line uses single quotes (‘Hello’), whereas the third line uses double quotes (“$greeting world!\n”)

Perl performs variable interpolation within double quotes, which means that it replaces a variable’s name with its value. (See Figure 1-2.) Single quotes don’t perform variable interpolation, which is why


Figure 1-2  Variable interpolation

$greeting = ‘Hello’;
print $greeting;

prints $greeting, whereas

$greeting = ‘Hello’;
print “$greeting”;

prints Hello.

Double quotes do something else that single quotes don’t do: They interpret backslashed characters. Inside double quotes, \n turns into a newline, but inside single quotes, it remains a slash-in-front-of-an-n.

print “Hello, world!\n\n\n”;

prints Hello, world followed by two blank lines.

print ‘Hello, world!\n\n\n’;

prints Hello, world!\n\n\n (Oops!)

“Plain” strings can be either double-quoted or single-quoted.

$string   = “Hello”;

$greeting = ‘Bonjour’;

But back to scalars. A scalar can be a string

$greeting   = ‘Bonjour’;

$salutation = “Hello, world!\n”;

or a number.

$number = -5;

$e =  2.71828;

You can name a scalar anything (well, nearly anything) you want, but it should begin with $, followed by a letter or an underscore, followed by more letters, digits, or underscores.

Like C and UNIX, Perl is case-sensitive, so capitalization matters. The following are all different (but legal) scalars:

• $Greeting
• $GREETING
• $gReEtINg
• $greeting

You can make variable names as long as you wish.

Functions

print() is a Perl “function.” Most functions accept one or more arguments (or parameters) and produce a single return value. Some functions cause something else to happen—a side effect. In the case of print(), it’s the side effect that’s important: writing text to your screen.

In C, functions require parentheses around their arguments, as in printf(“Hello!\n”). In Perl, parentheses are needed only to disambiguate between alternate interpretations. (That has to do with the relative precedence of Perl’s operators. There’s more on operators later in this chapter, and a table of their precedences in Appendix F.)

Parentheses are seldom used with print(). Sometimes you can even omit the quotes, as in hello3, which is featured in Listing 1-4.

Listing 1-4 hello3: Quotes aren’t always necessary

#!/usr/bin/perl -w
$greeting = ‘Hello’;
print $greeting;

print() sees just one argument: the scalar variable $greeting, which contains the string Hello.

        % hello3
RESULT: Hello

Of course, hello and hello2 print Hello, world, not just Hello. Luckily, print() can accept multiple arguments, so you can print both Hello and world with one statement.

Perl separates arguments with commas. In hello4 (Listing 1-5), three arguments are supplied to print(): $greeting, ‘ world!’, and “\n”.

Listing 1-5 hello4: Using commas to separate function arguments

#!/usr/bin/perl -w
$greeting = ‘Hello’;
print $greeting, ‘ world!’, “\n”;

        % hello4
RESULT: Hello world!

Now let’s make an interactive program, one that asks the user to enter some text. Listing 1-6 shows a short example.

Listing 1-6 spitback: Grabbing user input with <>

#!/usr/bin/perl -w
print ‘Enter a greeting: ’;
$greeting = <>;                    # (Now the user enters a line of text)
print $greeting;

The angle brackets (<>, also called the “diamond operator”) access what the user types. Don’t worry about how it works—you’ll learn more about that in Chapter 3. But it sure is handy, isn’t it? (You can use it to access command-line arguments, too.)

% spitback
Enter a greeting: Howdy!
Howdy!

        % spitback
RESULT: Enter a greeting: I can type anything I want...
        I can type anything I want...

You can use <> to query the user more than once. The madlibs program, shown in Listing 1-7, asks the user for a number, a noun, and a verb, and then combines all three into a single phrase. It also uses another Perl function (chomp()) and an operator (.) to join strings together. Note the similarity between the three “chunks” of code that grab and manipulate user entries.

Listing 1-7 madlibs: Retrieving and manipulating user-typed text

#!/usr/bin/perl -w

# Ask the user to type a number.  Place the result in $num,
# chopping off the newline (created when the user pressed <ENTER>).
print ‘Pick a number between 1 and 10: ’;
$num = <>;
chomp $num;

# Now do the same thing for $noun.

print ‘Gimme a noun: ’;
$noun = <>;
chomp $noun;
$noun = $noun . ‘s’;           # Pluralize noun by appending an ‘s’

# And do the same thing for $verb.

print ‘Now type a verb: ’;
$verb = <>;
chomp $verb;
$verb = ‘a-’ . $verb . ‘ing’;  # Add a prefix and a suffix to $verb

print “\n$num $noun $verb\n”;

When madlibs is run, the user is asked to provide a number, then a noun, and finally a verb. madlibs waits until the user presses [ENTER] before proceeding to the next statement.

        % madlibs
RESULT: Pick a number between 1 and 10: 10
        Gimme a noun: lord
        Now type a verb: leap

        10 lords a-leaping

Notice how the three variables $num, $noun, and $verb are all processed the same way, even though $num is a number and $noun and $verb are strings. To Perl, they’re all just scalars.


Previous Table of Contents Next