hollyさんのwiki

長らく離れていたが最近やるようになったので、ちょっと整理をすることにする

perlbrew

http://www.perlbrew.pl/ pythonbrewがあるようにperlにもある。こっちが本家かも知れないけど

インストール

curl -kL http://install.perlbrew.pl | bash

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
102  1022  102  1022    0     0    504      0  0:00:02  0:00:02 --:--:--  5084

## Download the latest perlbrew

## Installing perlbrew
The perlbrew is installed as:

    ~/perl5/perlbrew/bin/perlbrew

You may trash the downloaded /tmp/perlbrew-1415 from now on.

Perlbrew environment initiated under ~/perl5/perlbrew

Append the following piece of code to the end of your ~/.bash_profile and start a
new shell, perlbrew should be up and fully functional from there:

    source ~/perl5/perlbrew/etc/bashrc

For further instructions, simply run `perlbrew` to see the help message.

Happy brewing!

## Installing patchperl

## Done.

設定

if [ -f "$HOME/perl5/perlbrew/etc/bashrc" ]; then
        . "$HOME/perl5/perlbrew/etc/bashrc"
fi

使い方

とりあえずこれをはっつけておけばなんとか使えるはず
        perlbrew <command> [options] [arguments]

           Commonly used commands:
               available      インストール可能なperlバージョン一覧
               init           Initialize perlbrew environment.
               install        Install perl
               list           List installed perls
               use            Use the specified perl in current shell
               switch         Permanently use the specified perl as default
               mirror         Pick a preffered mirror site
               off            Permanently turn off perlbrew
               version        Display version
               help           Read more detail instructions
インストール
あとはlistとswitchさえ覚えておけばとりあえずはどうにかなるだろう
# thread使いたいのなら。使えなくてもいいんならこのオプションはいらない
# test不要な場合は --notest をつければよい
perlbrew install --notest perl-5.12.3  -Duseithreads

cpanm

便利なのでインストールしておく。$HOME/perl5/perlbrew/bin/cpanm にインストールされる。local::libもついてくる模様
perlbrew install-cpanm
http://mt.endeworks.jp/d-6/2010/08/perlbrew.html とかわかりやすい

App::pmuninstall

モジュールをアンインストールする場合
cat ./perl5/perlbrew/perls/perl-5.12.3/lib/site_perl/5.12.3/x86_64-linux-thread-multi/auto/Mojolicious/.packlist | xargs rm
のように.packlistに記述されているファイルを削除でもいいけど、ツールを使いところなのでインストールしておく
cpanm App::pmuninstall
普通は
# -f 問い合わせなし
# -v verbose
pm-uninstall Mojolicious
のように使うとよい

詳細はhttp://search.cpan.org/~xaicron/App-pmuninstall/li...参照

local::lib

pythonでいうvirtualenvのような感じ?
cpamn local::lib
使い方は
perl -Mlocal::lib
とすれば
export PERL_LOCAL_LIB_ROOT="/home/kurt/perl5";
export PERL_MB_OPT="--install_base /home/kurt/perl5";
export PERL_MM_OPT="INSTALL_BASE=/home/kurt/perl5";
export PERL5LIB="/home/kurt/perl5/lib/perl5/x86_64-linux-thread-multi:/home/kurt/perl5/lib/perl5";
export PATH="/home/kurt/perl5/bin:$PATH";
などと出力されるので
eval $(perl -Mlocal::lib)
としておけばよい。local::libに引数を与えるとそのディレクトリに対してインストールすることが可能。簡単な切り替え方法などはhttp://unknownplace.org/memo/2010/03/01/1/に書いている

ソースに記述する場合

local::libの実行結果を.bashrcなどに書いておくのもありだが、そうしたくないケースも無いことも無いのでそういう場合はscriptに
use Findbin qw($Bin);
use local::lib qw($Bin);
などのようにlocal::lib対象ディレクトリを引数に指定した状態でuseするとよい

deactivate

とりあえずlocal::libの設定を無効にしたい場合
eval $(perl -Mlocal::lib=--deactivate-all)
# or
eval $(perl -Mlocal::lib=--deactivate,/path/to/dir)

ExtUtils::Installed

インストールされているモジュールの一覧を取得
perl -MExtUtils::Installed -E 'say for ExtUtils::Installed->new->modules'
以下のように出力結果をファイルに保存して、cpanmに渡せば別環境で同じモジュールをインストールすることが可能とか思った
perl -MExtUtils::Installed -E 'say for ExtUtils::Installed->new->modules' > module-list.txt
cat module-list.txt | cpanm
http://d.hatena.ne.jp/ZIGOROu/20090416/1239863572詳しい

cpan-outdated

インストールされているモジュールの最新版を知ることができる
cpanm App::cpanoutdated
実行すると以下のように表示される
cpan-outdated
J/JD/JDHEDDEN/threads-1.85.tar.gz
J/JD/JDHEDDEN/threads-shared-1.40.tar.gz
Z/ZE/ZEFRAM/Time-HiRes-1.9724.tar.gz
S/SA/SADAHIRO/Unicode-Collate-0.78.tar.gz
cpanmを組み合わせれば表示されたモジュールを最新版にupdateすることができる
cpan-outdated | cpanm

CPAN::Uploader

作ったパッケージをPAUSEにアップロードする
cpanm CPAN::Uploader
cpan-uploadというコマンドがインストールされるのでそれを使えばよい
cpan-upload --user=<USER> ~/path/to/package.tar.gz
$HOME/.pause というファイルを置いておけばオプションなしでもいけそう
user <USER>
password <PASS>

cpan-upload

