Rick Triplett wrote:
Sorry; I forgot to include all the code:

It is always a good idea to provide enough for a working snippet to be cut and pasted for other to tinker with. You are not only more likely to get a response, it is more likely that the response will be correct.


<snip>
most of the variations died with the error that a scalar was found when an operator was expected. The variation below plugged into the template the literal '$student_id' instead of its value' trip304.' I've run out of ideas to try and books to look in. Anyone's advice would be most welcome!


my @info_prams = qw(
    student_id
    name_full            enroll_type
    name_nick            account_number
);

$student_id = "trip304";
$name_full = "John Smith";
$name_nick = "Johnny";
# Assign template parameters
foreach (@info_prams) {
    $tmpl->param( $_ => ("\$$_") );
} </snip>

Using a backslash escapes the first dollar sign, taking away its special meaning and just placing the literal $ in the string. This results in the key/value pairs being student_id => '$student_id', etc.


If you really want to do this with symbolic references here is a way:
     $tmpl->param( $_ => ${$_});

This will cause the value within {$_} to be treated as the name of a scalar variable. The curly braces disambiguate the meaning to perl.

With no braces $$_ would result in $student_id being dereferenced as if it were a reference to a scalar.

There is almost always a better way to do a given task than to resort to the use of symbolic references. Make sure you don't declare any of the scalars as my variables. There be dragons... see perldoc perlref for more info.

In this case since the names and values are hash entries why not just put the data in a hash and pass a reference to the hash to the template->param call?

Like this:

my %page = (student_id => "trip304",
            name_full => "John Smith",
            name_nick => "Johnny");


$tmpl->param(\%page);

Separate scalars with the same names as the hash keys means you have to maintain *three* copies of the same names, one in the template file, and two in the perl code. One more possiblity of missing one on an update, and one more thing to do with each change.

Hope this helps,

Mike

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to