如何在Perl的Data :: Dumper中控制变量名?

时间:2022-08-28 15:53:26

I've got this simple Perl script:

我有这个简单的Perl脚本:

#! /usr/bin/perl -w

use strict;
use Data::Dumper;

my %foo = ( 'abc' => 1 );

print Dumper(\%foo);

It outputs:

$VAR1 = {
          'abc' => 1
        };

How do I make it output this instead?

如何让它输出呢?

%foo = (
         'abc' => 1
       );

4 个解决方案

#1


print Data::Dumper->Dump( [ \%foo ], [ qw(*foo) ] );

The extended syntax takes two arrayrefs: one of scalars to dump, and one of names to use. If the name is prefixed by * and the corresponding scalar is an arrayref or hashref, an array or hash assignment is produced.

扩展语法需要两个arrayrefs:一个要转储的标量,另一个要使用的名称。如果名称前缀为*且相应的标量是arrayref或hashref,则会生成数组或散列赋值。

#2


In addition to ysth's answer, you can use Ovid's Data::Dumper::Names module.

除了ysth的答案,你可以使用Ovid的Data :: Dumper :: Names模块。

#3


use Data::Dumper;

$Data::Dumper::Terse = 1;

print '%foo = '.(Dumper \%foo);

#4


Also, Data::Dumper::Simple does roughly that.

此外,Data :: Dumper :: Simple大致相同。

#1


print Data::Dumper->Dump( [ \%foo ], [ qw(*foo) ] );

The extended syntax takes two arrayrefs: one of scalars to dump, and one of names to use. If the name is prefixed by * and the corresponding scalar is an arrayref or hashref, an array or hash assignment is produced.

扩展语法需要两个arrayrefs:一个要转储的标量,另一个要使用的名称。如果名称前缀为*且相应的标量是arrayref或hashref,则会生成数组或散列赋值。

#2


In addition to ysth's answer, you can use Ovid's Data::Dumper::Names module.

除了ysth的答案,你可以使用Ovid的Data :: Dumper :: Names模块。

#3


use Data::Dumper;

$Data::Dumper::Terse = 1;

print '%foo = '.(Dumper \%foo);

#4


Also, Data::Dumper::Simple does roughly that.

此外,Data :: Dumper :: Simple大致相同。