1>: Accessing Elements of an Array$fred[0] = "yabba";
$fred[1] = "dabba";
$fred[2] = "doo";
print $fred[0];
$fred[2] = "diddley";
$fred[1] .= "whatsis";
$number = 2.71828;
print $fred[$number − 1]; # Same as printing $fred[1]
2>: Special Array Indices
$rocks[0] = 'bedrock';
$rocks[1] = 'slate';
$rocks[2] = 'lava';
$rocks[3] = 'crushed rock';
$rocks[99] = 'schist'; # now there are 95 undef elements
$end = $#rocks; # 99, which is the last element's index
$rocks[ $#rocks ] = 'hard rock'; # the last rock
$rocks[ -1 ] = 'hard rock'; #easier way to do that last example
$dead_rock = $rocks[-100]; # gets 'bedrock'
$rocks[-200] = 'crystal'; # fatal error!
3>: List Literals
(1, 2, 3) # list of three value 1, 2, and 3
() # empty list - zero elements
(1..100) # list of 100 integers
(1.7..5.7) # same as (1, 2, 3, 4, 5)
qw( fred barney betty wilma dino) # same as ("fred", "barney", "betty", "wilma", "dino")
Perl actually lets you choose any punctuation character as the delimiter..
qw! fred barney betty wilma dino !
qw/ fred barney betty wilma dino /
qw# fred barney betty wilma dino # # like in a comment!
qw( fred barney betty wilma dino )
qw{ fred barney betty wilma dino }
qw[ fred barney betty wilma dino ]
qw< fred barney betty wilma dino >
4>: List Assignment
($fred, $barney, $dino) = ("flintstone", "rubble", undef);
($fred, $barney) = qw< flintstone rubble slate granite >; # two ignored items
($wilma, $dino) = qw[flintstone]; # $dino gets undef
($rocks[0], $rocks[1], $rocks[2], $rocks[3]) = qw/talc mica feldspar quartz/;
or (to refer to an entire array)
@rocks = qw/talc mica feldspar quartz/; # same as above
empty scalars start out with undef,
empty arrays start out with the empty list.
@copy = @quarry; # copy a list from one array to another
5>: The pop and push Operators
@array = 5..9;
$fred = pop(@array); # $fred gets 9, @array now has (5, 6, 7, 8)
$barney = pop @array; # $barney gets 8, @array now has (5, 6, 7)
pop @array; # @array now has (5, 6). (The 7 is discarded.)
(If the array is empty, pop will leave it alone (since there is no element to remove), and
it will return undef.)
push(@array, 0); # @array now has (5, 6, 0)
push @array, 8; # @array now has (5, 6, 0, 8)
push @array, 1..10; # @array now has those ten new elements
@others = qw/ 9 0 2 1 0 /;
push @array, @others; # @array now has those five new elements (19 total)
6>: The shift and unshift Operators
@array = qw# dino fred barney #;
$m = shift(@array); # $m gets "dino", @array now has ("fred", "barney")
$n = shift @array; # $n gets "fred", @array now has ("barney")
shift @array; # @array is now empty
$o = shift @array; # $o gets undef, @array is still empty
unshift(@array, 5); # @array now has the one-element list (5)
unshift @array, 4; # @array now has (4, 5)
@others = 1..3;
unshift @array, @others; # @array now has (1, 2, 3, 4, 5)
7>: Interpolating Arrays into Strings
$email = "fred\@bedrock.edu"; # Correct
$email = 'fred@bedrock.edu'; # Another way to do that
@fred = qw(hello dolly);
$y = "2*4";
$x = "This is $fred[1]'s place"; # "This is dolly's place"
$x = "This is $fred[$y−1]'s place"; # Argument "2*4" isn't numeric in subtraction(-)..
@fred = qw(eating rocks is wrong);
$fred = "right"; # we are trying to say "this is right[3]"
print "this is $fred[3]\n"; # prints "wrong" using $fred[3]
print "this is ${fred}[3]\n"; # prints "right" (protected by braces)
print "this is $fred"."[3]\n"; # right again (different string)
print "this is $fred\[3]\n"; # right again (backslash hides it)
8>: The foreach Control Structure
@rocks = qw/ bedrock slate lava /;
foreach $rock (@rocks) {
$rock ="\t$rock\n";
}
print "The rocks are:\n", @rocks; # Each one is indented, on its own line
Perl’s Favorite Default: $_
foreach (1..10) { # Uses $_ by default
print "I can count to $_!\n";
}
The reverse Operator
@fred = 6..10;
@barney = reverse(@fred); # gets 10, 9, 8, 7, 6
@wilma = reverse 6..10; # gets the same thing, without the other array
@fred = reverse @fred; # puts the result back into the original array
reverse @fred; # WRONG - doesn't change @fred
@fred = reverse @fred; # that's better
The sort Operator
@rocks = qw/ bedrock slate rubble granite /;
@sorted = sort(@rocks); # gets bedrock, granite, rubble, slate
@back = reverse sort @rocks; # these go from slate to bedrock
@rocks = sort @rocks; # puts sorted result back into @rocks
@numbers = sort 97..102; # gets 100, 101, 102, 97, 98, 99
sort @rocks; # WRONG, doesn't modify @rocks
@rocks = sort @rocks; # Now the rock collection is in order
9>: Scalar and List Context
42 + something # The something must be a scalar
sort something # The something must be a list
@people = qw( fred barney betty );
@sorted = sort @people; # list context: barney, betty, fred
$number = 42 + @people; # scalar context: 42 + 3 gives 45
@list = @people; # a list of three people
$n = @people; # the number 3
@backwards = reverse qw/ yabba dabba doo /; # gives doo, dabba, yabba
$backwards = reverse qw/ yabba dabba doo /; # gives oodabbadabbay
$fred = something; # scalar context
@pebbles = something; # list context
($wilma, $betty) = something; # list context
($dino) = something; # still list context! (one-element list)
First, some that provide scalar context to something:
$fred = something;
$fred[3] = something;
123 + something
something + 654
if (something) { ... }
while (something) { ... }
$fred[something] = something;
And here are some that provide a list context:
@fred = something;
($fred, $barney) = something;
($fred) = something;
push @fred, something;
foreach $fred (something) { ... }
sort something
reverse something
print something
@fred = 6 * 7; # gets the one-element list (42)
@barney = "hello" . ' ' . "world";
@wilma = undef; # OOPS! Gets the one-element list (undef)
# which is not the same as this:
@betty = ( ); # A correct way to empty an array
(Since undef is a scalar value, assigning undef to an array doesn’t clear the array. The
better way to do that is to assign an empty list.)
Forcing Scalar Context
@rocks = qw( talc quartz jade obsidian );
print "How many rocks do you have?\n";
print "I have ", @rocks, " rocks!\n"; # WRONG, prints names of rocks
print "I have ", scalar @rocks, " rocks!\n"; # Correct, gives a number
(On occasion, you may need to force scalar context where Perl is expecting a list. In that
case, you can use the fake function scalar)
10> : in List Context
@lines = ; # read standard input in list context
chomp(@lines = ); # read the lines, not the newlines