#rmap BLOCK $struct
#Takes a reference to an arbitrary struct and applies a given block of code to each of its scalar elements.
#This is basically a recursive version of the normal perl map function (so $_ is set to each element and can be modified to modify the original struct!).
#Good for cleaning up whitespace in data --> rmap {s/\s*//g} $system_data
#rmap {print $_} $system_data
sub rmap(&@)
{
my $code=shift;
foreach ( @_ )
{
my $ref = ref $_;
if ($ref eq 'HASH')
{
while (my($key,$val)=each %{$_})
{
$ref = ref $val;
$_->{$key}=&rmap($code,$val);
}
}
elsif ( $ref eq 'ARRAY' )
{
my $i;
for( $i=0 ; $i < scalar @{$_} ; $i++ )
{
@{$_}[$i]=&rmap( $code, @{$_}[$i] );
}
}
elsif ( $ref eq '' )
{
#It is just a normal scalar, so run the code block !
return $_ if &{$code}();
}
else
{
die("Unhandled type of reference : '$ref'");
}
return $_;
}
}
####
sub getSystemData
{
[... snip ...]
return $system_data;
}
####
sub getSystemData
{
[..snip..]
rmap {print} $system_data;
return $system_data;
}
####
print ref $system_data