lecture
in color
@a = (@b,@c); concatenates @b, @c into @a.
%h = (%k,%m); concatenates hashes %k and %m into
a single hash %h. %m overrides mappings that
conflict in %k.
%k = ('clinton'=>'silly', 'mondale'=> 'serious');
%m = ('clinton'=>'happy', 'dole'=>'sneezy');
(%k,%m) = ('mondale'=>'serious', 'clinton'=>'happy', 'dole'=>'sneezy');
$r = \@a; : a reference to array @a.
@a can be referred to as @$r.
$s = \%k; : a reference to hash %k.
%k can be referred to as %$s.
$t = \$x; : a reference to a scalar $x
$x can be referred to as $$t
sub foo { return 2; } $f = \&foo; : a reference to a function foo.
&$f() calls foo.
$g = sub { return 1; } : a reference to an anonymous function $g.
&{$g}() : a call to that function.
[1,2,3] is a reference to array.
$a=[1,2,3] is the same as saying
@a = (1,2,3); $a = \@a;
$a, e.g., is ${$a}[1], which is 2.
${$a}[1] means $a->[1].
\('a'=>1, 'b'=>2) (a reference to a hash)
can be written { 'a'=>1, 'b'=>2 }
$h = { 'a'=>1, 'b'=>2 };
${$h}{'a'}, which is 1
${$h}{'a'} means $h->{'a'}
| Array | Ref to array | Hash | Ref to hash |
|---|---|---|---|
@a = (1,2,3); |
$a = [1,2,3]; |
%h = ('a'=>1,'b'=>2); |
$h = {'a'=>1,'b'=>2}; |
$a[1] |
${$a}[1] or $a->[1] |
$h{'a'} |
${$h}{'a'} or $h->{'a'} |
$a[5]=7; |
$a->[5]=7; |
$h{'c'}=9; |
$h->{'c'}=9; |
$#a |
$#{$a} |
keys %h |
keys %$h |
@a |
@$a |
%h |
%$h |
$s = { 'name' => 'Bill Clinton',
'email' => 'president@whitehouse.gov',
'title' => 'President of the United States'
};
print "$s->{'name'} has email $s->{'email'}\n";
$a = [1,2,3]; makes $a a reference to an
array. Elements are $a->[0], $a->[1], $a->[2].
$h = { 'Bill'=>1, 'Ken'=>2 }; makes $h a
reference to a hash. Elements are $h->{'Bill'} and $h->{'Ken'}.
# $a->[0] $a->[1] $a->[2] $a = [[1,2,3],[4,5,6],[7,8,9]]; # ^$a->[1]->[2] # ^$a->[1]->[1] # ^$a->[1]->[0]
$a->[0]->[1]
as $a->[0][1].
$r = {
'instructor' => 'Bill Clinton',
'course' => 'Abuse of Power',
'tas' => ['agore', 'rreagan'],
'students => {
'kstar'=> {
'name'=>'Ken Star',
'phone'=>'5551212',
'homepage'=>'http://www.whitehouse.gov/~kstar',
},
'astudent' => {
'name'=>'A. Student',
'phone'=>'5551212',
'homepage'=>'http://www.eecs.tufts.edu/~astudent',
},
},
};
After this:
$r->{'instructor'} is 'Bill Clinton'.
$r->{'tas'} is ['agore','rreagan'].
$r->{'tas'}->[1] is 'rreagan'.
$r->{'students'} is
{
'kstar'=> {
'name'=>'Ken Star',
'phone'=>'5551212',
'homepage'=>'http://www.whitehouse.gov/~kstar',
},
'astudent' => {
'name'=>'A. Student',
'phone'=>'5551212',
'homepage'=>'http://www.eecs.tufts.edu/~astudent',
},
}
$r->{'students'}->{'astudent'} is
{
'name'=>'A. Student',
'phone'=>'5551212',
'homepage'=>'http://www.eecs.tufts.edu/~astudent',
},
$r->{'students'}->{'astudent'}->{'name'} is 'A. Student'.
$s->{'tas'}->[1]
can be written $s->{'tas'}[1].
$a[5][7] "works" in Perl 5.6 and above.
$a[5]->[7], which is more correct to use,
because it reminds you that the elements of the outer array are references
to the inner arrays.
int a[10][10];and we set
int *b; b=a[5];then
b[7] is identical with a[5][7]
anywhere it appears.
Further, one can replace a[5]
with b in any statement (or vice versa) and the
statement means the same thing as before. "If b means
a[5], then b can be substituted
anywhere a[5] could be written."
$a[5]->[7] = 'hi';if we set
$b = $a[5];then the substitution principle is obeyed. It is always true that
$b->[$i] is $a[5]->[$i]anytime we use the arrow notation for scalar reference dereferencing (because both
$b and $a[5] are scalars and
-> applies only to scalars).
$a[5][7],
it is absolutely never true that:
$b[7] is $a[5][7](because
$b[7] refers to element 7 of the array
@b, not element 7 of
the array referenced by the scalar $b).
$b[7] and $a[5][7] are references
to different namespaces.
if ($a->{'cred'}->[0]->{'auth_type'} eq 'DES') {
shift @{$a->{'cred'}};
} elsif ($a->{'cred'}->[0]->{'auth_type'} eq 'LOCAL') {
$a->{'cred'}->[0]->{'auth_name'} = 'couch.eecs.tufts.edu.';
}
This fragment is taken from my own code for the new account system.
Note that the substitution principle applies to this very complex
example, even if you don't know what it means. The following code has
exactly the same effect:
my $b = $a->{'cred'}->[0]; # first cred entry in cred table
if ($b->{'auth_type'} eq 'DES') {
shift @{$a->{'cred'}}; # dump DES credential on floor
} elsif ($b->{'auth_type'} eq 'LOCAL') {
$b->{'auth_name'} = 'couch.eecs.tufts.edu.';
}
$s = [[1,2,3],[4,5,6],[7,8,9]];
$s->[0] is [1,2,3],
$s->[0]->[0] is ${[1,2,3]}->[0] is 1
$s->[2]->[1] is ${[7,8,9]}->[1] is 8
$h = {'harry' => {'lazy'=>1, 'silly'=>1},
'george' => {'industrious'=>1, 'friendly'=>1 }};
$h->{'harry'} is
{'lazy'=>1, 'silly'=>1};
$h->{'harry'}->{'lazy'}
is ${{'lazy'=>1, 'silly'=>1}}->{'lazy'}, which
is 1;
$h->{'harry'}->{'friendly'}
is ${{'lazy'=>1, 'silly'=>1}}->{'friendly'},
which is undef.
Data::DumperData::Dumper
use Data::Dumper; print Dumper($reference);where
$reference is any reference.
Examples:
print Dumper(\(1,2,3)); print Dumper(\%thing);
Dumpercontents of perl_refs/e01.cgi...
#! /var/local/couch/bin/perl
use Data::Dumper;
print "Content-type: text/plain\n\n";
# illustrating structures
$r = {
'name' => 'Al Franken',
'peeves' => ['newsmen', 'olympic commentators'],
'hobbies' => {
'biking' => {
'type'=>'tandem',
'activity'=>'long-distance touring',
},
},
};
print "\$r->{'name'} = " . Dumper($r->{'name'});
print "\$r->{'hobbies'} = " . Dumper($r->{'hobbies'});
print "\$r->{'hobbies'}->{'biking'} = " . Dumper($r->{'hobbies'}->{'biking'});
...end of perl_refs/e01.cgi
use Data::Dumper;defines the routine
Dumper(reference) that'll
produce a printable version of any structure.
print Dumper($thing);prints a "pretty" representation of the structure of
$thing.
contents of perl_refs/e02.cgi...
#! /var/local/couch/bin/perl
use Data::Dumper;
print "Content-type: text/plain\n\n";
# building reference variables.
# 1) if you reference a substructure of a hash,
# and it doesn't exist, and there's no inherent
# conflict that would keep it from existing,
# then it's CREATED ON THE FLY.
$r = {};
$r->{'Clinton'} = 'Bill';
print Dumper($r);
$s = {
'name' => 'Al Franken',
'peeves' => ['newsmen', 'olympic commentators'],
'hobbies' => {
'biking' => {
'type'=>'tandem',
'activity'=>'long-distance touring',
},
},
};
print Dumper($s);
$t->{'name'} = 'Bill Clinton';
$t->{'hobbies'}->{'biking'}->{'type'} = "recumbent";
print Dumper($t);
...end of perl_refs/e02.cgi
contents of perl_refs/e03.cgi...
#! /var/local/couch/bin/perl
use Data::Dumper;
print "Content-type: text/plain\n\n";
# hash common practices
# 1. using a hash like an array for quick tests of membership in sets
$a = [2,5,6];
$h = {2=>1,5=>1,6=>1};
if ($h->{2}) {
print "2 is a member\n";
} else {
print "2 is not a member\n";
}
if ($h->{4}) {
print "4 is a member\n";
} else {
print "4 is not a member\n";
}
$h = {'weird'=>1,'strange'=>1,'silly'=>1};
$a = [keys %$h];
if ($h->{'weird'}) {
print "'weird' is a member\n";
} else {
print "'weird' is not a member\n";
}
if ($h->{'amanda'}) {
print "'amanda' is a member\n";
} else {
print "'amanda' is not a member\n";
}
...end of perl_refs/e03.cgi
undef
depending upon whether the corresponding key is in the set or not.
contents of perl_refs/e04.pl...
#! /var/local/couch/bin/perl
@universe = ('alva', 'frank', 'john', 'george',
'sue', 'judy', 'diane', 'david');
@set = ('alva', 'frank', 'george', 'sue', 'judy', 'diane');
# very slow membership function.
sub member1 {
my $member = shift;
my @set = @_;
foreach $m (@set) { return 1 if $member eq $m; }
return undef;
}
print "SET MEMBERS:\n";
foreach $u (@universe) {
if (&member1($u,@set)) {
print "$u is in s\n";
} else {
print "$u is not in s\n";
}
}
# convert to hash representation
$set = {};
foreach $m (@set) { $set->{$m}++; }
# very fast membership function.
sub member2 {
my $elem = shift;
my $hash = shift;
return $hash->{$elem};
}
print "SET MEMBERS:\n";
foreach $u (@universe) {
if (&member2($u,$set)) {
print "$u is in s\n";
} else {
print "$u is not in s\n";
}
}
@set2 = sort keys %$set; # get keys back into an array.
...end of perl_refs/e04.pl
This produces
contents of perl_refs/e04.pl.out... SET MEMBERS: alva is in s frank is in s john is not in s george is in s sue is in s judy is in s diane is in s david is not in s SET MEMBERS: alva is not in s frank is not in s john is not in s george is not in s sue is not in s judy is not in s diane is not in s david is not in s ...end of perl_refs/e04.pl.out
lecture
in color