in reply to Recursively change perl-data-structure
sub recursive_mult_by_100() { if (!ref) { $_ *= 100 } elsif (ref eq 'ARRAY') { recursive_mult_by_100 for @$_ } elsif (ref eq 'HASH') { recursive_mult_by_100 for values %$_ } else { die "Unhandled data type ".ref; } } recursive_mult_by_100 for $deep;
This takes advantage of the feature where Perl aliases things to $_ during a for loop, so modifying $_ modifies the underlying thing, and where ref operates on $_ by default if you don't give it a regular parameter.
I say it's not the best advice because it isn't the most readable code, nor a standard pattern that people expect. Note that the () on the function stops it from accepting parameters, which helps you avoid the mistake of calling recursive_mult_by_100($data) and getting buggy results.
|
|---|