Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

:-delimited lists in CGI parameters

by narse (Pilgrim)
on Mar 29, 2004 at 20:57 UTC ( [id://340752]=perlquestion: print w/replies, xml ) Need Help??

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

I am having some problems dealing with a colon delimited list inside a cgi script. My lists will not show up correctly inside a hidden() input within the browser. Here is a snippet:
defined $param{selected} ? ( $param{selected} .= ":$file" ) : ( $param{selected} = $file ); join ( "", map { hidden ( -name => $_, -default => $param{$_}, ) } keys %param )
Within the script, hidden() seems to forget everything after and including the first :. If I change the hidden() to Dumper, the correct stuff is displayed in the browser window, showing that $file is properly defined. Running the script from the comamnd line also returns the bad results so it isn't specifically a browser problem. What is strange is that if I run:
perl -le 'use CGI qw/ :standard /; print hidden( -name => "foo", -defa +ult => "foo:bar" )'
hidden() seems to do the right thing.

Is there something I am missing in this snippet? Is there a better way to pass a list within cgi arguments?

Thanks in advance.

Replies are listed 'Best First'.
Re: :-delimited lists in CGI parameters
by Ovid (Cardinal) on Mar 29, 2004 at 21:10 UTC

    Is there a better way to pass a list within cgi arguments?

    Don't forget that you can have a paramater repeated multiple times. For example, if you have the query string color=red;color=blue;color=green, you can read back all colors with a simple param call:

    my @colors = param('color');

    Cheers,
    Ovid

    New address of my CGI Course.

Re: :-delimited lists in CGI parameters
by PodMaster (Abbot) on Mar 29, 2004 at 21:14 UTC
    Huh? You're getting
    <input type="hidden" name="foo" value="foo:bar" />
    right? That's what you should be getting. What is it that you're expecting (How (Not) To Ask A Question)?

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Sorry, I should have included an example. If %param looks like:
      ( selected => "foo" )
      and $file = "bar"
      I would expect the larger snippet of code to give me something like this:
      <input type="hidden" name="selected" value="foo:bar">
      when what I get instead is:
      <input type="hidden" name="selected" value="foo">
      If I change the hidden() to a Dumper in order to show the contents of %param in place of the hidden input, the values are infact what I would expect. The reason I included the one liner is to demonstrate the inconsistancy.

      Ovid definately pointed out a better way that I hadn't thought of but I am still curious what the goofup is here. To get the full operation of the script means posting a lot of code and it its not something obvious, I'd rather just drop the issue and dtrt.
Re: :-delimited lists in CGI parameters
by eric256 (Parson) on Mar 29, 2004 at 22:09 UTC

    The effect you are seeing is cause by the fact that you are accessing the parameter hash directly. If you use the correct funtion instead if works great.

    use CGI qw/:standard/; $file = "bar"; defined param(selected) ? param(selected, param(selected) . ":$file") : param(selected , $file) ; print join ( "", map { hidden ( -name => $_, -default => $param{$_}, ) } param )
    Produces:
    C:\test>perl param.pl selected=food <input type="hidden" name="selected" value="food:bar" /> C:\test>perl param.pl <input type="hidden" name="selected" value="bar" />

    Its prob best to go with the right way instead though. Of course that all depends on your needs.


    ___________
    Eric Hodges
Re: :-delimited lists in CGI parameters
by tinita (Parson) on Mar 30, 2004 at 08:53 UTC
    there are some issues with your code:
    1. you're changing the hash %params. this does not change the cgi parameter hash. (i would expect, though, that changing $params, if it was a tied hash reference, would change the behaviour, but it doesn't). so the default is still the value with which the script was called. only using the param() method helps here. (can this be considered as a bug in CGI.pm?)
    2. setting the -default parameter does not change the value unless you also use -override => 1

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 12:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found