2.6 Scalar Operators and FunctionsThe most common operation on a scalar variable is assignment , which is the way to give a value to a variable. The Perl assignment operator is the equal sign (as in C or FORTRAN), which takes a variable name on the left side and gives it the value of the expression on the right, like so: $a = 17; # give $a the value of 17 $b = $a + 3; # give $b the current value of $a plus 3 (20) $b = $b * 2; # give $b the value of $b multiplied by 2 (40)
Notice that the last line uses the
You may have noticed that scalar variables are always specified with the leading
You may use a
scalar assignment as a value as well as an operation, as in C. In other words, $b = 4 + ($a = 3); # assign 3 to $a, then add 4 to that # resulting in $b getting 7 $d = ($c = 5); # copy 5 into $c, and then also into $d $d = $c = 5; # the same thing without parentheses That last example works because assignment is right-associative. 2.6.1 Binary Assignment Operators
Expressions like $a = $a + 5; # without the binary assignment operator $a += 5; # with the binary assignment operator And so are these: $b = $b * 3; $b *= 3; In each case, the operator causes the existing value of the variable to be altered in some way, not simply overwriting the value with the result of some new expression. Another common assignment operator is the string concatenate operator: $str = $str . " "; # append a space to $str $str .= " "; # same thing with assignment operator
Nearly all binary operators are valid in this way. For example, a
raise to the power of
operator is written as
Like the simple assignment operator, each of these operators has a value as well: the new value of the variable. For example: $a = 3; $b = ($a += 4); # $a and $b are both now 7 2.6.2 Autoincrement and Autodecrement
As if it weren't already easy enough to add one to $a += 1; # with assignment operator ++$a; # with prefix autoincrement $d = 17; $e = ++$d; # $e and $d are both 18 now
Here, the $c = 17; $d = $c++; # $d is 17, but $c is now 18
Because the value of the operand changes, the operand must be a real scalar variable, not just an expression. You cannot say
The
autodecrement operator (
$x = 12; --$x; # $x is now 11 $y = $x--; # $y is 11, and $x is now 10
The autoincrement and autodecrement operators also work on floating-point values. So autoincrementing a variable with the value
2.6.3 The chop( ) and chomp( ) Functions
A useful built-in function is
$x = "hello world"; chop($x); # $x is now "hello worl"
Note that the value of the argument is altered here, hence the requirement for a scalar variable, rather than simply a scalar value. It would not make sense, for example, to write
The value returned is the discarded character (the letter $x = chop($x); # WRONG: replaces $x with its last character chop($x); # RIGHT: as above, removes the last character
If you give
When you chop a string that has already been chopped, another character disappears off into "bit heaven." For example: $a = "hello world\n"; chop $a; # $a is now "hello world" chop $a; # oops! $a is now "hello worl"
If you're not sure whether the variable has a newline on the end, you can use the slightly safer
$a = "hello world\n"; chomp ($a); # $a is now "hello world" chomp ($a); # aha! no change in $a 2.6.4 Interpolation of Scalars into StringsWhen 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[ 13 ] 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:
$a = "fred"; $b = "some text $a"; # $b is now "some text fred" $c = "no such variable $what"; # $c is "no such variable " The text that replaces the variable is not rescanned; even if there are dollar signs in the replaced value, no further replacement occurs: $x = '$fred'; # literally a dollar sign followed by "fred" $y = "hey $x"; # 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, and this result 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 You can use the case-shifting string escapes to alter the case of letters that are brought in with variable interpolation.[ 14 ] 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 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, as described in Chapter 14 . |
|