[ Tutorial Page | Glossary | Course Page | Lectures ]

Civil Engineering 82.562 -- The Perl Programming Language

Strings and Variable Interpolation

----------

String Delimiters and Escape Sequences

We mentioned earlier that there was significant difference between using the single quote (') and the double quote (") to delimit strings. Now it is time to explain the difference.

If a string is delimited by a pair of single quotes, then all characters within the string are taken literally; there are no substitutions made. For example, the string

	'abc\n'
contains 5 characters: 'a', 'b', 'c', '\' and 'n'. This may not be very surprising.

In contrast, the double-quote delimited string:

	"abc\n"
contains 4 characters: 'a', 'b', 'c', and the newline character. What is going on here?

In a string delimited by double-quotes, a number of special escape sequences are allowed. These are special sequences of two or more characters that are used to represent single, special (usually non-printable) characters. The first character of the sequence is always the single back-slash '\'; following that are one or more characters denoting a special character. The special character is substituted for the entire sequence by the Perl processor.

For those with C and/or Unix experiences, the escape sequences will be pretty familiar:

        \t              tab
        \n              newline
        \r              return
        \f              form feed
        \b              backspace
        \a              alarm (bell)
        \e              escape
        \033            octal char
	\"		double quote
	\\		back slash
	\c		c - where 'c' is a normal character
There are others; see the manual.

Variable Interpolation

Not only that, the Perl processor will interpolate (substitute) the value of variables into double-quote delimited strings whenever the string is encountered during evaluation of an expression (i.e., at the time the statement containing the string is executed).

Anything that looks like a variable reference has the value obtained, that value converted to a character string, and the string substituted for the reference. For example, consider the following:

	$width = 10;
	$length = 20;
	$area = $length * $width;
	$msg = "The area of a $width x $length rectangle is $area";
The value assigned to variable $msg is the string:
	The area of a 10 x 20 rectangle is 200
This is a real easy way to get results printed, as in:
	...
	$area = $length * $width;
	print("The area of a $width x $length rectangle is $area\n");
Here we ended the string with a newline so that the next line of output will start on a new line.

Arrays can be interpolated as well. Be default, the elements are substituted in order, separated by a single blank. For example:

	@vec = (10,20,30.5);
	print("The vector elements are: @vec\n");
will print
	The vector elements are: 10 20 30.5

If you want '$', '@', or '%' to appear in a double-quote delimited string, you should protect them by preceding them with a single backslash, else Perl might think they are variable references.

[Quiz | Next Lesson ]


-- NMH, Wed Jan 25 17:18:09 EST 1995