perl 神器之 Smart::Comments

这个Perl 神器是一个CPAN模块,顾名思义,它是一个智能的注释模块。下面看CPAN中它的自我介绍

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.

1.直观的一瞥

首先我们先来体验一下这个模块的功能:

您也可以看爱程序网 这里的介绍:
貌似 xshalk 的博客也有。

首先写如下程序:

$ vim test.smart_comments.pl
use strict;use warnings;
use Smart::Comments;

for my $value( 1 .. 3 ){
  
  my $result = $value + 4;

  ### $result
}


我们来看运行结果:

$perl  test.smart_comments.pl


### $result: 5

### $result: 6

### $result: 7

如果关掉 Smart::Commets, 那么就没有输出结果!

从上面可以看到这个模块的功能!

2. 记载常用的命令,以供以后使用

  1. 查看变量与输出程序执行信息:
### EXPRESSION 

### TEXT... 

### [<now>] Acquiring data...
### Acquiring data at <loc>...

[<now>] 将会输出 程序执行的时间!

\2. 显示进度条(Progress Bars)

 for (@candidates) {       ### Evaluating [===|    ] % done

将会显示:

    Evaluating [|                ]   0% done

    Evaluating [===|             ]  25% done

    Evaluating [========|        ]  50% done

    Evaluating [============|    ]  75% done

    Evaluating [=================] 100% done

\3. 检查断言(Checks and Assertions via Comments) ?

  ### require: BOOLEAN_EXPR.

  ### require: $min < $result && $result < $max

if the expression evaluated false, the comment would die with the following message:

    ### $min < $result && $result < $max was not true at demo.pl line 86.
    ###     $min was: 7
    ###     $result was: 1000004
    ###     $max was: 99
    原文作者:沧浪之水v
    原文地址: https://www.jianshu.com/p/f694f2417cf9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