LoginSignup
1
1

More than 3 years have passed since last update.

[Perl] Smart::Commentsで安全にデバッグする

Posted at

今までずっと Data::Dumper を使ってデバッグしてました

test.pl
use Data::Dumper;

my $hoge = rand 10;
warn Dumper $hoge;
$ perl test.pl 
$VAR1 = '2.82097955540852';

調べてみたところ、Dumperした結果を同じく表示してくれるデバッグ用のモジュールで Smart::Comments というものがあるみたいです

導入

$ cpanm Smart::Comments

使ってみる

先程のものを、 Smart::Comments で書き換えてみます
デバッグしたいものの先頭に、コメントアウトを3つ付与します

test.pl
use Smart::Comments;

my $hoge = rand 10;
### $hoge;
$ perl test.pl
### $hoge: '5.59813882530765'

表示されました!先頭にコメントマークが3つついてますね

実行時だけモジュールを読み込ませてあげるようにする事もできます

test.pl
my $hoge = rand 10;
### $hoge;
$ perl -MSmart::Comments test.pl

### $hoge: '6.25320952226506'

こうすることで、完全にソースコードを汚さずに実行できるのがいいですね
もしデバッグを消し忘れたりタイポしても、ただのコメントアウトとしてperlは処理してくれるのでエラーになりません

my $hoge = rand 10;

# Data::Dumperlの場合、タイポしたらエラーになる
warn Dumper $hogee;

# Smart::Commentsの場合、コメントなのでタイポしててもエラーにならない
### $hogee;

心理的に楽かも?

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1