2024/02/13 22:13:39

perltidy

perltidy(ぱーるたいでぃ) は、Perlスクリプトを整形するツールです。
以下のように CPAN から Perl::Tidy モジュールをインストールすると、ついてきます。

$ cpan -i Perl::Tidy

使い方は簡単です。

まず、以下のようなコードを perltidy で整形します。

[ blood.pl ]

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

print "what is your blood type?";
chomp($blood = <STDIN>);
if ($blood =~ /a/i) {
print "sensitive";
} elsif ($blood =~ /b/i) {
print "headstrong";
} elsif ($blood =~ /o/i) {
print "carefree";
} else {
print "indecision";
}

perltidy コマンドにスクリプトを渡すと、blood.pl.tdy というファイルが作成されます。

$ perltidy blood.pl

ソースコードの中身は以下のように整形されています。

[ blood.pl.tdy ]

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

print "what is your blood type?";
chomp( $blood = <STDIN> );
if ( $blood =~ /a/i ) {
    print "sensitive";
}
elsif ( $blood =~ /b/i ) {
    print "headstrong";
}
elsif ( $blood =~ /o/i ) {
    print "carefree";
}
else {
    print "indecision";
}

print 文がインデントされ、if () のカッコ内に空白があけられていたり、else が改行されていたりと、読みやすくなっています。 perltidy によってコーディング規約を自動的に統一することができるわけです。便利ですね。

perltidy の出力オプション

もしも、オリジナルのファイルを整形されたソースにして、バックアップにオリジナルのソースとしたい場合は、-b オプションを利用します。

$ perltidy -b blood.pl

これで、 blood.pl が整形されたソースとなり、blood.pl.bak に元のコードがバックアップされます。

unix 的な入出力も可能です。

$ perltidy < blood.pl > reformatted_blood.pl

ファイルに出力せず、標準出力に吐き出すには、-st オプションをつけます。

$ perltidy -st blood.pl

perltidy は、整形だけでなく、-html オプションによって、HTML 出力用のフィルタとしても動作します(同時に整形はできない)。

$ perltidy -html blood.pl

また、perltidy は複数のファイルを一度に整形することもできます。

$ perltidy foo.pl bar.pl

ワイルドカードを使って、ディレクトリ内のファイルを一気に perltidy にかけることもできます。

$ perltidy *.pl

なお、バックアップとして生成される .tdy や .bak ファイルは、すでに存在する場合、上書きされてしまうので注意が必要です。

perltidyの設定

perltidy の整形ルールは、コマンドラインのオプションで指定するか、.perltidyrc をホームディレクトリに用意します。

コマンドラインのオプション

$ perltidy -i=2 -l=78 -ce foo.pl

細かいことを気にしたくないという人は、-pbp オプションで、Perl Best Practice 推奨のスタイルが適用できます。

$ perltidy -pbp foo.pl

$ perltidy -pbp foo.pl > bar.pl

.perltidyrc

.perltidyrc をどこに置けばよいかは -dpro オプションで知ることができます。

$ perltidy -dpro
# Config file search...system reported as: linux
# Testing: .perltidyrc
# Examining: $ENV{PERLTIDY}
# Examining: $ENV{HOME} = /home/bayashi
# Testing: /home/bayashi/.perltidyrc
# Testing: /usr/local/etc/perltidyrc
# Testing: /etc/perltidyrc
# ...no config file found

.perltidyrc のフォーマットは自由です。 パラメータをシンプルに並べるだけでかまいません。1行にいくつのパラメータを並べても良いし、それが何行になってもかまいません。 とはいえ、1行に1パラメータがもっとも読みやすいでしょう。

また、.perltidyrc では 空行と、# 文字以降は無視されます。

~/.perltidyrc

-l=78   # Max line width is 78 cols
-i=4    # Indent level is 4 cols
-ci=4   # Continuation indent is 4 cols
-st     # Output to STDOUT
-se     # Errors to STDERR
-vt=2   # vertical tightness
-cti=0  # Closing Token Placement
-pt=2   # Medium parenthesis tightness
-bt=1   # Medium brace tightness
-sbt=1  # Medium square brace tightness
-bbt=0  # Medium block brace tightness
-nsfs   # No space before semicolons
-nolq   # Don't outdent long quoted strings
# Break before all operators
-wbb="% + - * / x != == >= <= =~ !~ < > | & >= < = **= += *= &= <<= &&= -= /= |= >>= ||= .= %= ^= x="

.perltidyrc ではない名前のファイルを .perltidyrc として使いたい場合は、-pro オプションを利用して、ファイルを指定します。

$ perltidy -pro=file

SEE ALSO

オプションについて詳しい説明は CPAN - perltidyPerltidy Style Key にあります。

サイト内検索