这个模块用注释的方式调试和跟踪代码,写好了之后把use Smart::Comments去掉就可以了。
实验
最后上代码实验大部分特性:
[root@localhost Smart::Comments]# cat 1.pl #!/usr/bin/env perl use strict; use Smart::Comments; my $date = "2014-05-01"; #### Get: $date ### [<now>] Now... ### [<time>] Time... ### [<when>] When... ### Get time[<time>] ### Get here[<here>] ### Get file[<file>] ### Get line[<line>] my @values = (1..10); for (@values) { #### Progress[=== ] % done ### Round:$_ do_sth($_); } sleep 1; ### Compare lenth... my $len = length $date; ### require: $len > 11 sub do_sth() { my $a = shift; $a *= 2; } ### $date [root@localhost Smart::Comments]# perl 1.pl ### Get: '2014-05-01' ### [Thu May 1 22:38:49 2014] Now... ### [Thu May 1 22:38:49 2014] Time... ### [Thu May 1 22:38:49 2014] When... ### Get time[Thu May 1 22:38:49 2014] ### Get here["1.pl",line 14] ### Get file[1.pl] ### Get line[16] Progress[ ] 0% done ### Round: 1 Progress[= ] 11% done ### Round: 2 Progress[=== ] 22% done ### Round: 3 Progress[===== ] 33% done ### Round: 4 Progress[====== ] 44% done ### Round: 5 Progress[======== ] 55% done ### Round: 6 Progress[========== ] 66% done ### Round: 7 Progress[=========== ] 77% done ### Round: 8 Progress[============= ] 88% done ### Round: 9 ### Round: 10 ### Compare lenth... ### $len > 11 was not true at 1.pl line 27. ### $len was: 10
功能
- 显示变量的值
- 跟踪循环
- 验证断言
用法
#!/usr/bin/env perl use strict; use Smart::Comments; my $test = "Nice to meet you " ### $test [root@localhost Smart::Comments]# perl 1.pl ### $date: 'Nice to meet you '
使用3个#号,可以打印出后面的变量值 增加#号可以让模块更智能,最多5个#号
Debugging
### Label: Expression
打印变量值和标签
### expression
打印变量
### text...
进度条
可以用<time>
<here>
<file>
<line>
检查和断言
### require: BOOLEAN_EXPR
### assert: BOOLEAN_EXPR
### ensure: BOOLEAN_EXPR
### insist: BOOLEAN_EXPR
### require: $min < $result && $result < $max
如果表达式是假,注释就相当于执行die
命令,否者什么都不做:
### $min < $result && $result < $max was not true at demo.pl line 86. ### $min was: 7 ### $result was: 1000004 ### $max was: 99
### check: BOOLEAN_EXPR
### confirm: BOOLEAN_EXPR
### verify: BOOLEAN_EXPR
就是上面的warn
的版本
进度条
foreach my VAR ( LIST ) { ### Progressing... done for my VAR ( LIST ) { ### Progressing... done foreach ( LIST ) { ### Progressing... done for ( LIST ) { ### Progressing... done while (CONDITION) { ### Progressing... done until (CONDITION) { ### Progressing... done for (INIT; CONDITION; INCR) { ### Progressing... done
用C风格的循环,左边的花括号{
放在同一行,把注释也放到这行
for (@candidates) { ### Evaluating... done
执行效果如下,模拟...
到达右边的字符
Evaluating done Evaluating...... done Evaluating............. done Evaluating................... done Evaluating..........................done
当然可以把3个.
换成3个:
、=
、 |
也可以加入百分比的进度%
for (@candidates) { ### Evaluating [===| ] % done 效果如下 Evaluating [| ] 0% done Evaluating [===| ] 25% done Evaluating [========| ] 50% done Evaluating [============| ] 75% done Evaluating [=================] 100% done for (@candidates) { ### Evaluating |===[%] | 效果如下 Evaluating |[0%] | Evaluating |=[25%] | Evaluating |========[50%] | Evaluating |===============[75%] | Evaluating |===========================|
对于开放式的循环,比如一个带判断的while循环,%
就代表循环次数
时间估计
在for
循环的注释中,如果一次循环超过15秒,就会出现一个剩余时间故事的框 比如
for (@seven_samurai) { ### Fighting: [||| ] fight(); sleep 5; } 效果: Fighting: [ ] Fighting: [|||| ] Fighting: [||||||||| ] (about 20 seconds remaining) Fighting: [|||||||||||||| ] (about 20 seconds remaining) Fighting: [|||||||||||||||||| ] (about 10 seconds Fighting: [||||||||||||||||||||||| ] (less than 10 seconds remaining) Fighting: [|||||||||||||||||||||||||||]
依赖
都是核心模块,所以直接down下来,编译安装就可以了
- Filter::Simple
- version.pm
- List::Util
- Data::Dumper
- Text::Balanced
参考
http://search.cpan.org/~dconway/Smart-Comments-1.000005/lib/Smart/Comments.pm