Introducing UNIX and Linux |
PerlOverview |
VariablesIn the shell (and Awk also), there is only one type of variable, which is a string of characters. If those characters are digits, and the variable is used in a context where a number is expected, then the variable will be understood to be a number. In Awk, arrays are also provided for. In the shell, the value of a variable is accessed by placing a dollar symbol in front of the name of the variable, but its value is set by just using the name. In Awk, the dollar symbol is not needed at all. In Perl, the situation is more complex. First of all we consider
scalar variables. These are similar to variables you have
already met, and can contain strings of characters, which are
interpreted appropriately. The difference between Perl and
shell/Awk variables is that the name of a scalar variable is always
prepended with a dollar. For example, to set the value of
$i=0; $x=$i+1; Arrays are indicated by the In Awk, an array could be indexed by any string, and we
referred to such an array as an associative array. In Perl
it is called a hash. It is distinguished from an array in
three ways. First, the Simple use of Perl seldom requires the $month[0]='January'; $month[11]='December'; $age{'Chris'}=22; $age{'Sam'}=19; Either single or double quotes can be used to denote strings, but like the shell, dollars and escape characters will be interpreted within double quotes but not within single quotes. Strings can be concatenated - that is, joined together
- by use of the operator
is equivalent to the single string
|
Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck