Chapter 2 Using Modules
use lib '/Users/gilligan/lib';
The use lib indeed runs at compile time, so this also doesn’t work
my $LIB_DIR = '/Users/gilligan/lib'; ... use lib $LIB_DIR; # BROKEN use Navigation::SeatOfPants;
Fix it with "use constant"
use constant LIB_DIR => '/Users/gilligan/lib'; ... use lib LIB_DIR; use Navigation::SeatOfPants;
Setting the Path Outside the program
solution 1: using global variable
export PERL5LIB=/home/skipper/perl−lib
solution 2: assign include path on the command line
% perl −I/home/skipper/perl−lib /home/skipper/bin/get_us_home
Chapter 3 Intermediate Foundations
List Operation
my @input_numbers = (1, 2, 4, 8, 16, 32, 64); my @result = map $_ + 100, @input_numbers; # output 101, 102, 104, ... my @result = map { $_, 3 * $_ } @input_numbers; # output 1, 3, 2, 6, 4, 12, ... my %hash = map { $_, 3 * $_ } @input_numbers; # output hash { 1=>3, 2=> 6, ...}
Dereferencing the Array Reference
@ skipper # refer to the array @{ $ref_skipper } # refer to the array by reference @$ref_skipper # getting braces off if it is a simple scalar variable
Checking Reference Types
use Carp qw(croak); sub show_hash { my $hash_ref = shift; my $ref_type = ref $hash_ref; croak "I expected a hash reference!" unless $ref_type eq 'HASH'; croak "I expected a hash reference!" unless $ref_type eq ref {}; # another way foreach my $key ( %$hash_ref ) { ... } } # another way to accepts all objects that support "keys" function, not only hash object use Scalar::Util qw(reftype); sub show_hash2 { my $hash_ref = shift; my $ref_type = reftype $hash_ref; # works with objects croak "I expected a hash reference!" unless eval { keys %$ref_type; 1 }; foreach my $key ( %$hash_ref ) { ... } }
Chapter 5 References and Scoping
Nothing noted.
沒有留言:
張貼留言