學習 Intermediate Perl

記錄一點小經驗吧: less

關於 Math::BigInt ide

use Math::BigInt;
# 1連乘至100,這樣貌似可行
my $value = Math::BigInt->new(1);
foreach (1 .. 100) {
	$value->bmul($_);
}
print $value->bstr(), "\n";

chapter 3 ui

# 習題 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 code

# 習題 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;
  }
}
相關文章
相關標籤/搜索