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

Odd (literally) problems with HTML::Template and params

by legLess (Hermit)
on Jul 10, 2001 at 06:00 UTC ( [id://95221]=perlquestion: print w/replies, xml ) Need Help??

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

Monks ~

I'm writing a script with CGI and HTML::Template. This code:

$template->param( STYLE_SHEET => "$STYLE_SHEET", SCRIPT_LOCATION => "$SCRIPT_LOCATION", MEMBER_NAME => $q->param('realname') );
Produces an error ("You gave me an odd number of parameters to param()!") if $q->param('realname') isn't defined. Set $q->param('realname') to 0 (or anything else) and everything's fine.

First question
This code:

$template->param( MEMBER_NAME => $q->param('realname') );
Produces no errors with identical input. Why? As long as MEMBER_NAME is the only parameter, it seems not to matter if $q->param('realname') defined or not. It's only when there are other parameters that I get the error. The error itself makes a sort of sense, but this seems inconsistent.

Second question
One solution to this is to explicitly set every parameter to 0 if it's not defined. This is possible, but inelegant, and a headache. I like that Perl assumes false if something's undefined. Am I missing something?

Third question
This code is kind of ugly, especially compared with assigning to a hash slice (@row is a row returned from a database):

if (@row) { $q-param( mid => $row[0], name => $row[1], realname => $row[2], email => $row[3] ); }
And if, as in question 2 above, I set every param to 0 if it was undefined, I'd have to add something like this:
} else { $q-param( mid => 0, name => 0, realname => 0, email => 0 ); }
Surely there's a better way?

TIA
--
man with no legs, inc.

Replies are listed 'Best First'.
Re: Odd (literally) problems with HTML::Template and params
by bikeNomad (Priest) on Jul 10, 2001 at 08:02 UTC
    The problem is that param() is sensitive to context. If you call it in a list context (which you are), and there's no value, then it returns an empty list (that is, zero items). This is different than returning undef (which would be one item). If you really want just one item from param(), call it in scalar context:

    $template->param( STYLE_SHEET => "$STYLE_SHEET", SCRIPT_LOCATION => "$SCRIPT_LOCATION", MEMBER_NAME => scalar( $q->param('realname') ) );
      *Cough* Gee, legLess, does the work "duh" mean anything to you? Thanks for the gentle clue-stick, bikeNomad.
      --
      man with no legs, inc.
Re: Odd (literally) problems with HTML::Template and params
by LD2 (Curate) on Jul 10, 2001 at 08:43 UTC
    To clear parameters - use the clear_params() function. Also for more information, you may want to check out: .
    I believe one way to deal with the failure of one param out of many is using the die_on_bad_params option and set it to 1 - although, it's probably not the best way. I'd use bikeNomad's suggestion.
Re: Odd (literally) problems with HTML::Template and params
by bwana147 (Pilgrim) on Jul 10, 2001 at 10:59 UTC

    For the 3rd question, you want to assign to different hash elements the values of a list? You can use a counter (and take care of missing values with an ||):

    if ( @row ) { my $counter = 0; $q->param( map { $_ => $row[$counter++] || '' } qw/mid name realname emai +l/ ); }
    Don't know if it's more legible though.

    --bwana147

      Oooh - very pretty. Thanks!
      --
      man with no legs, inc.
Re: Odd (literally) problems with HTML::Template and params
by PrakashK (Pilgrim) on Jul 10, 2001 at 08:41 UTC
    What version are you using? This problem is fixed in version 2.2 and greater (I believe the current version is 2.3).

    /prakash

      My host is using 1.7; I'm trying to get them to upgrade.
      --
      man with no legs, inc.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2024-04-23 14:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found