请看Data::Dumper。
比如,这个微小的脚本:
use strict;
use Data::Dumper;
my(%hash);
$hash{key1} = [ 1, "b", "c" ];
$hash{key2} = [ 4.56, "g", "2008-12-16 19:10 -08:00" ];
print Dumper(\%hash);
生成此输出,可以清楚地进行编辑:
$VAR1 = {
'key2' => [
'4.56',
'g',
'2008-12-16 19:10 -08:00'
],
'key1' => [
1,
'b',
'c'
]
};
它还可以被用于将数据读回程序中进行评估。
扩展示例以显示读入和打印输出...请注意,代码分为两个主要块,唯一在这两个块之间共享的变量是文件名。
use strict;
use Data::Dumper;
use FileHandle;
my $file = "data.out";
{
my(%hash);
$hash{key1} = [ 1, "b", "c" ];
$hash{key2} = [ 4.56, "g", "2008-12-16 19:10 -08:00" ];
my $str = Data::Dumper->Dump([ \%hash ], [ '$hashref' ]);
print "Output: $str";
my($out) = new FileHandle ">$file";
print $out $str;
close $out;
}
{
my($in) = new FileHandle "<$file";
local($/) = "";
my($str) = <$in>;
close $in;
print "Input: $str";
my($hashref);
eval $str;
my(%hash) = %$hashref;
foreach my $key (sort keys %hash)
{
print "$key: @{$hash{$key}}\n";
}
}
那个脚本的输出结果是:
Output: $hashref = ;
Input: $hashref = ;
key1: 1 b c
key2: 4.56 g 2008-12-16 19:10 -08:00