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

Re^2: Building data structures from CGI params

by fullermd (Priest)
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

Replies are listed 'Best First'.
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, 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 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://874177]
[LanX]: he ... FB is banning the wistleblower ... (shoot the messenger)
[marto]: #deletefacebook
[LanX]: well try to find my profile xD
[marto]: derp
[marto]: I've never had an account, and wouldn't visit the site
LanX having only a fake profile is hard if a flirt asks to befriend you
[erix]: "surveillance companies" have been rebranded as "social media"
[LanX]: I'm using whatapp though :-/
[LanX]: Snowden remins me of Evan Spiegel (... start of a new conspiracy theory)
[LanX]: yes! The same person!

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (11)
As of 2018-03-19 15:24 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (240 votes). Check out past polls.