Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

CGI Buffer module will not return params.

by IOrdy (Friar)
on Nov 21, 2001 at 18:38 UTC ( #126768=perlquestion: print w/replies, xml ) Need Help??

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

I wrote this module and I also wrote one for Apache:Request but for some reason it's returning params us 'undef'. If someone could take a look and tell me if I have done anything wrong.
#!/usr/bin/perl -w package iXML::Buffer::CGI; use strict; # use CGI ':standard'; use CGI; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = { 'r' => new CGI, 'html' => undef, 'redir' => undef, }; bless($self, $class); return $self; } sub html { my ($self, $html) = @_; $self->{html} = $html; } sub redir { my ($self, $redir) = @_; $self->{redir} = $redir; } sub output { my $self = shift; if ($self->{redir}) { $self->{r}->status(302); # $self->{apr}->header_out('Location', $ENV{SCRIPT_NAME}.$self +->{redir}); $self->{r}->header_out('Location', $self->{redir}); $self->{r}->header('text/html'); $self->{r}->print($self->{html}); }else{ $self->{r}->header('text/html'); $self->{r}->print($self->{html}); } } sub param { my ($self) = shift; my @in_params = $self->{r}->param; my %out_params; foreach my $p (@in_params){ %out_params = ( $p => $self->{r}->param($p), ); } return \%out_params; } 1;

Replies are listed 'Best First'.
(jeffa) Re: CGI Buffer module will not return params.
by jeffa (Bishop) on Nov 21, 2001 at 21:30 UTC
    I'm not sure i understand your param() method - what is it's purpose? Wouldn't it be easier and more effective to just use something like:
    sub param { my ($self,$param) = @_; return $self->{'r'}->param($param); }
    With your code, what happens if i send a query string like foo.cgi?foo=bar&foo=baz&foo=qux?

    Style issues:
    Remember that => automagically quotes the left hand side:

    my $self = { r => CGI->new(), html => undef, redir => undef, };
    Also note that i used the direct notation to instantiate the CGI object, this is not at all necessary, but it is a good habit to get into.

    In output(), since you are refering to $self->{r} multiple times, i recommend assigning it to a scalar:

    sub output { my $self = shift; if ($self->{'redir'}) { my $r = $self->{'r'}; $r->status(302); # etc ... } }

    Other than that it looks good - try mortis's suggestion, then test it with a query string that contains multilples values for one variable. Make sure you get an array back, otherwise you might want to just write a wrapper for the param method like i did. But, again, what is the purpose of this module, why would i want to use it instead of CGI.pm?

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
      I wrote a site that used Apache::Request then yesterday I needed to swap it to CGI because it needs to run in a win32 environment under indigostars microweb (which only has CGI.pm).

      To save time having to swap various things (because if I do $apache_request->param it returns a hash reference) I thought it would be simpler to write them both into modules with the same interface so I can pick and choose.
Re: CGI Buffer module will not return params.
by mortis (Pilgrim) on Nov 21, 2001 at 20:13 UTC
    One issue I can see with your param sub is in the foreach loop. You're resetting %out_params each time through that loop instead of adding key/value paris to it. I'm assuming that's not what you want to do.

    To add elements, you need to the members of the hash instead of setting the hash equal to a new list:

    foreach my $p (@in_params) { $out_params{$p} = $self->{r}->param($p); }
    saying %out_params = (...); resets the hash to only the values within the list - (...).

    Does this make sense?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2023-02-01 16:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (11 votes). Check out past polls.

    Notices?