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

寵の児 提交于 2019-12-03 09:34:39

问题


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

回答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.




回答2:


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




回答3:


use Data::Dumper;

$Data::Dumper::Terse = 1;

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



回答4:


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



来源:https://stackoverflow.com/questions/908741/how-do-i-control-the-variable-names-in-perls-datadumper

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!