0: Fun thing, or module for (by) nut.<P>
1:
2: Inline::TT allows you to define your Perl code with Template Toolkit's BLOCK syntax. You can get the tarball <A href="http://bulknews.net/lib/archives/">here</A>.<P>
3: <READMORE>
4: <CODE>
5: package Inline::TT;
6:
7: use strict;
8: use vars qw($VERSION);
9: $VERSION = 0.01;
10:
11: use base qw(Inline);
12: use IO::File;
13: use Template::Parser;
14:
15: sub croak { require Carp; Carp::croak(@_) }
16:
17: #--------------------------------------------------
18: # Inline APIs
19: #--------------------------------------------------
20:
21: sub register {
22: return {
23: language => 'TT',
24: aliases => [ qw(tt) ],
25: type => 'interpreted',
26: suffix => 'tt',
27: };
28: }
29:
30: sub validate { }
31:
32:
33:
34: sub build {
35: my $self = shift;
36: my $code = $self->__compile($self->{API}->{code});
37: my $path = "$self->{API}->{install_lib}/auto/$self->{API}->{modpname}";
38: $self->mkpath($path) unless -d $path;
39:
40: my $obj = $self->{API}->{location};
41: my $out = IO::File->new("> $obj") or die "$obj: $!";
42: $out->print($code);
43: $out->close;
44: }
45:
46:
47: sub load {
48: my $self = shift;
49: my $obj = $self->{API}->{location};
50: my $in = IO::File->new($obj) or die "$obj: $!";
51: my $code = do { local $/; <$in> };
52: $in->close;
53:
54: eval "package $self->{API}->{pkg};$code;";
55: croak $@ if $@;
56: }
57:
58: sub info { }
59:
60: #--------------------------------------------------
61: # private methods
62: #--------------------------------------------------
63:
64: sub __compile {
65: my($self, $text) = @_;
66: my $parser = Template::Parser->new({ PRE_CHOMP => 1, POST_CHOMP => 1 });
67: my $content = $parser->parse($text) or croak $parser->error;
68: my $document = $self->__document($content);
69:
70: my $subs;
71: for my $block (keys %{$content->{DEFBLOCKS}}) {
72: $subs .= <<BLOCK;
73: sub $block {
74: my(\%args) = \@_;
75: \$Context->include(\$Context->template('$block'), \\\%args);
76: }
77:
78: BLOCK
79: }
80:
81: return <<CODE;
82: #------------------------------------------------------------------------
83: # Compiled template generated by the Inline::TT version $VERSION
84: #------------------------------------------------------------------------
85:
86: use Template::Context;
87: use Template::Document;
88:
89: my \$Doc = $document
90: my \$Context = Template::Context->new;
91: \$Context->visit(\$Doc->{_DEFBLOCKS});
92:
93: $subs
94: CODE
95: ;
96: }
97:
98: sub __document {
99: my($self, $content) = @_;
100:
101: # just pasted from Template::Document::write_perl_file
102: my ($block, $defblocks, $metadata) =
103: @$content{ qw( BLOCK DEFBLOCKS METADATA ) };
104: my $pkg = "'Template::Document'";
105:
106: $defblocks = join('',
107: map { "'$_' => $defblocks->{ $_ },\n" }
108: keys %$defblocks);
109:
110: $metadata = join('',
111: map {
112: my $x = $metadata->{ $_ };
113: $x =~ s/(['\\])/\\$1/g;
114: "'$_' => '$x',\n";
115: } keys %$metadata);
116:
117: return <<EOF;
118: bless {
119: $metadata
120: _HOT => 0,
121: _BLOCK => $block,
122: _DEFBLOCKS => {
123: $defblocks
124: },
125: }, $pkg;
126: EOF
127: ;
128: }
129:
130: 1;
131: __END__
132:
133: =head1 NAME
134:
135: Inline::TT - use TT BLOCK as your Perl sub
136:
137: =head1 SYNOPSIS
138:
139: use Inline 'TT';
140:
141: print add(args => [ 0, 1 ]); # 1
142: print rubyish(str => "Just another Perl Hacker"); # "Just/another/Ruby/hacker"
143:
144: __END__
145: __TT__
146: [% BLOCK add %]
147: [% result = 0 %]
148: [% FOREACH arg = args %]
149: [% result = result + arg %]
150: [% END %]
151: [% result %]
152: [% END %]
153:
154: [% BLOCK rubyish %]
155: [% strings = str.split(' ')
156: strings.2 = "Ruby"
157: %]
158: [% strings.join('/') %]
159: [% END %]
160:
161: =head1 DESCRIPTION
162:
163: Template-Toolkit is not just a Templating Engine. It's a
164: B<language>. Yep, Inline::TT is a Inline plugin to aloow you to code
165: your Perl subs in TT.
166:
167: =head1 AUTHOR
168:
169: Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
170:
171: This library is free software; you can redistribute it and/or modify
172: it under the same terms as Perl itself.
173:
174: =head1 SEE ALSO
175:
176: L<Template>, L<Inline>
177:
178: =cut
179: </CODE>
180: <PRE>--
181: Tatsuhiko Miyagawa
182: <A href="mailto:miyagawa@cpan.org">miyagawa@cpan.org</A></PRE> In reply to Inline::TT by miyagawa
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |