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

Hi, Please any one tell me how to pass a hash array as argument to an function, that is in one module.

Replies are listed 'Best First'.
Re: Pass a hash array to module
by almut (Canon) on Jun 01, 2010 at 08:50 UTC

    Normally, you'd pass a reference to the data structure, unless you want it flattened.

    # for hashes my %hash = ( ... ); my $hashref = { ... }; # pass references: MyModule::function(\%hash); MyModule::function($hashref); # pass keys/values as a flattened list: MyModule::function(%hash); MyModule::function(%$hashref); # for arrays my @array = ( ... ); my $arrayref = [ ... ]; # pass references: MyModule::function(\@array); MyModule::function($arrayref); # pass elements as a flattened list: MyModule::function(@array); MyModule::function(@$arrayref);

    When you flatten the hash/array, you'll get its entries as separate elements in the function's @_. Otherwise, there will only be one element in @_ holding the reference.

Re: Pass a hash array to module
by moritz (Cardinal) on Jun 01, 2010 at 08:51 UTC
    There are hashes and arrays, but no "hash arrays".

    And you can pass them either "flat" or by reference:

    # flat: mysub(@array); mysub(%hash); # by reference: mysub(\@array); mysub(\%hash);

    See also: perlintro, perlsub, perldata, perlreftut

      my %data; my $myObj = new test::mod1(); %data = $myObj->prepare(%data); ------------------------- package test::mod1; sub new { my ($class_name, $dql) = @_; my $internalData = {dql => $dql}; bless($internalData, $class_name); return $internalData; } sub prepare { my ($internalData, %record) = @_; return %record; }
      this is working... but i don't know how.. constructor how works ???
Re: Pass a hash array to module
by Gangabass (Vicar) on Jun 01, 2010 at 08:51 UTC