Arrays of scalars (also called lists) are
sequentially-arranged scalars: ('Sunday', 'Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday', 'Saturday')
(13,14,15,16,17,18,19) equivalent to (13..19)
(13,14,15,16,17,18,19)[2..4] equivalent to (15,16,17)
@whole_list
Associative arrays (also called hashes)
help you remember things: $DaysInMonth{'January'} = 31; $enrolled{'Joe College'} = 1;
$StudentName{654321} = 'Joe College';
$score{$studentno,$examno} = 89;
%whole_hash
Perl 5 allows combinations of these, such as lists of lists and associative
arrays of lists.
Name Conventions
Scalar variables start with '$', even when referring to an array element. The
variable name reference for a whole list starts with '@', and the variable name
reference for a whole associative array starts with '%'.
Lists are indexed with square brackets enclosing a number, normally starting
with [0]. In Perl 5, negative subscripts count from the end. Thus,
$things[5]
is the 6th element of array @things
, and
('Sun','Mon','Tue','Wed','Thu','Fri','Sat')[1]
equals
'Mon'
.
Associative arrays are indexed with curly brackets enclosing a string.
$whatever, @whatever, and %whatever are three different variables.
@days = (31,28,31,30,31,30,31,31,30,31,30,31);
# A list with 12 elements.
$#days # Last index of @days; 11 for above list
$#days = 7; # shortens or lengthens list @days to 8 elements
@days # ($days[0], $days[1],... )
@days[3,4,5] # = (30,31,30)
@days{'a','c'} # same as ($days{'a'},$days{'c'})
%days # (key1, value1, key2, value2, ...)
Case is significant--"$FOO", "$Foo" and "$foo" are all different
variables. If a letter or underscore is the first character after the $, @, or
%, the rest of the name may also contain digits and underscores. If this
character is a digit, the rest must be digits. Perl has several dozen special
variables whose second character is non-alphanumeric. For example, $/ is the
input record separator, newline "\n" by default. An uninitialized variable has a
special "undefined" value which can be detected by the function defined().
Undefined values convert depending on context to 0, null, or false.
The variable "$_" Perl presumes when needed variables are
not specified. Thus:
<STDIN>; assigns a record from filehandle STDIN to $_
print; prints the curent value of $_
chop; removes the last character from $_
@things = split; parses $_ into white-space delimited
words, which become successive
elements of list @things.
$_, $1, $2, $3,
and other implicit variables contribute to Perl Paradox Number
Two: What you don't see can help you or hurt you. See
Quick Reference Guide Section 25, Special Variables.
Subroutines and functions are referenced
with an initial '&', which is optional if reference is obviously a
subroutine or function such as following the sub, do, and sort directives:
sub square { return $_[0] ** 2; }
print "5 squared is ", &square(5);
Filehandles don't start with a special character, and so as
to not conflict with reserved words are most reliably specified as uppercase
names: INPUT, OUTPUT, STDIN, STDOUT, STDERR, etc.