记录一点小经验吧:
关于 Math::BigInt
use Math::BigInt;
# 1连乘至100,这样貌似可行
my $value = Math::BigInt->new(1);
foreach (1 .. 100) {
$value->bmul($_);
}
print $value->bstr(), "\n";
chapter 3
# 习题 1
use Cwd;
use File::Spec;
my $cwd = getcwd;
print map {" " . File::Spec->catfile( $cwd, $_ ) . "\n" } glob( ".* *" );
# 关于“习题2”,话说装模块还是软件中心给力 -> 有关条形码:0596102062
open OUT, ">bar" or die "Can't write: $!"; # 输出到某个文件
use Business::ISBN;
use GD::Barcode::EAN13;
my $isbn = Business::ISBN->new( $ARGV[0] );
print "ISBN is ", $isbn->as_string . "\n";
# 貌似 $isbn->country_code 不存在
print "Group code: " . $isbn->group_code . "\n";
print "Publisher code: " . $isbn->publisher_code . "\n";
print "Check to see if the ISBN is valid: " . $isbn->is_valid . "\n";
# creat an EAN13 barcode in PNG format
binmode( OUT ); # 非文本,输出文件时貌似此句不需要
print "Content-Type: image/png\n\n";
print OUT $isbn->png_barcode; # 可以,成功得到 png
#print OUT GD::Barcode::EAN13->new('123456789012')->plot->png; # 这是其 perldoc 里的示例
chapter 4
# 习题 2
my @gilligan = qw(red_shirt hat lucky_socks water_bottle);
my @professor = qw(sunscreen water_bottle slide_rule batteries radio);
my @skipper = qw(blue_shirt hat jacket preserver sunscreen);
my %all = (
"Gilligan" => \@gilligan,
"Skipper" => \@skipper,
"Professor" => \@professor,
);
check_items_for_all(\%all);
# 子程序如下:
sub check_items_for_all {
my $all = shift;
for my $person (sort keys %$all) {
check_required_items($person, $all->{$person});
}
}
sub check_required_items {
my $who = shift;
my $items = shift;
my @required = qw(preserver sunscreen water_bottle jacket);
my @missing = ();
for my $item (@required) {
unless (grep $item eq $_, @$items) { # not found in list?
print "$who is missing $item.\n";
push @missing, $item;
}
}
if (@missing) {
print "Adding @missing to @$items for $who.\n";
push @$items, @missing;
}
}