Data::Dumper模块

Data::Dumper模块主要用途是:给出一个或多个变量,包括引用,以PERL语法的方式返回这个变量的内容。

比方说,这里有个很复杂的hash,数据结构很复杂,我想看看这个hash里面的内容。除了常见的方式(直接用print或者编历keys然后打印), 我们也可以使用Data::Daumper->Dump([\%hash])的形式。同时,模块中定义了很多的配置参数,让用户可以调整打印格式。

 

简单列举几个(具体参见perldoc),这些变量在模块

$Data::Dumper::Indent

这个设置打印的缩进格式,可以设置成0,1,2和3。用户可以自己尝试下。

$Data::Dumper::Terse

如果设置这个变量,则不打印变量的名字,只打印变量的内容。

$Data::Dumper::Maxdepth

不超过这个变量的限制深度,才打印变量的内容。

 

下面写个程序说明问题:

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my %people = (
    'name' => 'ray',
    'age' => 24,
    'sex' => 'man',
    'food' => ['egg', 'apple'],
);

# See Data::Dumper module to get the default vaule of the
# following module gobal variable. You can overwrite the
# default value to user defined one.

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 = 3; # 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])

 

ray@localhost perl]$ perl data_dumper.pl
Show perl hash, with pre-defined variable name
 and without maxdepth
%my_info = (
             'food' => [
                         #0
                         'egg',
                         #1
                         'apple'
                       ],
             'name' => 'ray',
             'sex' => 'man',
             'age' => 24
           );
Show perl hash, without pre-defined variable name
 and with maxdepth is 1
{
             'food' => 'ARRAY(0x91d68c4)',
             'name' => 'ray',
             'sex' => 'man',
             'age' => 24
           }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值