@john = (47, "brown", 186);

@mary = (23, "hazel", 128);

@bill = (35, "blue", 157);

*****

@vitals = ('john', 'mary', 'bill');

*****

$vitals = $vitals[0];

eval "\$${vitals}[1] = 'red'";

*****

local(*array) = $vitals[0];  # Alias *array to *john.

$array[1] = 'red';           # Actually sets $john[1].

*****

$scalarref = \$foo;

$constref  = \186_282.42;

$arrayref  = \@ARGV;

$hashref   = \%ENV;

$coderef   = \&handler;

$globref   = \*STDOUT;

*****

$arrayref = [1, 2, ['a', 'b', 'c']];

*****

@list = (\$a, \$b, \$c);  

@list = \($a, $b, $c);      # same thing!

*****

$hashref = {

    'Adam'  => 'Eve',

    'Clyde' => 'Bonnie',

};

*****

sub hashem {        { @_ } }   # silently WRONG

sub hashem {       +{ @_ } }   # ok

sub hashem { return { @_ } }   # ok

*****

$coderef = sub { print "Boink!\n" };

*****

$objref = new Doggie Tail => 'short', Ears => 'long';

# same as

$objref = Doggie->new(Tail => 'short', Ears => 'long');

*****

splutter(\*STDOUT);

sub splutter {

    my $fh = shift;

    print $fh "her um well a hmmm\n";

}



$rec = get_rec(\*STDIN);

sub get_rec {

    my $fh = shift;

    return scalar <$fh>;

}

*****

$foo         = "two humps";

$scalarref   = \$foo;

$camel_model = $$scalarref;  # $camel_model is now "two humps"

*****

$bar = $$scalarref;

push(@$arrayref, $filename);

$$arrayref[0] = "January";

$$hashref{"KEY"} = "VALUE";

&$coderef(1,2,3);

print $globref "output\n";

*****

$refrefref = \\\"howdy";

print $$$$refrefref;

*****

$bar = ${$scalarref};

push(@{$arrayref}, $filename);

${$arrayref}[0] = "January";

${$hashref}{"KEY"} = "VALUE";

&{$coderef}(1,2,3);

*****

&{ $dispatch{$index} }(1, 2, 3);

*****

$  $arrayref  [0] = "January";        #1

${ $arrayref }[0] = "January";        #2

   $arrayref->[0] = "January";        #3



$  $hashref  {KEY} = "F#major";       #1

${ $hashref }{KEY} = "F#major";       #2

   $hashref->{KEY} = "F#major";       #3

*****

print $array[3]->{"English"}->[0];

*****

$array[3]->{"English"}->[0] = "January";

*****

$array[3]{"English"}[0] = "January";

*****

$answer[$x][$y][$z] += 42;

*****

$listref->[2][2] = "hello";    # pretty clear

$$listref[2][2] = "hello";     # a bit confusing

*****

$listref[$i]->[$j] = "hello";

*****

REF

SCALAR

ARRAY

HASH

CODE

GLOB

*****

print "My sub returned @{[ mysub(1,2,3) ]} that time.\n";

*****

print "That yields @{[ $n + 5 ]} widgets\n";

*****

print "That yields ${ \($n + 5) } widgets.";

*****

sub newprint {

    my $x = shift;

    return sub { my $y = shift; print "$x, $y!\n"; };

}

$h = newprint("Howdy");

$g = newprint("Greetings");

*****

# Time passes...

*****

&$h("world");

&$g("earthlings");

*****

Howdy, world!

Greetings, earthlings!

*****

sub get_method_ref {

    my ($self, $method) = @_;

    return sub { return $self->$method(@_) };

}

$dog_wag = get_method_ref($dog, 'wag');

&$dog_wag("tail");  # Calls $dog->wag('tail').

*****

$name = "bam";

$$name = 1;                # Sets $bam

${$name} = 2;              # Sets $bam

${$name x 2} = 3;          # Sets $bambam

$name->[0] = 4;            # Sets $bam[0]

@$name = ();               # Clears @bam

&$name();                  # Calls &bam() (as in prior versions of Perl)

$pkg = "THAT";             #  (Don't use package or pack!)

${"${pkg}::$name"} = 5;    # Sets $THAT::bam without eval

*****

use strict 'refs';

*****

no strict 'refs';

*****

${bareword};    # same as $bareword

${"bareword"};  # also $bareword, but treated as symbolic reference

*****

local $value  = "10";

{

    my $value = "20";

    print ${"value"};

} 

*****

$push = "pop on ";

print "${push}over";

*****

print ${push} . 'over';

*****

print ${ push } . 'over';

*****

$hash{ "aaa" }{ "bbb" }{ "ccc" }

*****

$hash{ aaa }{ bbb }{ ccc }

*****

$hash{ shift }

*****

$hash{ shift() }

$hash{ +shift }

$hash{ shift @_ }

*****

$x{ \$a } = $a;

($key, $value) = each %x;

print $$key;   # WRONG

*****

$r = \@a;

$x{ $r } = $r;

*****

# assign to an array a list of list references

@LoL = ( 

       [ "fred", "barney" ],

       [ "george", "jane", "elroy" ],

       [ "homer", "marge", "bart" ],

);