https://pause.cpan.org にログインして作成したモジュールをアップしなくてもよくなる。
http://search.cpan.org/~neilb/cpan-upload/cpan-upl...
cpan-upload -verbose -mailto aaa@bbb -user xxxx -password xxxxx App-CLI-Extension-0.1.tar.gz
とするだけでよい。結果が以下のように表示される
cpan-upload v2.2
Using FTP to upload files to PAUSE
  uploading file "App-CLI-Extension-0.1.tar.gz"
registering upload with PAUSE web server
  POSTing upload for App-CLI-Extension-0.1.tar.gz
    PAUSE add message sent ok [200]
1 file uploaded successfully.
ちゃんと登録後、メールが来てたり、CPANのアップロード後の状況を確認できるログ表示画面にもアップロードできていることが確認できる

perl critic check

Test::Perl::Criticのテストを行ってくれるサイト。モジュール作る前にチェックしておくとよい
http://perlcritic.com/

ちなみにPerl::Criticをインストールするとperlcriticというコマンドが付属するのでそっちもつかえると思う

Test::Perl::Critic

module-setupなどでモジュールを作成するとperlcriticのテストケースも自動で作成される。このモジュールがあるとテストが自動されるわけだが、かなり厳しいのでそういう場合はxt/perlcriticrcに
[TestingAndDebugging::ProhibitNoStrict]
allow=refs

# for mkdir $dir, 0777
[-ValuesAndExpressions::ProhibitLeadingZeros]

# we do eval $asset_pl a lot
[-BuiltinFunctions::ProhibitStringyEval]

# none use strict
[TestingAndDebugging::RequireUseStrict]
equivalent_modules = Mojo::Base Mojolicious::Plugin DBIx::Skinny DBIx::Skinny::Row

# none use warnings
[TestingAndDebugging::RequireUseWarnings]
equivalent_modules = Mojo::Base Mojolicious::Plugin DBIx::Skinny DBIx::Skinny::Row
などと書いておく。該当箇所には
## no critic
と書いておくのがちょっとしたルール。http://search.cpan.org/~thaljef/Perl-Critic/lib/Pe...みるとなんとなくわかるかも

perltidy

Perl::Tidyをインストールするとついてくるツール。きれいにフォーマットしてくれる。最近のperlだと普通にインストールされているかも
# -o /path/to/file を指定すると出力先を指定できる。指定なしの場合は元のファイル名.tidyみたいなファイル名になる
perltidy /path/to/file
きれいなpodも出力できる
perltidy --html /path/to/file

cpanm

依存も少ないし、軽いらしい。で使いやすいらしい
# さすがにここはcpanで
cpan install App::cpanminus

インストール

cpanm App::CLI::Extension
cpanm HOLLY/App-CLI-Extension-1.3.tar.gz
cpanm /path/to/archive.tar.gz
cpanm http://search.cpan.org/CPAN/authors/id/H/HO/HOLLY/App-CLI-Extension-1.3.tar.gz

自分自身のupdate

cpanm --self-update

download and extract

cpanm --look App::CLI::Extension

info

cpanm --info App::CLI::Extension
# HOLLY/App-CLI-Extension-1.3.tar.gz って出力される

Module::Setup

pmsetupの後継的なやつ(っていうかだいぶ前にでてたらしい)。CPANPLUSをインストールしているのなら
cpanm Module::Setup
でインストール可能。まとめはのちほど。 が詳しい。とりあえず
module-setup --init
module-setup --init --flavor-class=XS xs
最低限こんだけはしとくこと

Module::Install

ExtUtils::MakeMakerの後継。ちょっとしたhackや使い方は をみること
cpanm install Module::Install

Test::Fake::HTTPD

dummy http serverをmake testの時に起動させる用途で使う。内部はTest::TCPつかっているのかな。*.tなファイルで使う
use Test::More;
use Test::Fake::HTTPD;
use HTTP::Response;
use JSON::Any;
use LWP::UserAgent;

my $httpd = run_http_server {
        return HTTP::Response->new(200, "ok", ["Content-Type" => "application/json"], JSON::Any->new->encode({message: "hello"}));
    };
my $httpd = run_http_server { $fake_res };
my $ua = LWP::UserAgent->new;
my $content = $ua->get($httpd->endpoint);
# あとは自分の好きなテスト

Test::Mock::LWP::Conditional

もうひとつhttpなテストするときのねた。こっちはdummy http serverを起動しないパターン

最近はじめにやること

  • perlbrew
  • local::lib
としたうえでこれらをインストール
cpanm install \
Perl::Tidy \
Perl::Critic \
Module::Install \
Module::Install::AuthorTests \
Module::Install::ReadmePodFromPod  \
Module::Install::Repository \
Module::Install::GithubMeta \
Test::Spelling \
Test::Perl::Critic \
Test::Pod

その他いままでの使ったことのある役に立ったモジュール

結構古いものもあるけど、消すとあれなのでとりあえず羅列

昔の記憶

もはや古いのもあるだろうけど
  • Class::Accessor
  • Class::Data::Inheritable
  • Class::Data::Accessor
  • Class::Trigger
  • DBD::Mock
  • Devel::NYTprof
  • FindBin::Real
  • IPC::Cmd
  • IPC::Run
  • IPC::Open2
  • IPE::Open3
  • Path::Class
  • Proc::PID::File
  • Parallel::ForkManager Parallel::Preforkというものもある
  • Term::Prompt
  • Sub::Install
  • Class::C3(NEXTのかわりね) 5.10.0からはmro "c3"とするといいみたい。でその架け橋となるのがMRO::Compat
  • UNIVERSAL::require
  • Module::Pluggable
  • Log::Dispatch::Config
  • Regexp::Common

最近

  • Sub::Retry
  • autodie
  • parent
  • lib::require::all
タグ

Wiki内検索

Menu

ここは自由に編集できるエリアです。

管理人/副管理人のみ編集できます