我正在学习Perl。我有一个包含对哈希和数组的引用的Perl哈希表。这些哈希和数组可能反过来包含对其他哈希/数组的引用。
我编写了一个子例程来递归解析哈希并以适当的缩进打印它们。虽然该例程按预期工作,但我的讲师并不认为以下代码的可读性和优雅性。
我非常希望在此处得到Perl专家的意见,以优化以下代码。
这是我的完整代码片段。
# Array of Arrays
$ref_to_AoA = [
[ "fred", "barney" ],
[ "george", "jane", "elroy" ],
[ "homer", "marge", "bart" ],
];
#Array of Hashes
$ref_to_AoH = [
{
husband => "barney",
wife => "betty",
son => "bamm bamm",
},
{
husband => "george",
wife => "jane",
son => "elroy",
},
];
# Hash of Hashes
$ref_to_HoH = {
flintstones => {
husband => "fred",
pal => "barney",
},
jetsons => {
husband => "george",
wife => "jane",
"his boy" => "elroy", # Key quotes needed.
},
simpsons => {
husband => "homer",
wife => "marge",
kid => "bart",
},
};
# Hash which contains references to arrays and hashes
$finalHash = {
'arrayofArrays' => $ref_to_AoA,
'arrayofHash' => $ref_to_AoH,
'hashofHash' => $ref_to_HoH,
};
$string = str($finalHash);
print "$string\n";
#------------------------------------------------------------------
sub str {
my $hash = shift;
my ($space, $newline, $delimiter) = @_;
$space = "" unless (defined $space);
$newline = "\n\n\n" unless (defined $newline);
$delimiter = "\n--------------------------------------------" unless (defined $delimiter);
my $str = "";
for (sort keys %{$hash}) {
my $value = $hash->{$_};
$str .= "$newline$space$_ == $value$delimiter";
$str .= recurseErrors($value,$space);
}
$str;
}
#------------------------------------------------------------------
sub recurseErrors {
my $str;
my ($value,$space) = @_;
my $ref = ref $value;
if ($ref eq 'ARRAY') {
my $i = 0;
my $isEmpty = 1;
my @array = @$value;
$space .= "\t";
for my $a (@array) {
if (defined $a) {
$isEmpty = 0;
$str .= "\n$space$_\[$i\] :";
$str .= recurseErrors($a,$space);
}
$i++;
}
$str .= "= { }" if ($isEmpty);
} elsif ($ref eq 'HASH') {
$space .= "\t";
for my $k (sort keys %$value) {
if ( ( ref($value->{$k}) eq 'HASH') || (ref $value->{$k} eq 'ARRAY') ) {
my $val = $value->{$k};
$str .= "\n\n$space$k == ";
$str .= "$val";
}
else {
$str .= "\n$space$k == ";
}
$str .= recurseErrors($value->{$k},$space);
}
# we have reached a scalar (leaf)
} elsif ($ref eq '') {
$str .= "$value";
}
$str
}
#------------------------------------------------------------------
输出:
arrayofArrays == ARRAY(0x9d9baf8) -------------------------------------------- arrayofArrays[0] : arrayofArrays[0] :fred arrayofArrays[1] :barney arrayofArrays[1] : arrayofArrays[0] :george arrayofArrays[1] :jane arrayofArrays[2] :elroy arrayofArrays[2] : arrayofArrays[0] :homer arrayofArrays[1] :marge arrayofArrays[2] :bart
arrayofHash == ARRAY(0x9d9bba8) -------------------------------------------- arrayofHash[0] : husband == barney son == bamm bamm wife == betty arrayofHash[1] : husband == george son == elroy wife == jane
hashofHash == HASH(0x9da45f8) --------------------------------------------
flintstones == HASH(0x9d9bb48) husband == 弗雷德 pal == 巴尼
jetsons == HASH(0x9d9bbf8) his boy == 埃尔罗伊 husband == 乔治 wife == 简
simpsons == HASH(0x9d9bc48) husband == 霍默 kid == 巴特 wife == 玛吉