'
) provide a literal translation of the text they surround --
inside single quotes there are no special characters except for the
single quote itself, which must be preceded by a backslash (\
).
Double quotes ("
) provide a very useful feature called variable
interpolation. This means that, inside of your perl program, a variable
name inside of double quotes will be replaced by a representation of the
variable's value. This makes it very easy to create output containing
variable values. For example
$name = "Fred"; print "Hello, $name";will result in ``Hello, Fred'' being printed. This means that if, for example, you wanted to print a dollar sign, you would need to precede it with a backslash (
\
). The same is
true for several other characters, including the at sign (@
).
When variable interpolation is performed on an array, perl will insert spaces between the elements of the array, making it very useful for quickly checking the contents of an array. Note the difference in the output of the final two statements:
@friends = ('Joe','Fred','Tom','Nancy'); print @friends; # prints JoeFredTomNancy print "@friends"; # prints Joe Fred Tom Nancy
When you use double quotes, there are a number of escape sequences which have special meaning, summarized in Table 3.1.
Perl is actually much more lenient than most languages regarding the use of quotes around string values. A general rule for perl is that if you have a so-called bareword (a string value without quotes) appearing in a context in which a quoted string could be used, perl will treat that bareword as if it were quoted, unless the bareword is the name of a function, in which case it will evaluate the function with its default value. (Section 1.4) You should use barewords in this way with the utmost care, because if one of the barewords happens to coincide with a function in perl, you will get very surprising results. Consider the following four variables:
$first = "test"; $second = test; $third = "chop"; $fourth = chop;As you might expect, the variables
$first
and
$second
will both have the value test. However, while
$third
will be equal to chop, $fourth
will be
undefined. This is because there happens to be a perl function called
chop, which removes and returns the last character of a string value,
and since
its default value ($_
) is undefined, it returns an undefined value.
When you use a numeric variable in a double-quoted string, perl silently creates a string equivalent and uses that. Perl will remember both the numeric version and the string version of the variable as long as the variable exists.
Beyond these capabilities, perl offers several more advanced quoting operators. (See Section 3.2).
Perl provides two other operators for string values. A single period (.) is perl's string concatenation operator. For example:
$first = 'black'; $second = 'jack'; print $first . $second;would result in ``blackjack'' being printed. The spaces surrounding the period in the above example are optional, and were added to make the code more readable. Note that no blanks are added between the concatenated strings. You could add them explicitly, that is:
print $first . ' ' . $second;would insert a blank between the two words. Notice that string variables and literal (single- or double-quoted) strings can be freely mixed when using the concatenation operator. In fact this is true for most functions and operators.
However, most perl programmers would take advantage of the variable extrapolation provided by double quotes, and simply use
print "$first $second";In either case, neither of the two strings which are concatenated is modified in any way. Besides printing these results, they could just as easily be stored as variables for later use:
$thegame = "$first $second";would set the scalar variable
$thegame
equal to the string
value ``black jack''.
When perl performs variable interpolation, it will recognize non-alphanumeric
characters (except for underline or double colons) as marking the end of a variable.
So if you had a variable called $title
with the value ``mypage'',
perl would correctly interpolate the expression "$title.html"
into
``mypage.html''. Now suppose we want to append the number ``1''
to the end of the string stored in the $title
variable. Perl will interpret
"$title1"
as an entirely different variable. In cases like these, you
need to surround the variable name with curly braces ({}
) after the dollar
sign, to let perl know when the variable name ends and the literal characters
begin. In the current example, the string "${title}1"
would correctly
be interpolated into ``mypage1''.
The lower-case letter ``x'' serves as a repetition operator
for string values in perl.
The value on the right-hand side of the x
should be a number, and the
value on the left-hand side of the x
will be repeated that many times,
creating a new string value. If a number is used on the left-hand side of
the x
, perl will silently convert it to a string value. The following
examples illustrate these points:
print '-' x 10; # prints ---------- $x = 5; $fives = $x x 4; # sets $fives equal to '5555' $y = 12/7; print $y x 2; # prints 1.714285714285711.71428571428571In the last example, notice that perl's default string representation of a number may contain lots of decimal digits. To get more control over this, you can use the sprintf function. (See Section