這個模塊用註釋的方式調試和跟蹤代碼,寫好了之後把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