Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Adding a hash to a CGI param

by bradcathey (Prior)
on Jan 03, 2008 at 15:27 UTC ( #660213=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monasterians,

It seems like I've seen this done before, but I can't make this work now. I start by assigning a simple hash to a param (BTW, using CGI::Application). Later, I want to add another key-value pair to that param, so:

#EARLIER $self->param( 'sql' => \%sql ); #LATER $self->param( 'sql' => { 'page_id' => 45 } );

This, of course, clobbers the original param and I end up with just the new pair. I suppose I could grab all the pairs back into a hash and then add to it:

my %sql = %{ $self->param('sql') }; $sql{'page_id'} = 45; $self->param( 'sql' => \%sql );

But this seems a bit verbose. *Is* there a way to do this? What's the concept at work here? Thanks in advance.


—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Replies are listed 'Best First'.
Re: Adding a hash to a CGI param
by davidrw (Prior) on Jan 03, 2008 at 15:35 UTC
    I believe this will work:
    #EARLIER $self->param( 'sql' => \%sql ); #LATER $self->param( 'sql' )->{page_id} = 45;
    Where the concept is that the value of the 'sql' param is a hash reference, so you just need to retrieve that and work with it as if it you had done something like my $href= \%sql;
    In the line above, it simply deferences the return value of $self->param( 'sql' ) and sets the page_id key to 45.

      Yes, that worked and I understand the 'concept'. Thanks. I think it might have been the hash reference that was throwing me. I suppose if I were storing an array in the param, I could push.


      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
        I suppose if I were storing an array in the param, I could push .

        Assuming that what was returned by  $self->param('foo') was an array reference and not a hash ref, you could indeed say:

        push @{ $self->param('foo') }, $element, $anotherelement, $etc;

        And, in general, treat the expression as you would any array ref:

        $self->param('foo')->[$n] = 42;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2021-06-22 16:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (106 votes). Check out past polls.

    Notices?