Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Building data structures from CGI params

by fullermd (Curate)
on Nov 29, 2010 at 04:10 UTC ( #874177=note: print w/ replies, xml ) Need Help??


in reply to Re: Building data structures from CGI params
in thread Building data structures from CGI params

Ah - now I see, you want to do some "structured" parameters. No, I'm not aware of something like that, but if you go for dots as delimiters, [...]

Unfortunately (fortunately ;) this is perl, so we have to have multiple delimiters to be able to support both hashes and arrays. That makes things a little more complicated (not to mention that pesky error checking).

Thanks to you (and Anonymous below) for the code drops. I wasn't really looking for "help me write this code" so much as "help me find the module that surely already exists to do it", but I'm continually impressed at the willingness of the Monks to go that extra mile.

Sadly, none of us were successful at finding the existing module. I'm still shocked by that. Left me no choice but to go solve it.

So I did, and the next time someone asks, we can tell them CGI::Struct.

Annoyingly, I just got a mail about a test failure on 5.6.2. At a glance, it looks like it's just a problem 5.6 has with the test, not an actual code issue. Hrumph. Now I need to decide whether I want to fight through a 5.6 install somewhere to fix it...


Comment on Re^2: Building data structures from CGI params
Re^3: Building data structures from CGI params
by Anonymous Monk on Jul 27, 2012 at 09:36 UTC

    You forgot how CGI::Vars works, https://metacpan.org/module/CGI::Struct#Auto-arrays doesn't account for it

    #!/usr/bin/perl -- use strict; use warnings; use Data::Dump; use CGI; use CGI::Struct; my $q = CGI->new('row[]=row;row[]=row;row[]=row;row[]=your boat'); my %qVars = $q->Vars; my @errors; my $struct = build_cgi_struct \%qVars, \@errors; dd $q, \%qVars, $struct, \@errors; ### WORKAROUND for stupid Vars (must've written this line 100 times) %qVars = map { $_ => [ $q->param($_) ] } $q->param; dd \%qVars; $struct = build_cgi_struct \%qVars, \@errors; dd $struct, \@errors; __END__ ( bless({ ".charset" => "ISO-8859-1", ".fieldnames" => {}, ".iterator" => 2, ".parameters" => ["row[]"], "escape" => 1, "param" => { "row[]" => ["row", "row", "row", "your boat"] +}, "use_tempfile" => 1, }, "CGI"), { "row[]" => "row\0row\0row\0your boat" }, { row => ["row\0row\0row\0your boat"] }, [], ) { "row[]" => ["row", "row", "row", "your boat"] } ({ row => ["row", "row", "row", "your boat"] }, [])

      I'm not sure what you mean. { row => ["row", "row", "row", "your boat"] } looks like the "right" answer to me...

      What were you expecting?

        I'm not sure what you mean.  { row => ["row", "row", "row", "your boat"]  }   looks like the "right" answer to me... What were you expecting?

        That line is generated by the workaround

        This is Vars output

        { "row[]" => "row\0row\0row\0your boat" },

        And this is what your module does with vars output

        { row => ["row\0row\0row\0your boat"] },

        This is the bug, it forgot to split on \0, because Vars joins on \0

        or better still it should simply accept a CGI.pm compatible object (one with param method, whether its CGI/CGI::Simple/CGI::Lite.....) and use the Vars workaround on it

        Do you get it now?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-12-28 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (179 votes), past polls