| 1 | package CGI::Builder::TT2; | ||
| 2 | |||
| 3 | $VERSION = 0.03; | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Scalar::Util; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Template; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | ▶ | use Devel::Peek; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | $Carp::Internal{ Template }++; | ||
| 12 | $Carp::Internal{+__PACKAGE__}++; | ||
| 13 | |||
| 14 | my $print_code; | ||
| 15 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||
| 16 | 0 | ▶ | $print_code = sub { |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 17 | 0 | shift()->CGI::Builder::TT2::_::tt_print( @_ ) | |
| 18 | } | ||
| 19 | 1 | } | |
| 20 | |||
| 21 | |||
| 22 | 1 | ▶ | use Class::groups( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||
| 23 | { name => 'tt_new_args' , | ||
| 24 | 0 | ▶ | default => sub { |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 25 | 0 | { INCLUDE_PATH => [ $_[0]->page_path ] } | |
| 26 | } | ||
| 27 | } , | ||
| 28 | 1 | ); | |
| 29 | |||
| 30 | 1 | ▶ | use Class::props( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||
| 31 | { name => 'tt' , | ||
| 32 | 0 | ▶ | default => sub { shift()->tt_new( @_ ) } |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 33 | } , | ||
| 34 | 1 | ); | |
| 35 | |||
| 36 | 1 | ▶ | use Object::groups( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 37 | 1 | { name => 'tt_vars' , | |
| 38 | } , | ||
| 39 | 1 | ); | |
| 40 | |||
| 41 | 1 | ▶ | use Object::props( |
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||
| 42 | { name => 'tt_lookups_package' , | ||
| 43 | 0 | ▶ | default => sub { |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 44 | 0 | ref( $_[0] ) . '::Lookups' | |
| 45 | } | ||
| 46 | } , | ||
| 47 | { name => 'tt_template' , | ||
| 48 | 0 | ▶ | default => sub { |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 49 | 0 | $_[0]->page_name . $_[0]->page_suffix | |
| 50 | } | ||
| 51 | } , | ||
| 52 | { name => 'page_suffix' , | ||
| 53 | default => '.tt2' | ||
| 54 | } , | ||
| 55 | { name => 'page_content' , | ||
| 56 | 0 | ▶ | default => sub { $print_code }, |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 57 | } | ||
| 58 | 1 | ); | |
| 59 | |||
| 60 | # Template->new takes a hashref as sole arg, force scalar context | ||
| 61 | 0 | ▶ | sub tt_new { Template->new( scalar($_[0]->tt_new_args) ) } |
|
Subroutine
tt_new: not called CC=1 SCAR=6.9
| |||
| 62 | |||
| 63 | |||
| 64 | sub CGI::Builder::TT2::_::tt_print | ||
| 65 | { | ||
| 66 | my $s = shift; | ||
| 67 | |||
| 68 | Scalar::Util::weaken( $s ); | ||
| 69 | $s->tt_vars( CBF => sub { return $s } ); | ||
| 70 | |||
| 71 | { | ||
| 72 | # Inspect the symbol table of the Lookups package, store refs | ||
| 73 | # in tt_vars for TT to use. | ||
| 74 | 1 | ▶ | no strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 75 | my $href = $s->tt_lookups_package() . '::'; | ||
| 76 | |||
| 77 | foreach my $symbol ( keys %$href ) { | ||
| 78 | |||
| 79 | local *glob = $href->{ $symbol }; | ||
| 80 | |||
| 81 | $s->tt_vars( $symbol => defined $glob ? $glob | ||
| 82 | : defined @glob ? \@glob | ||
| 83 | : defined %glob ? \%glob | ||
| 84 | : defined &glob ? \&glob | ||
| 85 | : undef ); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | foreach my $symbol ( keys %{ scalar $s->tt_vars() } ) { | ||
| 90 | next unless ref( $s->tt_vars( $symbol ) ) eq 'CODE'; | ||
| 91 | $s->tt_vars( $symbol => | ||
| 92 | CGI::Builder::TT2::_::make_wrapper( $s->tt_vars( $symbol ), $s ) | ||
| 93 | ) | ||
| 94 | } | ||
| 95 | |||
| 96 | # process() prints to STDOUT. Could pass a scalar to collect | ||
| 97 | # output, but that would eat memory. | ||
| 98 | my $ok = $s->tt->process( $s->tt_template(), scalar $s->tt_vars()); | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | sub CGI::Builder::TT2::_::make_wrapper | ||
| 103 | { | ||
| 104 | my $code = shift; | ||
| 105 | my $app_object = shift; | ||
| 106 | |||
| 107 | # Stop memory leak by weakening enclosed references | ||
| 108 | Scalar::Util::weaken($app_object); | ||
| 109 | |||
| 110 | return sub { | ||
| 111 | unshift @_, $app_object; | ||
| 112 | goto &{ $code }; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | sub page_content_check | ||
| 118 | { | ||
| 119 | my $s = shift; | ||
| 120 | |||
| 121 | # Template uses a search path to find templates, and can use different | ||
| 122 | # providers to get templates from a DB or the web, so a -f test might not | ||
| 123 | # be valid here. The tt->context->template method loads the template or | ||
| 124 | # throws an exception if loading fails. | ||
| 125 | if ($s->page_content eq $print_code) { | ||
| 126 | eval { $s->tt->context->template($s->tt_template) }; | ||
| 127 | return !$@; | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | return length $s->page_content; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | 1; | ||
| 136 | |||
| 137 | __END__ | ||