简体   繁体   中英

How do I control the variable names in Perl's Data::Dumper?

I've got this simple Perl script:

#! /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
       );
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.

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

use Data::Dumper;

$Data::Dumper::Terse = 1;

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM