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

Hi all,
I need to pass an array of values, to be accessed by another module.
I use the following code,
This does not give me desired result(all values from 1st module).
I need to incorporate 2 such modules(like 1st) into 3rd.
module1 ..... ..... <some code> sub passMe{ my @data=(); push(@data, { %params, %extend_params, %basics, %annotations, %navigator, position => 'annotations', sid => $sid, navigator_link => '<a id="expand_nav" onclick="Element.hi +de(\'expand_nav\');">[brief]</a>', annotations_detail_link => q|[<a id="sed" onclick="Element.hid +e('sed'); Element.show('hed'); show_experiment_details();"> experimen +t details</a>]|, template => $tmpl }); return \@data, $ID; } module2 sub report { my ($aa, $bb)= &passMe; while ( my ($key, $val) = each( %{$aa}) ) { while ( my ($key1, $value) = each(%{$key}) ) { print "$key1 => $value\n"; } } }

Please help me to solve this.

Thanking all in advance.
Sailas

Replies are listed 'Best First'.
Re: Problem with passing hashes between modules
by hipowls (Curate) on Mar 05, 2008 at 10:50 UTC

    You are returning a reference to an array whose content is a single hash reference. You then attempt to dereference the array as a hash. You probably mean something like this

    sub passMe { my %data = ( # note not '{' ... ); return \%data, $ID; }

    If you aren't getting what you expect then using Data::Dumper or YAML to print out the data structure will often help you find where the problem lies.

Re: Problem with passing hashes between modules
by Fletch (Bishop) on Mar 05, 2008 at 14:31 UTC

    Also note that you're going to get the contents of the subsidiary hashes flattened out into a list of key value pairs when you do { %params, %extended_params, ... } like that with whatever appears for any given key last winning (e.g. if %params and %navigator both had a key "foo" then the new hashref's value for "foo" would be what was in $navigator{"foo"}).

    (Which may be just what you want, but just calling attention to it.)

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Problem with passing hashes between modules
by hangmanto (Monk) on Mar 05, 2008 at 15:56 UTC
    I modified you subroutine 'PassMe' so that only references to a hash are pushed into the array @data. (I named it 'other_hash', but you should definitely rename it!). This should ease the maintenance.
    sub passMe { my @data = (); my %other_hash = ( position => 'annotations', sid => $sid, navigator_link => '<a id="expand_nav" onclick="Element.hide(\'expand_nav\');"> +[brief]</a>', annotations_detail_link => q|[<a id="sed" onclick="Element.hide('sed'); Element.show('h +ed'); show_experiment_details();"> experiment details</a>]|, template => $tmpl ); push( @data, { \%params, \%extend_params, \%basics, \%annotations, \%navigator, \%other_hash } ); return \@data, $ID; }

      Erm, so you've now given him a hashref which contains a reference to %extend_params keyed by the stringified reference to %params, a reference to %annotations keyed by the stringified ref to %basics, and likewise for the last two elements.

      Can you share what color the sky is on your planet where that's a useful data structure?

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.