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

Greetings,
The following code doesn't print the correct value.
my %hash=( 'NEW_A/B/C' => '3' ); my $t=HTML::Template->new(filename=> 'test.tmpl'); $t->param('NEW_A/B/C' => $hash{'NEW_A/B/C'}); print $t->output();

test.tmpl contains  <tmpl_var NEW_/A/B/C> . I guess the problem with the special characters in the "tmpl_var".Please suggest me How we can proceed ? .
-kulls

Replies are listed 'Best First'.
Re: How to Set the 'tmpl_var' value in HTML::Template
by bobf (Monsignor) on Dec 29, 2007 at 06:49 UTC

    I guess the problem with the special characters in the "tmpl_var".
    The "special characters" are not might be the problem*. Based on my test, the reason you are not getting the expected result is because the template contains an error. Note the missing text, indicated below:
    <TMPL_VAR NAME=NEW_A/B/C> ^^^^^

    When I changed the template as indicated above, it worked fine for me.

    On an unrelated note, I would avoid assigning to %hash and simply do $template->param( 'NEW_A/B/C' => 3 );.

    HTH

    *Update: Softened wording based on esper's comment (thanks for mentioning it).

      I suspect it's actually both. According to HTML::Template, "The "NAME=" in the tag is optional, although for extensibility's sake I recommend using it. Example - "<TMPL_LOOP LOOP_NAME>" is acceptable." Personally, I've never included the "name=" and it's never caused me a problem.

      However, I also only use alphanumerics and underscores in my key names. I wouldn't be at all surprised to find that HTML::Template needs a little extra help (in the form of an explicit "name=") to deal with the slashes. It does seem a little odd that just adding "name=" fixes it without also requiring the name to be quoted, though.

        I agree with esper and personally stopped using name= ages ago.

        What I don't understand is why you would use a variable name that looks nothing like a variable name. IMHO, it looks more like some weird regex or other obfuscated Perl code. You might want to check out Damian Conway's Perl Best Practices for some tips at naming variables. I'd get confused using variable names that look like that.

        "Fundamentals" my son.


        —Brad
        "The important work of moving the world forward does not wait to be done by perfect men." George Eliot