A variable is a symbolic placeholder for a value, a lot like the variables in algebra. Perl has several built-in variable types:
Scalar variables have names beginning with $. The name must begin with a letter or underscore, and can contain as many letters, numbers or underscores as you like. These are all valid scalars:
You assign values to a scalar variable using the = operator (not to be confused with ==, which is numeric comparison). You read from scalar variables by using them wherever a value would go.
A scalar variable can contain strings, floating point numbers, integers, and more esoteric things. You don't have to predeclare scalars. A scalar that once held a string can be reused to hold a number, and vice-versa:
Code:
$p = 'Potato'; # $p now holds the string "potato" $bushels = 3; # $bushels holds the value 3 $potatoes_per_bushel = 80; # $potatoes_per_bushel contains 80; $total_potatoes = $bushels * $potatoes_per_bushel; # 240 print "I have $total_potatoes $p\n"; |
Output:
I have 240 Potato
The example above shows one of the interesting features of double-quoted strings. If you place a scalar variable inside a double quoted string, it will be interpolated into the string. With a single-quoted string, no interpolation occurs.
To prevent interpolation, place a backslash in front of the variable:
print "I have \$total_potatoes \$p\n"; # prints: I have $total_potatoes $p |
You can use a scalar in any string or numeric expression like
$hypotenuse = sqrt($x**2 + $y**2) or $name =
$first_name . ' ' . $last_name. There are also numerous
shortcuts that combine an operation with an assignment:
Example Code:
$potatoes_per_bushel = 80; # $potatoes_per_bushel contains 80; $p = 'one'; $p .= ' '; # append a space $p .= 'potato'; # append "potato" $bushels = 3; $bushels *= $potatoes_per_bushel; # multiply print "From $p come $bushels.\n"; |
Output:
From one potato come 240.
The increment (++) operator can be placed before or after the variable name, and in either case, the effect on the variable is to bump it up by one. However, when you put the operator before the variable name, the value of the expression as a whole is the value of the variable after the operation (preincrement). If you put the operator after the variable name, the value of the expression is the value of the variable before it was incremented:
$potatoes = 80; # $potatoes holds 80 $onions = ++$potatoes; # $onions holds 81, $potatoes holds 81 $parsnips = $potatoes++; # parsnips holds 81, $potatoes holds 82 |
The decrement (--) operator works the same way.
$potatoes = 80; # $potatoes holds 80
($onions = $potatoes) += 10;
# $onions now 90, and $potatoes still 80
$sequence = 'GAGTCTTTTGGG';
($reversec = reverse $sequence) =~ tr/GATC/CTAG/;
# reverse reverses a string
# tr/// translates one set of characters into another
# $sequence holds 'GAGTCTTTTGGG'
# $reversec holds 'CCCAAAAGACTC' |
Here's a simple way to swap the values of two variables in one fast step:
($onions,$potatoes) = ($potatoes,$onions); # $onions now holds the original value of $potatoes, and vice-versa |
NOTE: The obvious alternative DOES NOT work:
$onions = $potatoes; $potatoes = $onions; # oops! |
Here a correct non-idiomatic alternative:
$tmp = $onions; $onions = $potatoes; $potatoes = $tmp; |
($onions,$potatoes,$turnips) = ($potatoes,$turnips,$onions); # $onions <- $potatoes # $potatoes <- $turnips # $turnips <- $onions |
|
| Contents |
Next |