Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

HTML::Template Interpolation Error?

by hbo (Monk)
on Aug 16, 2004 at 20:58 UTC ( [id://383456]=perlquestion: print w/replies, xml ) Need Help??

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

Greets.

Some code I wrote came back to me today with the complaint that it was blowing up with an error:

HTML::Template : Attempt to set nonexistent parameter '<input type="ch +eckbox" ...
The code in question looked like this:
$html->param('rcadd_list' => checkbox_group( ...
So it looks like the 'rcadd_list' parameter is getting swallowed. To make a long story short, changing the code like this:
$html->param('rcadd_list' => ''.checkbox_group( ...
Fixed it up.

I'm relying on interpolating the return value of a CGI call into the parameter list of an HTML::Template call throughout this code, not to mention throughout my career. Why does it fail in this case?

Update:

blokhead supplied my answer.

checkbox_group() returns a list in list context. HTML::Template::param takes a list of key/value pairs. The first pair was thus

'rcadd_list' => <input type="checkbox ..>
where the second parameter is the first <input> tag returned by the checkbox_group call. But then the next <input> tag becomes a parameter tag for HTML::Template::param(), which is totally fubar.

Prepending an empty string as above forced scalar context, which made checkbox_group return a space-seperated list of input tags, which was just what I wanted. I ended up changing the code like so:

# We need to force scalar context because # checkbox_group() returns a list, otherwise. $html->param(rcadd_list => scalar ( checkbox_group( ...
Which makes the whole thing more explicit, in case anyone as thick as I comes along to work on this in the future. 8)

Thanks to blokhead for the correct answer!

"Even if you are on the right track, you'll get run over if you just sit there." - Will Rogers

Replies are listed 'Best First'.
Re: HTML::Template Interpolation Error?
by blokhead (Monsignor) on Aug 16, 2004 at 21:20 UTC
    checkbox_group returns a list! So what HTML::Template gets is:
    $html->param( rcadd_list => "<input type=checkbox ...", "<input type=checkbox ..." => "<input type=checkbox ...", "<input type=checkbox ..." => ... );
    This causes an HTML::Template error when die_on_bad_params is set. Either use join "" => checkbox_group(...) or force scalar context (with scalar or with concatenation as you did above) to get what you want.

    blokhead

      Bingo, I think!

      So, why does ''.checkbox_group(.. flatten the list? Is

      "some string".('a','list','of','strings');
      equivilant to
      "some string".'a'.'list'.'of'.'strings';
      ?

      "Even if you are on the right track, you'll get run over if you just sit there." - Will Rogers

        No, it's because the author of CGI.pm wrote it to do so:
        sub checkbox_group { ... return wantarray ? @elements : join(' ',@elements) ... }
        A list in scalar context (as in your example directly above) returns the last element of the list (your expression evaluates to "some stringstrings"). For a function, if you want different behavior (as in checkbox_group), you check for scalar or list context with wantarray and return the appropriate thing manually.

        The CGI docs could probably do a better job making this obvious, though. Perhaps there's a blanket statement somewhere about all HTML-generation functions having this behavior in scalar context?

        blokhead

Re: HTML::Template Interpolation Error?
by hardburn (Abbot) on Aug 16, 2004 at 21:01 UTC

    The problem is likely in your template, probably because of a missing quote or '>'.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re: HTML::Template Interpolation Error?
by hbo (Monk) on Aug 16, 2004 at 21:21 UTC
    None of those are a problem as far as I can tell. And note that the solution is to force string interpolation by prepending ''. to the checkbox_group paramater.

    Here's the relevant section of the template:

    <TMPL_IF name=rcadd_list> <P> Please select what additional packages you would like installed via Re +d Carpet.<br> You may select multiple packages. All packages with a check mark wil +l be installed. <p> <TMPL_VAR name=rcadd_list> </TMPL_IF>

    "Even if you are on the right track, you'll get run over if you just sit there." - Will Rogers

Re: HTML::Template Interpolation Error?
by bradcathey (Prior) on Aug 16, 2004 at 21:16 UTC

    If you feel it's not an issue, you can, of course, set die_on_bad_params => 0. Other wise I usually find an error like this showing up because of a misnamed-misspelled-missing bracket-mismatched quote in my HTML, or even an incorrect file name in my calling Perl. Just my .02.


    —Brad
    "Don't ever take a fence down until you know the reason it was put up." G. K. Chesterton

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-19 01:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found