使用Smart::Comments模块调试Perl脚本
Smart::Comments - Comments that do more than just sit there"Comments that do more than just sit here...."
以前脚本高度使用Data::Dumper,它让调式更方便了。
Smart comments provide an easy way to insert debugging and tracking code into a program. They can report the value of a variable, track the progress of a loop, and verify that particular assertions are true.
Best of all, when you're finished debugging, you don't have to remove them. Simply commenting out the use Smart::Comments line turns them back into regular comments. Leaving smart comments in your code is smart because if you needed them once, you'll almost certainly need them again later.
Smart::Comments不仅能像Dumper那样把你关心的变量信息打印出来,更重要的是,当你准备发布你的程序时,你可以直接将user Smart::Comments改为no Smart::Comments,多余的调式信息一下就消失了。
'Smart::Comments'会把特定的注释变成调试语句。
示例代码:
use Smart::Comments;
$a{'a'}[0]=28;
#debug info
### %a
$a{'a'}[0]++;
### %a
print $a{'a'}[0];
可以发现在###注释的地方,程序把'%a'哈希输出了,这样只要通过简单的是否use Smart::Comments就可以在调试模式下切换了。
还有从命令行调用的方式,这样就不必使用'use'了:
perl -MSmart::Comments smart.pl
在Perl程序中显示进度条之使用Smart::Comments模块
简单的演示
#!/usr/bin/perl
use Smart::Comments;
#Can be use for "foreach, for, while, until)
for ( 1 .. 10 ) { ### Progressing... done
sleep(1);
}
for ( 1 .. 10 ) { ### Progressing===| done
sleep(1);
}
for ( 1 .. 10 ) { ### Evaluating [===| ] % done
sleep(1);
}
for ( 1 .. 10 ) { ### Evaluating |===[%] |
sleep(1);
}
显示剩余时间
When a progress bar is used with a for loop, the module tracks how long each iteration is taking and makes an estimate of how much time will be required to complete the entire loop.
Normally this estimate is not shown, unless the estimate becomes large enough to warrant informing the user. Specifically, the estimate will be shown if, after five seconds, the time remaining exceeds ten seconds. In other words, a time-remaining estimate is shown if the module detects a for loop that is likely to take more than 15 seconds in total.
The precision of the reported time-remaining estimate is deliberately vague, mainly to prevent it being annoyingly wrong.
use Smart::Comments;
for ( 1 .. 10 ) { ### Fighting: [||| ]
sleep(5);
}
参考文档:Smart::Comments