Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

miyagawa (1653)

miyagawa
  (email not shown publicly)
http://bulknews.vox.com/
AOL IM: bulknews (Add Buddy, Send Message)

Journal of miyagawa (1653)

Tuesday May 08, 2007
03:21 AM

Adding custom statement to TT generated code

[ #33212 ]

Investigating the UTF-8 on/off performance issues for work. Template::Stash::ForceUTF8 does utf8_on calls to all stash variables, which could be crucial to perfomance to the large-scale site like ours.

Looking through TT source code, I came up with the simple hack to add 'use encoding "utf-8"' to the generated code by TT. This solves the utf-8 bytes and string concatination (auto-upgrades from latin-1), and could be a good example to add custom perl statement to the TT generated code in general.

use Template;
use Template::Parser;
my $tt = Template->new({
    PARSER => Template::Parser->new({
        FACTORY => "Template::Directive::WithEncoding"
    }),
});
 
package Template::Directive::WithEncoding;
use base 'Template::Directive';
 
sub template {
    my $self = shift;
    my $code = $self->SUPER::template(@_);
    return qq(use encoding "utf-8";\n$code);
}

Similary you can write the following code to do array push, instead of string concatination for each TT statement and do join the array at last. I'm skeptical how this technique could be useful in Perl, as compared to JavaScript or Python, where string concatination is slower than array expansion.

use Template;
use Template::Parser;
 
$Template::Directive::OUTPUT = 'push @output, ';
 
my $tt = Template->new({
    PARSER => Template::Parser->new({
        FACTORY => "Template::Directive::ArrayPush",
    }),
});
 
package Template::Directive::ArrayPush;
use base 'Template::Directive';
 
sub template {
    my $self = shift;
    my $code = $self->SUPER::template(@_);
 
    # @output stuff
    $code =~ s|^sub {|sub {\n    my \@output;|;
    $code =~ s|(return \$output;\s*\}\s*)$|return join '', \@output;\n}|;
 
    return $code;
}

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.