perl模块之Smart::Comments
这个模块用注释的方式调试和跟踪代码,写好了之后把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... 可以用 获得 检查和断言### require: BOOLEAN_EXPR ### assert: BOOLEAN_EXPR ### ensure: BOOLEAN_EXPR ### insist: BOOLEAN_EXPR ### require: $min < $result && $result < $max 如果表达式是假,注释就相当于执行 ### $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 就是上面的 进度条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个 也可以加入百分比的进度 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 (@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下来,编译安装就可以了
参考http://search.cpan.org/~dconway/Smart-Comments-1.000005/lib/Smart/Comments.pm (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |