Perl Variables and Types


Type Character Example Is a name for
Scalar $ $cents A number (int or float) or
string or reference
Array @ @large A list of values, keyed by number;
zero based: @large[0]
Hash % %stuff A group of values, keyed by string;
a stuff of key is value: %stuff{key}
Subroutine & &how A callable chunk of perl code
Typeglob * *struck Everything named struck
Reference \ \ANY Reference to ANY, e.g. \%stuffi, or \@ary

To assign a list of values to an array assign a comma sepatrated list in parenthesis, e.g.:
   @home = ("couch", "chair", "table");
Can be in an assignment, e.g. following is a swap:
   ($potato, $chip)={$chip, $potato);
Can do a push or pop to an array (at high end).

When evaluated, a typeglob produces a scalar values that represents all the objects of that name, including any scalar, array, or hash varaiable, as well as any filehandle, format, or subroutine. When assigned to, a typeglob sets up its own name to be an alias for whatever typeglob value was assigned to it.

To pass more than one array or hash into or out of a function and maintin their integrity, use an explict pass by reference: @gunk=add_arys(\@a1, \@a2, \@3);


Construct Meaning
$days simple scalar value $days
$days[28] 29th element of array @days
$days{'Feb'} "Feb" value from hash %days
$#days Last index of array @days
$days->[28] 29th element of array pointed to by reference $days

Construct Meaning
@days same as ($days[0], $days[1], ..., $days[n])
@days[3, 4, 5] same as ($days[3], $days[4], $days[5])
@days[3..5] same as ($days[3], $days[4], $days[5])
@days{'Jan', 'Feb'} same as ($days{'Jan'}, $days{'Feb'})

Construct Meaning
%days (Jan => 31, Feb => $leap ? 29 : 28, Mar => ...)

Each of these 3 has their own namespace. Thus the following are all different:
   $foo @foo %foo
$foo[1] is part of @foo.