# さくっと Perl のメトリクスを取得する {{tag: perl, cpan}} ローカルにあるやつもリモートにあるやつも、ファイルごとでもディレクトリ内まとめてでも、よしなにサクッと Perl コードのメトリクスを取得したかったので書きました。 ## App::plmetrics cpanm App::plmetrics {{cpan: App::plmetrics}} をインストールすると、`plmetrics` コマンドが使えるようになります。 {{cpan: Perl::Metrics::Lite}} に同梱されている {{cpan: measureperl}} はローカルファイルまたはディレクトリを対象に解析を行いますが、plmetrics はローカルのものだけでなく、リモートの git リポジトリや tar.gz を対象に解析することが可能です。 ### plmetrics モジュール名で解析。`--module` $ plmetrics --module Test::AllModules /usr/lib/perl5/site_perl/5.8.8/Test/AllModules.pm .---------------------------------------------------. | | avg | max | min | range | sum | methods | +-------+-------+-----+-----+-------+-----+---------+ | cc | 6.67 | 25 | 2 | 23 | 60 | 9 | | lines | 14.11 | 52 | 3 | 49 | 127 | 9 | '-------+-------+-----+-----+-------+-----+---------' 手元のファイルを解析。`--file` $ plmetrics --file lib/App/plmetrics.pm lib/App/plmetrics.pm .--------------------------------------------------. | | avg | max | min | range | sum | methods | +-------+------+-----+-----+-------+-----+---------+ | cc | 2.95 | 11 | 1 | 10 | 59 | 20 | | lines | 9.70 | 26 | 1 | 25 | 194 | 20 | '-------+------+-----+-----+-------+-----+---------' ディレクトリは `--dir` $ plmetrics --dir lib App/plmetrics.pm .--------------------------------------------------. | | avg | max | min | range | sum | methods | +-------+------+-----+-----+-------+-----+---------+ | cc | 2.95 | 11 | 1 | 10 | 59 | 20 | | lines | 9.70 | 26 | 1 | 25 | 194 | 20 | '-------+------+-----+-----+-------+-----+---------' `--result methods` を付けるとメソッドごとの結果表示になります。 $ plmetrics --file lib/App/plmetrics.pm --result methods .---------------------------------. | | cc | lines | +--------------------+----+-------+ | _stats_methods | 5 | 14 | | _load_or_recommend | 5 | 16 | | _header | 1 | 3 | | _target_git | 3 | 20 | | opt | 1 | 1 | | _stats_cc | 1 | 4 | | _target_module | 2 | 7 | | run | 1 | 6 | | _stats_lines | 1 | 4 | | _round | 1 | 3 | | new | 1 | 4 | | _stats_module | 3 | 12 | | _row | 1 | 6 | | _cc_lines | 3 | 11 | | _get_targets | 6 | 8 | | _target_tar | 4 | 26 | | _get_metrics | 5 | 20 | | _target_dir | 2 | 7 | | _target_file | 2 | 6 | | _stats | 11 | 16 | '--------------------+----+-------' `--result cc` や `--result lines` もあります。 #### リモートの例 github だとこんな感じ(せっかくなので `--result cc`)。 $ plmetrics --git git://github.com/bayashi/App-YG.git --result cc cc .--------------------------------------------------------------------------. | | avg | max | min | range | sum | methods | +-------------------------------+------+-----+-----+-------+-----+---------+ | lib/App/YG/Apache/Error.pm | 4.00 | 7 | 1 | 6 | 8 | 2 | | lib/App/YG.pm | 5.00 | 10 | 1 | 9 | 95 | 19 | | lib/App/YG/Nginx/Main.pm | 7.00 | 13 | 1 | 12 | 14 | 2 | | lib/App/YG/Apache/Common.pm | 5.50 | 10 | 1 | 9 | 11 | 2 | | lib/App/YG/Apache/Combined.pm | 6.50 | 12 | 1 | 11 | 13 | 2 | '-------------------------------+------+-----+-----+-------+-----+---------' あと、tar.gz の URL は `--tar` でいけます。 ## まとめ {{cpan: Docopt}} まじイノベーティブ!!