in reply to subroutine memory variable scope
You're maybe looking for something like this?
use strict; use warnings; sub analyse_user_inputs { my $hash_ref = shift @_; my @the_inputs; foreach ( keys %{$hash_ref} ) { push @the_inputs, $_; if (ref $hash_ref->{$_} eq 'HASH') { push @the_inputs, @{ analyse_user_inputs($hash_ref->{$_}) +}; } elsif (ref $hash_ref->{$_} eq 'ARRAY') { foreach my $array_element (@{$hash_ref->{$_}}){ push @the_inputs, @{ analyse_user_inputs($array_elemen +t) }; } } } return(\@the_inputs); } my $data = { foo => 1, bar => [ { xyzzy => 'magic', } ], baz => { quux => 123, } }; use Data::Dumper; print Dumper(analyse_user_inputs($data));
No need to play funny tricks with variable scoping. Just take the results of your recursive calls and push them onto @the_inputs.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: subroutine memory variable scope
by austinj (Acolyte) on Dec 13, 2012 at 17:41 UTC | |
by tobyink (Canon) on Dec 13, 2012 at 18:29 UTC | |
by austinj (Acolyte) on Dec 13, 2012 at 18:33 UTC | |
by tobyink (Canon) on Dec 13, 2012 at 19:49 UTC |