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

I am trying to call a function referenced in a hash based on a parameter sent as a form field. I've gotten the function call portion to work, but I'm having difficulties with passing the needed parameters. Here's what I have so far:
my %functions = ( "setup" => { function => \&get_events, args => [\%formdata, $filelocation] }, );
Now suppose that $formdata{'action'}=setup. If I wanted to call the function &get_events while passing the parameters stored in args, how would I call this? This is what I have so far, but it's not working. I probably have a bracket or paren swapped around or something.
$functions{$formdata{'action'}}->{function}->($functions{$formdata{'ac +tion'}}->{args});
Any help is appreciated.

Eric

Replies are listed 'Best First'.
Re: passing hash elements as parameters
by hiseldl (Priest) on Oct 09, 2002 at 18:42 UTC

    You have to dereference the array ref and then you will have access to your 'args.' Look at get_events for an example how to dereference.

    #!perl -w use Data::Dumper; $action = 'setup'; $filelocation = '.'; %formdata = ( 'one' => 123, 'two' => 234, ); my %functions = ( "setup" => { function => \&get_events, args => [\%formdata, $filelocation] }, ); $functions{ $action }->{function}-> ( $functions{ $action }->{args} ); sub get_events { my $ref = shift; my $hashref = $ref->[0]; # get \%formdata my $fileloc = $ref->[1]; # get $filelocation print "F:$fileloc\n"; print Dumper( $hashref ); print $hashref->{one},$/,$hashref->{two},$/; }

    --
    hiseldl
    What time is it? It's Camel Time!

      Okay, I think I see what my mistake was. In my original sub get_events I had the line my ($formdata, $location) = @_ in order to grab the vars being passed. I think my mistake was that the individual vars weren't being passed, but rather an array containing the references to the hash and the scalar.

      Tell me if I'm right from your above code:
      # get the reference to the array [args] my $ref = shift; # 1st element in array = reference to %formdata # 2nd element in array = scalar $filelocation my $hashref = $ref->[0]; my $fileloc = $ref->[1];
      Am I understanding this correctly?

          I think my mistake was that the individual vars weren't being passed, but rather an array containing the references to the hash and the scalar.

        You're mostly correct; an array reference containing the elements are being passed, not just a plain array. Here's a breakdown that might help:

        args => [ # start an array reference \%formdata, # pass a hash reference $filelocation # pass a scalar ] # end an array reference # if you are passing this as an argument to your sub, you # need to dereference it sub mysub { $ref = shift; # now we have the array ref # use array reference notation to access the elements $first_element = $ref->[0]; }

        --
        hiseldl
        What time is it? It's Camel Time!

        You are right. You weren't passing an array but a reference to an array, so:
        ($formdata,$location)=@_;
        would result in $formdata containing an arrayref and $location being undefined.

        CU
        Robartes- just butting in :)

      Perfect! I used a modified version of your code in sub get_events and it worked. Now I just have to work on understanding "why". Thanks for the help.
Re: passing hash elements as parameters
by runrig (Abbot) on Oct 09, 2002 at 18:32 UTC
    If your function expects a reference to an array containing a hash reference and a scalar, then its ok, otherwise you will need to dereference your args:
    @{$functions{setup}{args}}
      Having a reference to a hash and a scalar passed to the functiong is fine, so long as I can access the data stored in both of the variables. You say "then its ok"...do you mean my code should work as it is? I haven't gotten it to work so far though.