3

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

这是它如何工作的示例:

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

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

1.在没有调试打印语句的情况下运行

perlScript.pl 

2.运行调试打印语句

perlScript.pl -debug

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

4

5 回答 5

3

在 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 中的调试级别。

于 2015-06-23T16:24:24.753 回答
2

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

如果你有一个 perl 程序

use strict;
use warnings;

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

并使用命令行运行它

perl -s myprog.pl -debug

然后程序将打印1

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

#!/usr/bin/perl -s

use strict;
use warnings;

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

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

myprog.pl -debug
于 2015-06-23T19:50:06.087 回答
1

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

warn "debug info:..." if ( $debug );
于 2015-06-23T16:20:24.137 回答
1

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

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});
}
于 2015-06-23T16:41:51.477 回答
-2

在 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每次运行脚本时自己显式使用开关)

于 2015-06-23T19:25:39.687 回答