roncoe2000 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks. Here's some simple code but the OOPS seems to be giving me trouble with eval statement. I want to dynamically change the foreach arguments below for different xml configs since the data arrays change structure.
this works hard coded obviously: foreach $e (@{$data->{channel}{item} }) but not for foreach $e (@{$data->(eval($y) }) where I want eval($y) = {channel}{item} (which is in the code below) or {item} with the appropriate data struc +ture . Got errors about not blessing the eval variable

Replies are listed 'Best First'.
Re: OOPS and Eval Statement on Strings
by NetWallah (Canon) on May 27, 2012 at 05:50 UTC
    Do you realize that the pointy-paren in
    $data->()
    is a subroutine call ?

    The subroutine call is completely different from yout hashref-dereference in

    $data->{somekey1}{somekey2}
    What you seem to be looking for (It is unclear from your description) is something like:
    $y='{channel}{item}'; foreach my $e (@{eval("\$data->$y")}){...

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

Re: OOPS and Eval Statement on Strings
by Anonymous Monk on May 27, 2012 at 08:24 UTC