Good job!
I'm still going to recommend that you stop using prototypes. Not using prototypes is a lesson I learned from my wise fellow monks. This article is a "must read" on the subject.
You can also simplify your code somewhat. The following are just suggestions, of course (ultimately you should do what makes the most sense for you), but here are some things to consider ...
Why not pull all your arguments out of @_ in one fell swoop, with my ($hash, @row) = @_?
When I see an if { ... } else { ... } where the first block is fairly long and the second very short, I often like to reverse them, so the short block is first. Then you can often get rid of the else clause as well. So instead of:
if(ref($hash) eq 'HASH'){
my $header = $hash->{'header'};
while(my ($key, $value) = each(%$hash)){
push(@stack, unpack_row($value, (@row, $key)));
}
}
else {
return([@row, $hash]);
}
I prefer the more "balanced" looking:
if(ref($hash) ne 'HASH'){
return([@row, $hash]);
}
my $header = $hash->{'header'};
while(my ($key, $value) = each(%$hash)){
push(@stack, unpack_row($value, (@row, $key)));
}
Then I noticed you're not using $header, so you can remove that line.
Additionally, you can make the construction of @stack within the loop into a map statement, and then throw away @stack entirely, and just return the result from map.
Here's the entire program (with a few other simplifications):
use strict;
use warnings;
use Data::Dumper;
sub unpack_row {
my ($hash, @row) = @_;
return [ @row, $hash ] unless (ref $hash eq 'HASH');
return map { unpack_row($hash->{$_}, @row, $_) } keys %$hash;
}
my $hash = {
foo => { bar => { bat => 5, bird => 2 } },
goo => { tab => { bird => 2 } },
};
print Dumper($hash);
print Dumper(unpack_row($hash, ()));
Isn't that a bit easier to follow?
s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
|