Click here to get back home

Is there a better way to reset CGI object for AJAX POSTDATA?

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Is there a better way to reset CGI object for AJAX POSTDATA? Dodger 12-21-2007
Posted by Dodger on December 21, 2007, 6:10 pm
Please log in for more thread options
Currently I'm doing this:

use CGI();
my $cgi = new CGI;
$cgi = new CGI($cgi->param('POSTDATA'));

... in order to get the data an XMLHTTPRequest object sends through on
a POST request, because everything goes under the POSTDATA param as a
query string. AND can I just say... Yuck!

Anyway, my solution seems kludgy and I don't like it. It smells funny.

CGI.pm docs mention a class method restore_parameters() that works
like so:
open (IN,"test.in") || die;
restore_parameters(IN);
close IN;
...but there's no other mention of it, and no mention of an object
method. I can't stand using the functional interface for CGI.pm.

Isn't there any way to just reset an existing CGI object directly
without calling the constructor all over again?

--
Sean 'Dodger' Cannon

Posted by Dodger on December 21, 2007, 6:27 pm
Please log in for more thread options
> Currently I'm doing this:
>
> use CGI();
> my $cgi = new CGI;
> $cgi = new CGI($cgi->param('POSTDATA'));
>
> ... in order to get the data an XMLHTTPRequest object sends through on
> a POST request, because everything goes under the POSTDATA param as a
> query string. AND can I just say... Yuck!
>
> Anyway, my solution seems kludgy and I don't like it. It smells funny.
>
> CGI.pm docs mention a class method restore_parameters() that works
> like so:
> open (IN,"test.in") || die;
> restore_parameters(IN);
> close IN;
> ...but there's no other mention of it, and no mention of an object
> method. I can't stand using the functional interface for CGI.pm.
>
> Isn't there any way to just reset an existing CGI object directly
> without calling the constructor all over again?
>
> --
> Sean 'Dodger' Cannon

Actually, even that's not working right. Dammit.
According to the docs, I can create a new CGI object with a properly
formatted query string as an argument and *those* params are supposed
to be what it parses.

Quoth the docs:
"or from a properly formatted, URL-escaped query string:

$query = new CGI('dinosaur=barney&color=purple');"

But if I try this, and even if I am VERY careful to make sure it's in
scalar context:

use CGI();
my $cjax = new CGI;
my $POSTDATA = $cjax->param('POSTDATA');
undef $cjax;

my $cgi = new CGI($POSTDATA);

I still get back:
print join ', ', $cgi->param();
prints:
POSTDATA

So WTF??

Posted by Dodger on December 21, 2007, 6:29 pm
Please log in for more thread options
>
>
>
> > Currently I'm doing this:
>
> > use CGI();
> > my $cgi = new CGI;
> > $cgi = new CGI($cgi->param('POSTDATA'));
>
> > ... in order to get the data an XMLHTTPRequest object sends through on
> > a POST request, because everything goes under the POSTDATA param as a
> > query string. AND can I just say... Yuck!
>
> > Anyway, my solution seems kludgy and I don't like it. It smells funny.
>
> > CGI.pm docs mention a class method restore_parameters() that works
> > like so:
> > open (IN,"test.in") || die;
> > restore_parameters(IN);
> > close IN;
> > ...but there's no other mention of it, and no mention of an object
> > method. I can't stand using the functional interface for CGI.pm.
>
> > Isn't there any way to just reset an existing CGI object directly
> > without calling the constructor all over again?
>
> > --
> > Sean 'Dodger' Cannon
>
> Actually, even that's not working right. Dammit.
> According to the docs, I can create a new CGI object with a properly
> formatted query string as an argument and *those* params are supposed
> to be what it parses.
>
> Quoth the docs:
> "or from a properly formatted, URL-escaped query string:
>
> $query = new CGI('dinosaur=barney&color=purple');"
>
> But if I try this, and even if I am VERY careful to make sure it's in
> scalar context:
>
> use CGI();
> my $cjax = new CGI;
> my $POSTDATA = $cjax->param('POSTDATA');
> undef $cjax;
>
> my $cgi = new CGI($POSTDATA);
>
> I still get back:
> print join ', ', $cgi->param();
> prints:
> POSTDATA
>
> So WTF??


BTW -- yes, POSTDATA taken as a param does contain a properly
formatted string, like so:

POSTDATA: pixel=43:62:yellow:8&pixel=43:65:yellow:8&pixel=43:76:yellow:
8&pixel=43:84:yellow:8&pixel=45:94:yellow:8&pixel=45:98:yellow:
8&pixel=45:102:yellow:8...etc...

So the string is right. I should definitely not need to write this to
a file, and honestly if the docs are being wrong about this part, I
don't expect them to br right about reading from a file, either.

