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

Does anyone can see what is wrong with this code?, I am looking how to make an OO module but I am having trouble passing arguments in a hash form like this:

mySub( opt1 => $value1 opt2 => $value2 opt3 => $value3 )
The concrete problem is the code below, I am expecting to print "one" but I get an error.
#!/usr/bin/perl use strict; hash( uno => 'one', dos => 'two', tres => 'three', ); sub hash { my $param = @_; print $param->{uno}, "\n"; }

When I execute that code I get the following error:

~/programming/perl/oo$ ./test.pl Can't use string ("6") as a HASH ref while "strict refs" in use at ./t +est.pl line 7.
Thanks

Replies are listed 'Best First'.
Re: hash ref 101
by moritz (Cardinal) on Dec 17, 2008 at 09:03 UTC
    my $param = @_;

    When you assign an array to a scalar, it gets the number of items in the array - not what you want. Try this instead:

    my %param = @_; print $param{uno}, "\n";
Re: hash ref 101
by lakshmananindia (Chaplain) on Dec 17, 2008 at 11:24 UTC
    use strict; use Data::Dumper; hash({ uno => 'one', dos => 'two', tres => 'three' }); sub hash { print Dumper @_; my ($param) = @_; print $param->{uno}, "\n"; }
      Also this way
      hash( uno => 'one', dos => 'two', tres => 'three', ); sub hash { my $param = {@_}; print $param->{uno}, "\n"; }
Re: hash ref 101
by DStaal (Chaplain) on Dec 17, 2008 at 13:57 UTC

    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"; }
      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".

Re: hash ref 101
by matrixmadhan (Beadle) on Dec 17, 2008 at 10:30 UTC
     my $param = @_;
    This should have been just as a hash ref
     my ($param) = @_;