Intermediate Perl 第一章 学習メモ

タイトルのとおりです。最近やっと重い腰を上げて読み始めました。   週1章くらいのペースで読んでいければいいかなと思います。

Intermediate Perl

Intermediate Perl

メモ

  • use lib '<path>'; でモジュールをロードするパスを設定できる
  • ローカルで実行する場合はlocal::libを使うと良い
  • cpanm便利

Excersise

1

#!/usr/bin/env perl
use 5.10.0;
use strict;
use warnings;

use File::Spec;
use Cwd;

my $dir = getcwd;
my @files = glob "*";
foreach my $file (@files){
    my $fullPath = File::Spec->catpath( undef, $dir, $file );
    print "    $fullPath\n";
}

実行結果

$ ./intermed_c01_problem01.pl
   /home/fakeyakuza/dev/study.perl/intermed_c01_001.pl
   /home/fakeyakuza/dev/study.perl/intermed_c01_002.pl
   /home/fakeyakuza/dev/study.perl/intermed_c01_003.pl
   /home/fakeyakuza/dev/study.perl/intermed_c01_004.pl
   /home/fakeyakuza/dev/study.perl/intermed_c01_problem01.pl
   /home/fakeyakuza/dev/study.perl/intermed_c01_problem02.pl
   /home/fakeyakuza/dev/study.perl/intermed_c01_problem02_2.pl

2

Module::CoreListの使い方がよくわからず答えを写経。

実行結果

$ ./intermed_c01_problem02_2.pl |more
                                AnyDBM_File 5
                                  App::Cpan 5.011003
                                 App::Prove 5.010001
                          App::Prove::State 5.010001
                  App::Prove::State::Result 5.010001
            App::Prove::State::Result::Test 5.010001
                           Archive::Extract 5.009005
                               Archive::Tar 5.009003
                     Archive::Tar::Constant 5.009003
                         Archive::Tar::File 5.009003
                        Attribute::Handlers 5.007003
                                 AutoLoader 5
                                  AutoSplit 5
                                          B 5.005
                                 B::Concise 5.006001
                                   B::Debug 5.005
                                 B::Deparse 5.005
                                    B::Lint 5.005
                                    (以下略)

3

モジュールのインストール

$ cpanm local::lib
    $ cpanm --local-lib=~/perl5 local::lib && eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib)
$ cpanm -f usiness::ISBN (テストが通らなかったので渋々-fをつけた)

コード

#!/usr/bin/env perl
use 5.10.0;
use strict;
use warnings;

use local::lib;
use Business::ISBN;

my $isbn = Business::ISBN->new(9781449393090);

print $isbn->group_code,"\n";
print $isbn->publisher_code,"\n";

実行結果

$ ./intermed_c01_problem03.pl
1
4493

雑感

root権限なくてもlocal::lib + cpanm + perlbrewで自分仕様のperl開発環境が構築できるのは便利です。