使用标志进行 Perl 调试

Perl Debugging Using Flags

所以我的目标是找到一种使用标志在 Perl 中打开打印语句的简单方法。在 C/C++ 中,您可以使用 #define 来选择某些代码是否为 运行,这是一种打开和关闭调试打印语句的方法。如果定义了#define DEBUG,那么你打印一些东西,否则你 运行 它没有打印语句。我想知道在 Perl 中是否有一种简单的方法可以做到这一点。

这是它如何工作的一个例子:

for($i = 0 ; $i < 10; $i++){
    if(debug flag){
         print some info;
    }
    do operational stuff.
}

现在,您可以从命令行执行以下两项操作之一:

1.Run 没有调试打印语句

perlScript.pl 

2.Run 带有调试打印语句

perlScript.pl -debug

或者如果有人有更好的主意请告诉我!

与您的想法类似,但更简洁,转到 stderr 并假设您使用 Getopt::Long 之类的东西来设置调试 CLI 选项。

warn "debug info:..." if ( $debug );

在perl中,编译时间也是运行时间。因此,使用 #define 类型语句确实没有太大优势。

我常用的技巧是:

my $debug = 0; 
$debug += scalar grep ( "-d", @ARGV ); 

(不过老实说,GetOpt 可能是一个更好的计划)

然后使用:

print if $debug;
print $statement if $debug > 2;

这意味着我有一种简单的方法来设置冗长程度,还允许我通过递增语句来选择它。

有时我会嵌入一个信号处理程序来调整调试级别 -

#!/usr/bin/perl

use strict;
use warnings;

my $debug = 0; 
$debug += scalar grep ( "-d", @ARGV ); 

$SIG{'USR1'} = { $debug++ };
$SIG{'USR2'} = { $debug-- };

while ( 1 ) {
    print "Debugging at: $debug\n";
    sleep 1;
}

更多的是我在写什么样的代码的问题——我在做 forky 的时候特别喜欢后者,因为这样我就可以在每个 fork 中独立地动态调整调试级别。

我通常使用以下样板通过 Log::Log4perl 登录脚本。您可以从命令行覆盖 logbase/log 配置位置(或者,更常见的是,将适当的位置设置为部署时的默认位置),并在向脚本提供一个或多个 -verbose 标志后,覆盖该日志记录并记录到屏幕, 4 verboses 给你屏幕输出。这使您可以通过提供冗长的标志以获取每个日志输出轻松地从调试过渡到将其传递到自定义日志处理程序配置以调试子系统,再到在生产部署中设置日志记录,所有这些都需要 minimal/no 代码更改。

use Getopt::Long;
use Pod::Usage;
use Log::Log4perl qw/:easy/;

my $opts = { logconf        => undef,
             logbase        => 'corp.team.app.appname'
             debug          => 0,
           };

GetOptions ( 'logconf|l=s'       => $opts->{logconf},
             'logbase=s'         => $opts->{logbase},
             'verbose|v+'        => $opts->{debug},  ### debug levels - 0 = off (default), 1 = error, 2 = warn, 3 = info, 4 = debug.
                                                      ### Ignored if a logconf is provided.
           ) or pod2usage(0);

### Initialize logging subsystem
init_logger();

### Usage
logger('app_subsystem')->info('some message...');
logger()->debug('debug message...');


### Initialize logging system
sub init_logger {
    ### If a log configuration is found, and debug was not set, use it
    if (        $opts->{logconf}
         and -e $opts->{logconf}
         and  ! $opts->{debug}
       ) {
        Log::Log4perl->init($opts->{logconf});
    }
    ### Otherwise fall through to easy_init a screen logger based on the verboseness level
    ### Logging off if no config found and no verboseness set
    else {
        my ($min, $max) = ( 0, 4 );
        my %levels;
        @levels{$min .. $max} = ( $OFF, $ERROR, $WARN, $INFO, $DEBUG );
        my $log_level = $opts->{debug};
        if ($log_level < $min) {
            $log_level = $min;
        }
        elsif ($log_level > $max) {
            $log_level = $max;
        }
        Log::Log4perl->easy_init($levels{$log_level});
    }
}

### Shorthand shim sub to get a logger
### Always returns a Log::Log4perl logger object
sub logger {
    my ($category) = @_;
    if ($category) {
        return Log::Log4perl->get_logger($opts->{logbase} . '.' . $category);
    }
    return Log::Log4perl->get_logger($opts->{logbase});
}

从 v5.10 开始,您还可以通过 -P 命令行开关在您的 Perl 脚本上直接使用 C 预处理器,记录在 perlrun.

#!perl -P
# myScript.pl
#define DEBUG(x) x
$foo = 42;
DEBUG( print "foo is $foo\n"; )

$ perl -P myScript.pl
foo is 42

(与 -T 开关一样,#!perl -P 不会自动在您的脚本中使用 C 预处理器,但它会强制您每次都明确地使用 -P 开关你 运行 脚本)

-s 选项允许您在命令行上指定 main 包变量的值,尽管您必须 运行 从文件而不是使用 perl 程序-e 选项

如果你有 perl 程序

use strict;
use warnings;

our $debug
print $debug, "\n";

和运行它与命令行

perl -s myprog.pl -debug

然后程序将打印1

请注意,您可以在程序文件本身的 shebang 行上指定它,而不是在命令行上使用 -s,因此如果您的代码看起来像

#!/usr/bin/perl -s

use strict;
use warnings;

our $debug
print $debug, "\n";

那么你的命令行只需要包含

myprog.pl -debug