in reply to hash ref 101

Parameters passed to a function get flattened into a list, which is placed in @_. So, in your code above at line 11: @_ = qw(uno one dos two tres three).

Basically, you aren't passing a hash reference. You are trying to pass a hash directly. Hashes can be converted to lists easily, so that's what happens.

There are two ways to do what you want to do, off the top of my head. (With minimal modification to your code.) First is to convert the list back into a hash:

sub hash { my %param = @_; # I hope I'm remembering this right... print $param{uno}, "\n"; }

The second, (and in my opinion better, especially if you are going to be working with objects, where another parameter will be automatically passed) is to actually pass a hashref:

hash( { uno => 'one', dos => 'two', tres => 'three', } ); sub hash { my ($param) = @_; print $param->{uno}, "\n"; }

Replies are listed 'Best First'.
Re^2: hash ref 101
by AnomalousMonk (Archbishop) on Dec 17, 2008 at 14:41 UTC
    Also maybe take a look at the Perl Monks tutorial Data Type: Hash and the many links therefrom, and at some of the tutorials and FAQs at perl.org

      Thanks for all your posts

      I think I will take the solution from eleron because this function will be used by more people that is not experience with Perl programming and I don't want to confuse them.

      I am planning to use an OO module, right now I have a simple module but my design requires and object to keep track on several variables.

      Regarding the OO implementations with arrays I don't think that will be too much problem (I hope) because I have seen in many modules the implementation in this way:

      my $object = new Object(); $object->hash( uno => 'one', dos => 'two', tres => 'three', );
      And in the module I will have
      sub hash { my $class = shift; my $param = {@_}; $class->{uno} = $param->{uno}; print $class->{uno}, "\n"; }

      So this way I can save in the object the variable with value "one".