Chapter 9 Data Structures
Arrays of Arrays
# Assign a list of array references to an array.
my @AoA = (
["fred", "barney" ],
["george", "jane", "elroy" ],
["homer", "marge", "bart" ],
);
print $AoA[2][1]; # prints "marge"
print $AoA[2]–>[1] # same command of previous one
# Create a reference to an array of array references.
my $ref_to_AoA = [
[ "fred", "barney", "pebbles", "bamm bamm", "dino", ],
[ "homer", "bart", "marge", "maggie", ],
[ "george", "jane", "elroy", "judy", ],
];
print $ref_to_AoA–>[2][3]; # prints "judy"
print $ref_to_AoA->[2]->[3]; #same command of previous one
A CPAN module that we like to use for displaying our data dumps is Data::Dump.
use v5.14;
use Data::Dump qw(dump);
# for push on scalars
# CPAN module
my @AoA = (
[ "fred", "barney" ],
[ "george", "jane", "elroy" ],
[ "homer", "marge", "bart" ],
);
push $AoA[0], "wilma", "betty";
dump \@AoA;
When constructing an array of arrays, remember to compose new references for
the subarrays. Otherwise, you will just create an array containing the element
counts of the subarrays.
$AoA[$i] = @array; # Wrong!!
$AoA[$i] = [ @array ]; # Safest, sometimes fastest
$AoA[$i] = \@array; # Fast but risky, depends on my–ness of array
@{ $AoA[$i] } = @array; # A bit tricky
Chapter 10 Packages
The contents of a package are collectively called a symbol table. Symbol tables are stored in a hash whose name is the same as the package, but with two colons appended. The symbol table for the Red::Blue package is named %Red::Blue::.Inside a symbol table’s hash, each key/value pair matches a variable name to its value. The keys are the symbol identifiers, and the values are the corresponding typeglobs. The following have (nearly) the same effect
*sym = *main::variable;
*sym = $main::{"variable"};
Print out the package hash table.
foreach $symname (sort keys %main::) {
local *sym = $main::{$symname};
print "\$$symname is defined\n" if defined $sym;
print "\@$symname is nonnull\n" if @sym;
print "\%$symname is nonnull\n" if %sym;
}
*PI = \3.14159265358979;
use constant PI => 3.14159;
*PI = sub () { 3.14159 };
The following are all equivalent to one another, though the first two compute the symbol table entry at compile time, while the last two do so at run time.
*sym = *oldvar;
*sym = \*oldvar; # autodereference
*sym = *{"oldvar"}; # explicit symbol table lookup
*sym = "oldvar"; # implicit symbol table lookup
If you think about it sideways, the typeglob itself can be viewed as a kind of hash,
with entries for the different variable types in it. In this case, the keys are fixed,
since a typeglob can contain exactly one scalar, one array, one hash, and so on.
*pkg::sym{SCALAR} # same as \$pkg::sym
*pkg::sym{ARRAY} # same as \@pkg::sym
*pkg::sym{HASH} # same as \%pkg::sym
*pkg::sym{CODE} # same as \&pkg::sym
*pkg::sym{GLOB} # same as \*pkg:;sym
*pkg::sym{IO} # internal file/dir handle, no direct equivalent
*pkg::sym{NAME} # "sym" (not a reference)
*pkg::sym{PACKAGE} # "pkg" (not a reference)
sub identify_typeglob {
my $glob = shift;
print "You gave me ", *{$glob}{PACKAGE}, "::", *{$glob}{NAME}, "\n";
}
identify_typeglob(*identify_typeglob); # print out "You gave me main::identify_typeglob"
The special symbol __PACKAGE__ contains the current package name
Autoloading
Skip First
Chapter 11 Modules
The opposite of use is no. The syntax :no MODULE;
no MODULE LIST;
no MODULE VERSION;
no MODULE VERSION LIST;
沒有留言:
張貼留言