Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

CGI:Application and HTML::Template parameter passing

by emgrasso (Novice)
on Sep 02, 2007 at 19:54 UTC ( [id://636624] : perlquestion . print w/replies, xml ) Need Help??

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

I am just learning CGI::Application. My first app is based closely on some examples I have found online, including

There is something very basic that I seem to be missing about passing information back and forth. I have verified that the sitepoint example works correctly in my environment and tweaked my code to be as similar to it as possible.

One of my templates ends with the lines:

<tmpl_var name=userid> <input type="Hidden" name="userid" value="<tmpl_var name=userid>"> <input type="Hidden" name="rm" value="maintain_user"> </form> </body> </html>
Note that the <tmpl_var name=userid> displays the value of userid so I'm sure it is what I think it should be: 0 or the userid of the record that is being edited.

The maintain_user runmode looks like:

sub maintain_user { my $self = shift; my $q = $self->query; # validate the input use CGI::Application::Plugin::ValidateRM (qw/check_rm/); my ($results, $err_page) = $self->check_rm('admin_edit_user +', '_user_profile'); return $err_page if $err_page; if ($q->param('userid') == 0) { $self->_create_user(); } else { $self->_update_user(); } $self->param('message', 'User saved'); $self->admin_edit_user(); }

My problem is that $q->param('userid') seems to be always coming back empty, so the script always takes the create_user branch.

Devpopup shows incoming HTTP_REFERER http://localhost/cgi-bin/IncentivePoints.cgi/IncentivePoints.cgi?rm=admin_edit_user&userid=test3 and outgoing QUERY_STRING rm=admin_edit_user&userid=test3.

Is there a better way to do the parameter passing? Or a good way to debug the problem?

Or can anyone suggest what might be going wrong?

Replies are listed 'Best First'.
Re: CGI:Application and HTML::Template parameter passing
by arpad.szasz (Pilgrim) on Sep 02, 2007 at 21:39 UTC

    the problem is that you are using numeric comparison (==) when you need string comparison (eq), so the contents of $q->param('userid') always evaluates to true regardless if it's empty or not. change your code like this:

    if ($q->param('userid') eq '') { $self->_create_user(); } else { $self->_update_user(); }

    see also the tutorials here on PerlMonks that cover the various ways Perl test for true/false.

    also here is how you could debug input parameter passing in your CGI::Application modules:

    sub maintain_user { my $self = shift; my $q = $self->query; use Data::Dumper; return Dumper $q; }

      In addition to the above and since you're using the ValidateRM plugin (hoorah!) you may want to also consider using $result->valid('userid') instead of $q->param('userid') to make sure you're taking advantage of all the goodes that ValidateRM provides.

      If any of the params are using a filter (like trim or digit or a custom one) or if you eventually start using filters, doing this will stop you from having to change anything later. Filters are definately good to clean your data up.

Re: CGI:Application and HTML::Template parameter passing
by shmem (Chancellor) on Sep 03, 2007 at 18:49 UTC
    Is there a better way to do the parameter passing? Or a good way to debug the problem?
    Yes. Take note and write the following in big letters on the rim of your monitor:

    Always use -w.

    See perlrun. Alternatively, use warnings. And use strict

    For CGI programming, always use -T. See perlsec.


    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}