Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: passing hash elements as parameters

by hiseldl (Priest)
on Oct 09, 2002 at 18:42 UTC ( [id://204008]=note: print w/replies, xml ) Need Help??


in reply to passing hash elements as parameters

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!

Replies are listed 'Best First'.
Re: Re: passing hash elements as parameters
by emilford (Friar) on Oct 09, 2002 at 19:14 UTC
    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 :)

Re: Re: passing hash elements as parameters
by emilford (Friar) on Oct 09, 2002 at 19:03 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://204008]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-26 04:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found