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

Dear PerlMonks:

I am passing a hash reference while calling a function named StartCapture. I am making use of named optional parameters in the function.

How to receive and read the values of the hash reference in addition to the use of Optional Parameters?
code
sub StartCapture { my %args = ( results => 'c:/SomeDefault.txt', @_ ); print "results => $args{results}"; } StartCapture( { twigs => {filter => $filterTwig, capture => $captur +eTwig}, hashes => {filter => \%filterHash, capture => \%capt +ureHash}, files => {filter => 'filter.txt', capture => 'captur +e.txt'}, results => 'c:/temp.txt' } );
FYI
FYI: I posted a similar question asked in the following thread
http://www.perlmonks.com/?node_id=487518
The solution was to pass the parameters as a list instead of hash ref. But I want to pass hash reference for some reason.

Replies are listed 'Best First'.
Re: Hash reference as a parameter
by graff (Chancellor) on Sep 12, 2005 at 21:43 UTC
    Did you get the answer you wanted about the issue of "optional" parameters? Is this the kind of thing you're after?
    # caller: StartCapture( { twigs => {filter => $filterTwig, capture => $captureTw +ig}, hashes => {filter => \%filterHash, capture => \%captur +eHash}, files => {filter => 'filter.txt', capture => 'capture. +txt'}, results => 'c:/temp.txt' } ); # ... sub StartCapture { my $hashref = shift; $$hashref{results} ||= 'c:/SomeDefault.txt'; # ... }
    Using the "||=" operator, the LHS (hash element in this case) will be assigned the RHS value iff the original LHS evaluates to false. (I believe that the most recent perl version also has a "//=" operator, which makes the assignment only if the LHS is undef, as opposed to being zero or an empty string.)
      Thanks. My optional parameters problem is solved, but I learned a new way of doing it by seeing your post
Re: Hash reference as a parameter
by osunderdog (Deacon) on Sep 12, 2005 at 20:32 UTC

    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!

      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

      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!

Re: Hash reference as a parameter
by InfiniteSilence (Curate) on Sep 12, 2005 at 20:46 UTC
    I could swear that I've seen this problem posted before...

    At any rate, if you modify the call by removing the brackets like so it will work:

    StartCapture( twigs => {filter => $filterTwig, capture => $captur +eTwig}, hashes => {filter => %filterHash, capture => %captur +eHash}, files => {filter => 'filter.txt', capture => 'captur +e.txt'}, results => 'c:/temp.txt' );

    Result:

    perl www.pl results => c:/temp.txt C:\Temp>

    It is important to use Data::Dumper or the like to analyze things that are being passed when you are doing things like this. For instance, how do you know you want the entire %captureDefaultHash, rather than just a bunch of elements in it?

    Celebrate Intellectual Diversity

Re: Hash reference as a parameter
by saberworks (Curate) on Sep 12, 2005 at 21:08 UTC
    I don't understand why you want a hashref, but it's really not that much different than your last node:
    #!/usr/bin/perl use strict; use Data::Dumper; my_sub({foo => 'bar', oof => 'wha'}); sub my_sub { my $ref = shift; my %args = ( foo => 'oop', pah => 'meh', oof => 'off', %{$ref} ); warn Dumper(\%args); }
      I am a beginner in Perl. Please correct me if I am wrong!
      I want to do something like the following, so that I can alter only some values of the hash and call the function again and again. I am not sure whether the same thing can be done using a 'list' as suggested in the previous post.
      #!/usr/bin/perl use strict; use Data::Dumper; my %input = {foo => 'bar', oof => 'wha'}; my_sub(\%input); $input{foo} = 'bar1'; my_sub(\%input); sub my_sub { my $ref = shift; my %args = ( foo => 'oop', pah => 'meh', oof => 'off', %{$ref} ); warn Dumper(\%args); }
        Hrm, interesting question. It seems more like you want a global hash which gets reset to the default values each time the function is called with the exception of the values you pass in. I still don't think a reference is the way to go, but if it works, meh. What about this:
        #!/usr/bin/perl use strict; use Data::Dumper; my %hash = (); my_sub(foo => 'bar', oof => 'wha'); my_sub(pah => 'eek'); sub my_sub { %hash = ( foo => 'oop', pah => 'meh', oof => 'off', @_ ); warn Dumper(\%hash); }