Fun thing, or module for (by) nut.
Inline::TT allows you to define your Perl code with Template Toolkit's BLOCK syntax. You can get the tarball here.
package Inline::TT;
use strict;
use vars qw($VERSION);
$VERSION = 0.01;
use base qw(Inline);
use IO::File;
use Template::Parser;
sub croak { require Carp; Carp::croak(@_) }
#--------------------------------------------------
# Inline APIs
#--------------------------------------------------
sub register {
return {
language => 'TT',
aliases => [ qw(tt) ],
type => 'interpreted',
suffix => 'tt',
};
}
sub validate { }
sub build {
my $self = shift;
my $code = $self->__compile($self->{API}->{code});
my $path = "$self->{API}->{install_lib}/auto/$self->{API}->{modpname}";
$self->mkpath($path) unless -d $path;
my $obj = $self->{API}->{location};
my $out = IO::File->new("> $obj") or die "$obj: $!";
$out->print($code);
$out->close;
}
sub load {
my $self = shift;
my $obj = $self->{API}->{location};
my $in = IO::File->new($obj) or die "$obj: $!";
my $code = do { local $/; <$in> };
$in->close;
eval "package $self->{API}->{pkg};$code;";
croak $@ if $@;
}
sub info { }
#--------------------------------------------------
# private methods
#--------------------------------------------------
sub __compile {
my($self, $text) = @_;
my $parser = Template::Parser->new({ PRE_CHOMP => 1, POST_CHOMP => 1 });
my $content = $parser->parse($text) or croak $parser->error;
my $document = $self->__document($content);
my $subs;
for my $block (keys %{$content->{DEFBLOCKS}}) {
$subs .= <new;
\$Context->visit(\$Doc->{_DEFBLOCKS});
$subs
CODE
;
}
sub __document {
my($self, $content) = @_;
# just pasted from Template::Document::write_perl_file
my ($block, $defblocks, $metadata) =
@$content{ qw( BLOCK DEFBLOCKS METADATA ) };
my $pkg = "'Template::Document'";
$defblocks = join('',
map { "'$_' => $defblocks->{ $_ },\n" }
keys %$defblocks);
$metadata = join('',
map {
my $x = $metadata->{ $_ };
$x =~ s/(['\\])/\\$1/g;
"'$_' => '$x',\n";
} keys %$metadata);
return <
--
Tatsuhiko Miyagawa
miyagawa@cpan.org