使用Data::Dumper模块展示复杂结构
它的主要用途是:给出一个或多个变量,包括引用,以Perl语法的方式返回这个变量的内容。比如,这里有个很复杂的hash,数据结构很复杂,我想看看这个hash里面的内容。除了常见的方式(直接用print或者编历keys然后打印), 我们也可以使用Data::Daumper->Dump([\%hash])的形式。同时,模块中定义了很多的配置参数,让用户可以调整打印格式。有时我们需要知道相关的变量中的变量值,对于只含单个变量元素还好处理,复杂结构的只能借助于第三方模块来得到了。
Data::Dumper有面向对象和直接使用函数两种调用方法,这里介绍直接使用函数的方式,简单好用,能够满足绝大多数需求:Dumper接收的参数为一个标量的列表或者一个引用的列表。
程序的输出会按照引用在参数列表中的位置自动命名VAR[n]。
当然它自身也是可配置的,这样可以控制它的输出格式等方面。
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
The first statement makes the output more compact and much more readable when your data structure is several levels deep.
The second statement makes it easier to scan the output and quickly find the keys you are most interested in.
If the data structure contains binary data or embedded tabs/newlines, also consider
$Data::Dumper::Useqq = 1;
which will output a suitable readable representation for that data.
$Data::Dumper::Indent
这个设置打印的缩进格式,可以设置成0,1,2和3,用户可以自己尝试下
$Data::Dumper::Terse
如果设置这个变量,则不打印变量的名字,只打印变量的内容
$Data::Dumper::Maxdepth
不超过这个变量的限制深度,才打印变量的内容
use Data::Dumper;
my%people=(
'name'=>'hto',
'age'=> 28,
'domain'=>['freeoa.net','163.com'],
'family'=>{'husd'=>'tom','wife'=>'maray','son'=>'bat'},
'red'=> [{'hero' => 'Spiderman', 'villain' => 'Green Goblin'}, {'hero' => 'Superman', 'villain' => 'LexLuthor'}]
);
print"Show perl hash, with pre-defined variable name\n";
print"and without maxdepth\n";
$Data::Dumper::Terse = 0;# default is 0
$Data::Dumper::Indent = 1;# default is 2
$Data::Dumper::Maxdepth = 0;# default is 0
my $variable_name='*'."my_info";
print Data::Dumper->Dump([\%people],[$variable_name]);
print"Show perl hash, without pre-defined variable name\n";
print"and with maxdepth is 1\n";
$Data::Dumper::Terse = 1;# default is 0
$Data::Dumper::Indent = 2;# default is 2
$Data::Dumper::Maxdepth = 1;# default is 0
$variable_name='$'."my_info";
print Data::Dumper->Dump([\%people],[$variable_name]);
$ perl dumper.pl
Show perl hash, with pre-defined variable name
and without maxdepth
%my_info = (
'domain' => [
'freeoa.net',
'163.com'
],
'red' => [
{
'villain' => 'Green Goblin',
'hero' => 'Spiderman'
},
{
'villain' => 'LexLuthor',
'hero' => 'Superman'
}
],
'name' => 'hto',
'age' => 28,
'family' => {
'son' => 'bat',
'wife' => 'maray',
'husd' => 'tom'
}
);
Show perl hash, without pre-defined variable name
and with maxdepth is 1
{
'domain' => 'ARRAY(0x96b1818)',
'red' => 'ARRAY(0x975c998)',
'name' => 'hto',
'age' => 28,
'family' => 'HASH(0x96cb698)'
}
Data::Dumper::Pertidy
One possible solution is to use Data::Dumper::Pertidy which runs the output of Data::Dump through Perltidy.
use Data::Dumper::Perltidy;
my $data = [{title=>'This is a test header'},{data_range=>[0,0,3,9]},{format => 'bold' }];
print Dumper $data;
__END__
Prints:
$VAR1 = [
{ 'title' => 'This is a test header' },
{ 'data_range' => [ 0, 0, 3, 9 ] },
{ 'format' => 'bold' }
];
在一次打印多个变量时,会按其顺序从编号1开始进行计数:VAR1,VAR2...
这可能会带来一些不便,可以使用一些写法来将变量与变量内容对应起来:
Data::Dumper->Dump([\@nums, \@new],[qw/*nums *new/]);
将会有友好输出:
nums=(1,2,3),@new=(...);
用其变量名称取代了VARN。其作者发布了Data::Dumper::Names模块,与Data::Dumper功能一致,但引入了上述功能。
参考文档:Data::Dumper