【问题标题】:how can I display a variable's name in perl, along with the value of the variable?如何在 perl 中显示变量的名称以及变量的值?
【发布时间】:2019-11-12 16:40:23
【问题描述】:

为了诊断或调试我的 perl 代码,我想轻松地显示变量的名称及其值。在bash 中,键入以下内容:

#!/bin/bash
dog=pitbull
declare -p dog

perl 中,考虑以下脚本junk.pl

#!/usr/bin/perl
use strict; use warnings; 
my $dog="pitbull";
my $diagnosticstring;
print STDERR "dog=$dog\n";
sub checkvariable {
    foreach $diagnosticstring (@_) { print "nameofdiagnosticstring=$diagnosticstring\n"; }
}
checkvariable "$dog";

如果我们调用这个脚本,我们会得到

bash> junk.pl
dog=pitbull
nameofdiagnosticstring=pitbull
bash> 

但是,当调用子例程checkvariable 时,我希望打印以下内容:

dog=pitbull

这将使编码更容易,更不容易出错,因为不必输入两次变量的名称。

【问题讨论】:

标签: perl subroutine declare


【解决方案1】:

您可以使用PadWalker(您需要从 CPAN 安装)执行类似的操作。但几乎可以肯定,它比您希望的要复杂得多。

#!/usr/bin/perl

use strict;
use warnings; 
use PadWalker 'peek_my';

my $dog="pitbull";
print STDERR "dog=$dog\n";
sub checkvariable {
  my $h = peek_my(0);
  foreach (@_) {
    print '$', $_,'=', ${$h->{'$'. $_}}, "\n";
  }
}
checkvariable "dog";

【讨论】:

    【解决方案2】:

    Data::Dumper::Names 可能就是您要找的。​​p>

    #! perl
    
    use strict;
    use warnings;
    
    use Data::Dumper::Names;
    
    my $dog = 'pitbull';
    my $cat = 'lynx';
    my @mice = qw(jumping brown field);
    
    checkvariable($dog, $cat, \@mice);
    
    sub checkvariable {
        print Dumper @_;
    }
    
    1;
    

    输出:

    perl test.pl
    
    $dog = 'pitbull';
    $cat = 'lynx';
    @mice = (
              'jumping',
              'brown',
              'field'
            );
    

    【讨论】:

      【解决方案3】:

      (不是答案,是格式化的评论)

      checkvariable sub 只接收一个值,并且没有(简单或可靠的)方法可以找出 variable 持有该 value 的内容。

      这就是 Data::Dumper 强制您将 varnames 指定为字符串的原因:

      perl -MData::Dumper -E '
        my $x = 42;
        my $y = "x";
        say Data::Dumper->Dump([$x, $y]);
        say Data::Dumper->Dump([$x, $y], [qw/x y/])
      '
      
      $VAR1 = 42;
      $VAR2 = 'x';
      
      $x = 42;
      $y = 'x';
      

      【讨论】:

        【解决方案4】:

        以下内容通常会有所帮助

        use strict;
        use warnings;
        
        use Data::Dumper;
        
        my $debug = 1;
        
        my $container = 20;
        my %hash      = ( 'a' => 7, 'b' => 2, 'c' => 0 );
        my @array     = [ 1, 7, 9, 8, 21, 16, 37, 42];
        
        debug('container',$container) if $debug;
        debug('%hash',  \%hash)       if $debug;
        debug('@array', @array)       if $debug;
        
        sub debug {
            my $name  = shift;
            my $value = shift;
        
            print "DEBUG: $name [ARRAY]\n", Dumper($value) if ref $value eq 'ARRAY';
            print "DEBUG: $name [HASH]\n",  Dumper($value) if ref $value eq 'HASH';
            print "DEBUG: $name = $value\n"                if ref $value eq '';
        }
        

        但是为什么不在内置调试器下运行 perl 脚本呢?选项 -d

        The Perl Debugger

        【讨论】:

          猜你喜欢
          • 2011-01-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多