eval and variables in templates

Hi,
I've just found out a thing with templates.
This does not work as advertised:

Damir Dzeko wrote:
I guess that's because variable replacement happens after eval phase.
Here is how I fixed the problem. I've put the variable replacement into a sub called _variable_replace (attached in var-repl.pl) and called it from both places where needed: $Self->_variable_replacement(\%Param, $GlobalRef, \$Line); I used call by reference because it improves performance since perl in that case does not have to copy large strings of data just to make them available for function (which gets called pretty frequently since templating engine is line-based). Significant performance gains could be achieved if call-by value would be replaced with call-by-reference, where possible. Also, it would be great not to use hashes at all for data-structures that are pre-defined (fixed parts). It would be far more efficient to access them in this way: Instead of $Hash{XY} use $ArrayRef->[A_XY] And A_XY define as constant function sub A_XY () { 0 } # index in array Next index would be 1, 2, ... and so on. That two things alone could gain amazing improvement in performance. Also, it would help in development since compiler could now complain if someone accidentally types Xy instead of XY, which in current scheme could go unnoticed (and cause a ton of headache). :) Also, the logic of template-engine could be made more efficient if it would be first pre-processed on line-by-line basis (remove comments), and then processed as single string using s{}{}egxs, and instead of calling s/// for each line. Regards, D. # -- # variable replacement # (internal use only) sub _variable_replacement { my ($Self, $ParamRef, $GlobalRef, $StringRef) = @_; ${$StringRef} =~ s{ \$(QData|Data|Env|Config|Include)\{"(.+?)"\} } { if ($1 eq "Data" || $1 eq "Env") { if (defined $GlobalRef->{"$1Ref"}->{$2}) { my $x = $GlobalRef->{"$1Ref"}->{$2}; # print STDERR "Expanded $1/$2 as: $x\n"; $x; } else { # output replace with nothing! ""; } } elsif ($1 eq "QData") { my $Code = $1; my $Text = $2; if (!defined($Text) || $Text =~ /^","(.+?)$/) { ""; } elsif ($Text =~ /^(.+?)","(.+?)$/) { if (defined $GlobalRef->{"DataRef"}->{$1}) { $Self->Ascii2Html(Text => $GlobalRef->{"DataRef"}->{$1}, Max => $2); } else { # output replace with nothing! ""; } } else { if (defined $GlobalRef->{"DataRef"}->{$Text}) { $Self->Ascii2Html(Text => $GlobalRef->{"DataRef"}->{$Text}); } else { # output replace with nothing! ""; } } } # replace with elsif ($1 eq "Config") { if (defined $Self->{ConfigObject}->Get($2)) { $Self->{ConfigObject}->Get($2); } else { # output replace with nothing! ""; } } # include dtl files elsif ($1 eq "Include") { $ParamRef->{TemplateFile} = $2; $Self->Output(%$ParamRef); } }egx; }
participants (1)
-
Damir Dzeko