Posted by Mumia W. on December 22, 2007, 6:20 am
Please log in for more thread options
On 12/21/2007 05:29 PM, Dodger wrote:
>>
>>
>>
>>> Currently I'm doing this:
>>> use CGI();
>>> my $cgi = new CGI;
>>> $cgi = new CGI($cgi->param('POSTDATA'));
>>> ... in order to get the data an XMLHTTPRequest object sends through on
>>> a POST request, because everything goes under the POSTDATA param as a
>>> query string. AND can I just say... Yuck!
>>> Anyway, my solution seems kludgy and I don't like it. It smells funny.
>>> CGI.pm docs mention a class method restore_parameters() that works
>>> like so:
>>> open (IN,"test.in") || die;
>>> restore_parameters(IN);
>>> close IN;
>>> ...but there's no other mention of it, and no mention of an object
>>> method. I can't stand using the functional interface for CGI.pm.
>>> Isn't there any way to just reset an existing CGI object directly
>>> without calling the constructor all over again?
>>> --
>>> Sean 'Dodger' Cannon
>> Actually, even that's not working right. Dammit.
>> According to the docs, I can create a new CGI object with a properly
>> formatted query string as an argument and *those* params are supposed
>> to be what it parses.
>>
>> Quoth the docs:
>> "or from a properly formatted, URL-escaped query string:
>>
>> $query = new CGI('dinosaur=barney&color=purple');"
>>
>> But if I try this, and even if I am VERY careful to make sure it's in
>> scalar context:
>>
>> use CGI();
>> my $cjax = new CGI;
>> my $POSTDATA = $cjax->param('POSTDATA');
>> undef $cjax;
>>
>> my $cgi = new CGI($POSTDATA);
>>
>> I still get back:
>> print join ', ', $cgi->param();
>> prints:
>> POSTDATA
>>
>> So WTF??
>
>
> BTW -- yes, POSTDATA taken as a param does contain a properly
> formatted string, like so:
>
> POSTDATA: pixel=43:62:yellow:8&pixel=43:65:yellow:8&pixel=43:76:yellow:
> 8&pixel=43:84:yellow:8&pixel=45:94:yellow:8&pixel=45:98:yellow:
> 8&pixel=45:102:yellow:8...etc...
>
> So the string is right. I should definitely not need to write this to
> a file, and honestly if the docs are being wrong about this part, I
> don't expect them to br right about reading from a file, either.

I have no business trying to answer since I barely know what going on,
but I'll throw caution to the wind and answer anyway.

I see three potential problems: (1) rogue newlines (which were probably
not part of the original data), and (2) CGI.pm likes to concatenate
values for parameters with the same name, and (3) the ->new() method
can set and entire query object, but param() cannot. Also some POSTDATA
reformatting is probably in order:

#!/usr/bin/perl
use strict;
use warnings;
use CGI ();
use Data::Dumper;

my $POSTDATA = do {
local $/;
my $px = 1;
my $pd = <DATA>;
$pd =~ s/\s+//g;
$pd =~ s/pixel/'pixel'.($px++)/eg;
$pd;
};
my $cjax = new CGI($POSTDATA);
print Dumper();


__DATA__
pixel=43:62:yellow:8&pixel=43:65:yellow:8&pixel=43:76:yellow:
8&pixel=43:84:yellow:8&pixel=45:94:yellow:8&pixel=45:98:yellow:
8&pixel=45:102:yellow:8

Note, I used your original postdata as displayed in my
newsreader--newlines and all.

__HTH__



Posted by Mumia W. on December 22, 2007, 6:52 am
Please log in for more thread options
On 12/21/2007 05:29 PM, Dodger wrote:
>
> BTW -- yes, POSTDATA taken as a param does contain a properly
> formatted string, like so:
>
> POSTDATA: pixel=43:62:yellow:8&pixel=43:65:yellow:8&pixel=43:76:yellow:
> 8&pixel=43:84:yellow:8&pixel=45:94:yellow:8&pixel=45:98:yellow:
> 8&pixel=45:102:yellow:8...etc...
>
> So the string is right. I should definitely not need to write this to
> a file, and honestly if the docs are being wrong about this part, I
> don't expect them to br right about reading from a file, either.

I forgot that CGI.pm can handle multiple values for the same parameter
quite nicely:

#!/usr/bin/perl
use strict;
use warnings;
use CGI ();

my $POSTDATA = do {
local $/;
my $pd = <DATA>;
$pd =~ s/\s+//g;
$pd;
};
my $cjax = new CGI($POSTDATA);
my @pixels = $cjax->param('pixel');
print $_, "\n" for @pixels;

__DATA__
pixel=43:62:yellow:8&pixel=43:65:yellow:8&pixel=43:76:yellow:
8&pixel=43:84:yellow:8&pixel=45:94:yellow:8&pixel=45:98:yellow:
8&pixel=45:102:yellow:8


Similar ThreadsPosted
AppConfig: how to reset object? October 1, 2004, 9:20 am
CGI::AJAX and header June 16, 2006, 4:06 pm
Modules for AJAX framework April 2, 2007, 11:15 pm
DISCUSS: CPAN Namespace for AJAX August 3, 2005, 9:18 am
Looking for AJAX HTML Table Form Module August 15, 2007, 11:29 am
Can't find loadable object November 19, 2004, 1:05 pm
ANNOUNCE: Rose::DB::Object March 21, 2005, 4:24 pm
Can't locate loadable object August 4, 2005, 12:18 am
Set::Object installation Problem October 14, 2006, 6:04 am
Object oriented database February 8, 2008, 10:37 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap