When a string literal is double-quoted, it is subject to variable interpolation (besides being checked for backslash escapes). This means that the string is scanned for possible scalar variable[2] names - namely, a dollar sign followed by letters, digits, and underscores. When a variable reference is found, it is replaced with its current value (or an empty string if the variable has not yet been assigned a value). For example:
$name = "fred"; $barney = "some text $name"; # $barney is now "some text fred" $charlie = "no such variable $what"; # $charlie is "no such variable "
The text that replaces the variable is not rescanned; that is, even if there are dollar signs in the replaced value, no further replacement occurs:
$xray = '$fred'; # literally a dollar sign followed by "fred" $yahoo = "hey $xray"; # value is 'hey $fred': no double substitution
To prevent the substitution of a variable with its value, you must either alter that part of the string so that it appears in single quotes, or precede the dollar sign with a backslash, which turns off the dollar sign's special significance:
$fred = 'hi'; $barney = "a test of " . '$fred'; # literally: 'a test of $fred' $barney2= "a test of \$fred"; # same thing
The variable name will be the longest possible variable name that makes sense at that part of the string. This can be a problem if you want to follow the replaced value immediately with some constant text that begins with a letter, digit, or underscore. As Perl scans for variable names, it would consider those characters to be additional name characters, which is not what you want. Perl provides a delimiter for the variable name. Simply enclose the name of the variable in a pair of curly braces. Or, you can end that part of the string and start another part of the string with a concatenation operator:
$fred = "pay"; $fredday = "wrong!";
$barney = "It's $fredday"; # not payday, but "It's wrong!"
$barney = "It's ${fred}day"; # now, $barney gets "It's payday"
$barney2 = "It's $fred"."day"; # another way to do it
$barney3 = "It's " . $fred . "day"; # and another way
The case-shifting string escapes can be used to alter the case of letters brought in with variable interpolation.[3] For example:
$bigfred = "\Ufred"; # $bigfred is "FRED" $fred = "fred"; $bigfred = "\U$fred"; # same thing $capfred = "\u$fred"; # $capfred is "Fred" $barney = "\LBARNEY"; # $barney is now "barney" $capbarney = "\u\LBARNEY"; # $capbarney is now "Barney" $bigbarney = "BARNEY"; $capbarney = "\u\L$bigbarney"; # same
As you can see, the case-shifting string escapes are remembered within a string until they are used, so even though the first letter of BARNEY doesn't follow the \u, it remains uppercase because of the \u.
The term variable interpolation is often used interchangeably with double-quote interpolation, because strings that are double-quoted are subject to variable interpolation. So too, are backquoted strings, which we'll see in a later lesson.
Next: Scalar Operators
[3]
Although checkout the uc, ucfirst, lc, and
lcfirst functions in a later lesson.
Return to the page from whence you came
[2]
And array variables, but that's for a later lesson!
Return to the page from whence you came