Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

HTML::Template Interpolation Error?

by hbo (Monk)
on Aug 16, 2004 at 20:58 UTC ( #383456=perlquestion: print w/replies, xml ) Need Help??
hbo has asked for the wisdom of the Perl Monks concerning the following question:


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?


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.


      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 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?


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.

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://383456]
Approved by hardburn
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2018-06-19 01:51 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.