Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Passing multiple hashes?

by Monolith-0 (Beadle)
on Dec 11, 2001 at 05:30 UTC ( #130834=perlquestion: print w/replies, xml ) Need Help??

Monolith-0 has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to pass two hashes to a subroutine, but it seems I cannot do that..

Here's some of the code I'm working with:
sub register { my (%error, %FORM) = @_; ### <- note this line my $CGI = new CGI; my $template = HTML::Template->new( filename => 'register.tmpl', path => $cfg{'template_path'} ); $template->param( ERROR => $error{'any'}, ERROR_NAME => $error{'name'}, USERNAME => $FORM{'username'}, ERROR_NAME_TAKEN => $error{'name_taken'}, ERROR_PASS => $error{'pass'}, ERROR_PASS2 => $error{'pass2'}, ERROR_EMAIL => $error{'email'}, EMAIL => $FORM{'email'} ); print $CGI->header; print $template->output; # temp debug stuff print ".."; print $FORM{'username'}; print $error{'name'}; } sub register_submit { # Get the stuff submitted from the form read(STDIN, my $buffer, $ENV{'CONTENT_LENGTH'}); # Split the name-value pairs my @pairs = split(/&/, $buffer); my %FORM; foreach my $pair (@pairs) { my ($name, $value) = split(/=/, $pair); # Remove plus signs and decode %-encoding $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/<!--(.|\n)*-->//g; $FORM{$name} = $value; } # Check for errors in the data my %error; if (!$FORM{'username'}) { $error{'name'} = 1; $e +rror{'any'} = 1; } if (!$FORM{'password'}) { $error{'pass'} = 1; $e +rror{'any'} = 1; } if ($FORM{'password'} ne $FORM{'pass2'}) { $error{'pass2'} = 1; $e +rror{'any'} = 1; } if (!$FORM{'email'}) { $error{'email'} = 1; $e +rror{'any'} = 1; } if ($error{'any'}) { &register(%error,%FORM); ### <- note this line } else { ## Go on doing the stuff it's supposed to do } }

I have found that the subroutine 'register' gets the first hash alright, but it doesn't pick up the second one. I can see why this might not work, but my question is if there is any way I can get it to work. If not, then could I still get some suggestions on what I might do otherwise to achive the desired effect?

- Monolith

Replies are listed 'Best First'.
Re: Passing multiple hashes?
by VSarkiss (Monsignor) on Dec 11, 2001 at 05:37 UTC

    Perl passes everything to your subroutine in the @_ array, so it can't tell where one hash ends and the other begins. Everything will end up in your first hash, as you've found.

    TIMTOWTDI, but probably the easiest is to pass references instead:

    sub register { my ($eref, $fref) = @_; my %error = %$eref; my %FORM = %$fref; # ... } # # Call it like so: # register(\%error, \%FORM);

    HTH

      Thanks, that's just what I needed. Unfortunatly, when I try it exactly that way, I occationally get the error:

      Can't use an undefined value as a HASH reference at A_user.pm line 54.
      Where line 54 is:
      my %error = %$eref;
      I managed to avoid this by just using the refs directly:
      $$error_ref{'any'};

      - Monolith

Re: Passing multiple hashes?
by Zaxo (Archbishop) on Dec 11, 2001 at 06:54 UTC

    As long as you use CGI.pm anyway, why not let it parse the form for you? It's probably an error to make a new CGI object inside the register sub. Better to have a file scoped:

    my $cgi = new CGI;
    Now your form is already parsed.

    After Compline,
    Zaxo

Re: Passing multiple hashes?
by kwoff (Friar) on Dec 11, 2001 at 05:52 UTC
    A hash is something like an array. Say you have
    %foo = ( 'bar' => 'baz', 'qux' => 'quux', );
    Then do register(%foo), and now the @_ variable inside register might be like this:
    $_[0] = 'bar'; $_[1] = 'baz'; $_[2] = 'qux'; $_[3] = 'quux';

    It's almost like you called it with register('bar' => 'baz', 'qux' => 'quux'). And remember that => is just a comma operator which quotes the left argument, so that's like doing: register('bar', 'baz', 'qux', 'quux').

    Note how my assignment to %foo above is like assigning a list ('bar' => 'baz', 'qux' => 'quux') to %foo. So, what happened when you did

    my (%error, %FORM) = @_;
    is the elements of array @_ were assigned to %error, where %error gobbles up all the list passed to it because it doesn't know where to stop.

Re: Passing multiple hashes?
by chip (Curate) on Dec 11, 2001 at 06:48 UTC
    So why not use CGI::param() instead of the dance of substitutions in &register?

        -- Chip Salzenberg, Free-Floating Agent of Chaos

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2023-02-01 19:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (12 votes). Check out past polls.

    Notices?