in reply to Hash reference as a parameter

Here is an example. You pass the parmeter as a hash reference and access it as a hash reference in the function.

func({1=>'a', 2=>'a', 'a'=>99, 'f'=>'234', 'xyzzy'=>'Grue be here.', 'foo'=>'geww',}); sub func { my $hashref = shift; print "an item from hash: $hashref->{'xyzzy'}\n"; printf("another item from hash: %s\n", $hashref->{'foo'}); }

Hazah! I'm Employed!

Replies are listed 'Best First'.
Re^2: Hash reference as a parameter
by InfiniteSilence (Curate) on Sep 12, 2005 at 20:49 UTC
    I think you missed the @_ added to the end of the hash, which would effectively OVERWRITE the default values with the ones passed in.

    Celebrate Intellectual Diversity

Re^2: Hash reference as a parameter
by ramya2005 (Scribe) on Sep 12, 2005 at 20:34 UTC
    How to handle the optional parameters in this case?

      Oh, From your response, it sounds like you are asking about a specific function that is part of a package? XML::Twig perhaps?

      Adding that to the question will probably get you a better answer... Your question sounded like you were having trouble passing a has and using it within a function.

      Hazah! I'm Employed!

        I do not need a functionality of XML::Twig. I want to combine the functionality of default(Named) parameters with passing hash refs.