print $LoL[2][2];   # prints "bart"

*****

# assign to a scalar variable a reference to a list of list references

$ref_to_LoL = [

    [ "fred", "barney", "pebbles", "bambam", "dino", ],

    [ "homer", "bart", "marge", "maggie", ],

    [ "george", "jane", "elroy", "judy", ],

];



print $ref_to_LoL->[2][2];   # prints "elroy"

*****

$LoL[2][2]

$ref_to_LoL->[2][2]

*****

$LoL[2]->[2]

$ref_to_LoL->[2]->[2]

*****

while (<>) {

    @tmp = split;

    push @LoL, [ @tmp ];

} 

*****

for $i ( 1 .. 10 ) {

    @tmp = somefunc($i);

    $LoL[$i] = [ @tmp ];

}

*****

while (<>) {

    push @LoL, [ split ];

} 

*****

for $i ( 1 .. 10 ) {

    $LoL[$i] = [ somefunc($i) ];

}

*****

my (@LoL, $i, $line);

for $i ( 0 .. 10 ) 

    $line = <>;

    $LoL[$i] = [ split ' ', $line ];

} 

*****

my (@LoL, $i);

for $i ( 0 .. 10 ) 

    $LoL[$i] = [ split ' ', <> ];

} 

*****

my (@LoL, $i);

for $i ( 0 .. 10 ) 

    $LoL[$i] = [ split ' ', scalar(<>) ];

} 

*****

my $ref_to_LoL;

while (<>) {

    push @$ref_to_LoL, [ split ];

} 

*****

for $x (1 .. 10) {

    for $y (1 .. 10) {

        $LoL[$x][$y] = func($x, $y);

    }

}



for $x ( 3, 7, 9 ) {

    $LoL[$x][20] += func2($x);

} 

*****

# add new columns to an existing row

push @{ $LoL[0] }, "wilma", "betty";

*****

push $LoL[0], "wilma", "betty";  # WRONG!

*****

print $LoL[0][0];

*****

print @LoL;         # WRONG

*****

for $array_ref ( @LoL ) {

    print "\t [ @$array_ref ],\n";

}

*****

for $i ( 0 .. $#LoL ) {

    print "\t element $i is [ @{$LoL[$i]} ],\n";

}

*****

for $i ( 0 .. $#LoL ) {

    for $j ( 0 .. $#{$LoL[$i]} ) {

        print "element $i $j is $LoL[$i][$j]\n";

    }

}

*****

for $i ( 0 .. $#LoL ) {

    $aref = $LoL[$i];

    for $j ( 0 .. $#{$aref} ) {

        print "element $i $j is $aref->[$j]\n";

    }

}

*****

for $i ( 0 .. $#LoL ) {

    $aref = $LoL[$i];

    $n = @$aref - 1;

    for $j ( 0 .. $n ) {

        print "element $i $j is $aref->[$j]\n";

    }

}

*****

@part = ();

$x = 4;     

for ($y = 7; $y < 13; $y++) {

    push @part, $LoL[$x][$y];

} 

*****

@part = @{ $LoL[4] } [ 7..12 ];

*****

@newLoL = ();

for ($startx = $x = 4; $x <= 8; $x++) {

    for ($starty = $y = 7; $y <= 12; $y++) {

        $newLoL[$x - $startx][$y - $starty] = $LoL[$x][$y];

    }

} 

*****

for ($x = 4; $x <= 8; $x++) {

    push @newLoL, [ @{ $LoL[$x] } [ 7..12 ] ];

}

*****

@LoL = ( [2, 3], [4, 5, 7], [0] );

print "@LoL";

*****

ARRAY(0x83c38) ARRAY(0x8b194) ARRAY(0x8b1d0)

*****

print $LoL[1][2];

*****

my $listref = [

    [ "fred", "barney", "pebbles", "bambam", "dino", ],

    [ "homer", "bart", "marge", "maggie", ],

    [ "george", "jane", "elroy", "judy", ],

];



print $listref[2][2];   # WRONG!

*****

print $listref->[2][2];

*****

use strict 'vars'; # or just use strict

*****

for $i (1..10) {

    @list = somefunc($i);

    $LoL[$i] = @list;       # WRONG!

} 

*****

for $i (1..10) {

    @list = somefunc($i);

    $LoL[$i] = \@list;      # WRONG!

} 

*****

for $i (1..10) {

    @list = somefunc($i);

    $LoL[$i] = [ @list ];

} 

*****

for $i (1..10) {

    @list = somefunc($i);

    @{$LoL[$i]} = @list;

} 

*****

$LoL[3] = \@original_list;

*****

@{$LoL[3]} = @list;

*****

for $i (1..10) {

    my @list = somefunc($i);

    $LoL[$i] = \@list;

} 

*****

for $i (1..10) {

    $LoL[$i] = [ somefunc($i) ];

} 

*****

$LoL[$i] = [ @list ];   # safest, sometimes fastest

$LoL[$i] = \@list;      # fast but risky, depends on my-ness of list

@{ $LoL[$i] } = @list;  # too tricky for most uses

*****

