How do I control the variable names in Perl's

2020-05-23 16:19发布

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
       );

4条回答
啃猪蹄的小仙女
2楼-- · 2020-05-23 17:00

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

查看更多
霸刀☆藐视天下
3楼-- · 2020-05-23 17:10
use Data::Dumper;

$Data::Dumper::Terse = 1;

print '%foo = '.(Dumper \%foo);
查看更多
做自己的国王
4楼-- · 2020-05-23 17:19
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.

查看更多
beautiful°
5楼-- · 2020-05-23 17:22

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

查看更多
登录 后发表回答