Variables

A variable is a symbolic placeholder for a value, a lot like the variables in algebra. Perl has several built-in variable types:

Scalars: $variable_name
A single-valued variable, always preceded by a $ sign.

Arrays: @array_name
A multi-valued variable indexed by integer, preceded by an @ sign.

Hashes: %hash_name
A multi-valued variable indexed by string, preceded by a % sign.

Filehandle: FILEHANDLE_NAME
A file to read and/or write from. Filehandles have no special prefix, but are usually written in all uppercase.
We discuss arrays, hashes and filehandles later.

Scalar Variables

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

Scalar Variable String Interpolation

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

Operations on Scalar Variables

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:

$a++
Increment $a by one

$a--
Decrement $a by one

$a += $b
Modify $a by adding $b to it.

$a -= $b
Modify $a by subtracting $b from it.

$a *= $b
Modify $a by multiplying $b to it.

$a /= $b
Modify $a by dividing it by $b.

$a .= $b
Modify the string in $a by appending $b to it.

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.

Preincrement vs postincrement

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.

Weird Perl Assignment Idioms

Modify a Value and Save the Original in One Operation


  $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'

Swap the Values of Two Variables

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; 

Rotate the Values of Three Variables


  ($onions,$potatoes,$turnips) = ($potatoes,$turnips,$onions);

  # $onions <- $potatoes
  # $potatoes <- $turnips
  # $turnips <- $onions

<< Previous
Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Tue Oct 10 17:06:12 EDT 2000