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

ral*_*ldi 10 perl data-dumper

我有这个简单的Perl脚本:

#! /usr/bin/perl -w

use strict;
use Data::Dumper;

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

print Dumper(\%foo);
Run Code Online (Sandbox Code Playgroud)

它输出:

$VAR1 = {
          'abc' => 1
        };
Run Code Online (Sandbox Code Playgroud)

如何让它输出呢?

%foo = (
         'abc' => 1
       );
Run Code Online (Sandbox Code Playgroud)

yst*_*sth 23

print Data::Dumper->Dump( [ \%foo ], [ qw(*foo) ] );
Run Code Online (Sandbox Code Playgroud)

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


Cha*_*ens 8

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


小智 5

use Data::Dumper;

$Data::Dumper::Terse = 1;

print '%foo = '.(Dumper \%foo);
Run Code Online (Sandbox Code Playgroud)