in reply to Recursively change perl-data-structure

The easy way would be to use the aliasing of @_:

... elsif ( ref($data) eq '' ) { $_[0] = $subref->($data); } ...

... but that will of course break as soon as you change the order or arguments of change_all_elements.

The other approach would be to rewrite your code so that it does the assignment and check one level higher:

... if ( ref($data) eq 'HASH' ) { for my $k ( keys %$data ) { if( ref $data->{$k} ) { change_all_elements( $data->{$k}, $subref ); } else { $data->{$k} = subref->($data->{$k}); } } } ... # and the same for arrays

Replies are listed 'Best First'.
Re^2: Recursively change perl-data-structure
by richard90522 (Novice) on Nov 02, 2022 at 15:58 UTC
    Helpful and working, the old $_[0] changed because it's a pointer to the real data:
    $_[0] = $subref->($data);
    Thanks!