in reply to Hash reference as a parameter

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); }

Replies are listed 'Best First'.
Re^2: Hash reference as a parameter
by ramya2005 (Scribe) on Sep 12, 2005 at 21:36 UTC
    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); }
        Thanks for your time. But this is not the solution that I wanted!

        My idea is very simple. If the user creates the input parameters as a hash then he can alter the hash at any time and call the function. This is what I mean by the following statements.
        my %input = {foo => 'bar', oof => 'wha'}; my_sub(\%input); $input{foo} = 'bar1'; my_sub(\%input);