blib/lib/Template/Directive.pm untested

filter: Statementstmt 0.0% 0 / 308 Branchbran 0.0% 0 / 154 Conditioncond 0.0% 0 / 37 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 48 Podpod 0.0% 0 / 43 totaltotal 0.0% 0 / 590 CC 74 SCAR 32.7
CC
74
Cov
0%
try
110.0
ident
72.0
if
42.0
CRAP 26.3
1 #================================================================= -*-Perl-*-
2 #
3 # Template::Directive
4 #
5 # DESCRIPTION
6 # Factory module for constructing templates from Perl code.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 # WARNING
12 # Much of this module is hairy, even furry in places. It needs
13 # a lot of tidying up and may even be moved into a different place
14 # altogether. The generator code is often inefficient, particularly in
15 # being very anal about pretty-printing the Perl code all neatly, but
16 # at the moment, that's still high priority for the sake of easier
17 # debugging.
18 #
19 # COPYRIGHT
20 # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved.
21 #
22 # This module is free software; you can redistribute it and/or
23 # modify it under the same terms as Perl itself.
24 #
25 #============================================================================
26  
27 package Template::Directive;
28  
29 use strict;
30 use warnings;
31 use base 'Template::Base';
32 use Template::Constants;
33 use Template::Exception;
34  
35 our $VERSION = '3.100';
36 our $DEBUG = 0 unless defined $DEBUG;
37 our $WHILE_MAX = 1000 unless defined $WHILE_MAX;
38 our $PRETTY = 0 unless defined $PRETTY;
39 our $OUTPUT = '$output .= ';
40  
41  
42 sub _init {
43 my ($self, $config) = @_;
44 $self->{ NAMESPACE } = $config->{ NAMESPACE };
45 return $self;
46 }
47  
48 sub trace_vars {
49 my $self = shift;
50 return @_
51 ? ($self->{ TRACE_VARS } = shift)
52 : $self->{ TRACE_VARS };
53 }
54  
55 sub pad {
56 my ($text, $pad) = @_;
57 $pad = ' ' x ($pad * 4);
58 $text =~ s/^(?!#line)/$pad/gm;
59 $text;
60 }
61  
62 #========================================================================
63 # FACTORY METHODS
64 #
65 # These methods are called by the parser to construct directive instances.
66 #========================================================================
67  
68 #------------------------------------------------------------------------
69 # template($block)
70 #------------------------------------------------------------------------
71  
72 sub template {
73 my ($self, $block) = @_;
74 $block = pad($block, 2) if $PRETTY;
75  
76 return "sub { return '' }" unless $block =~ /\S/;
77  
78 return <<EOF;
79 sub {
80 my \$context = shift || die "template sub called without context\\n";
81 my \$stash = \$context->stash;
82 my \$output = '';
83 my \$_tt_error;
84  
85 eval { BLOCK: {
86 $block
87 } };
88 if (\$@) {
89 \$_tt_error = \$context->catch(\$@, \\\$output);
90 die \$_tt_error unless \$_tt_error->type eq 'return';
91 }
92  
93 return \$output;
94 }
95 EOF
96 }
97  
98  
99 #------------------------------------------------------------------------
100 # anon_block($block) [% BLOCK %] ... [% END %]
101 #------------------------------------------------------------------------
102  
103 sub anon_block {
104 my ($self, $block) = @_;
105 $block = pad($block, 2) if $PRETTY;
106  
107 return <<EOF;
108  
109 # BLOCK
110 $OUTPUT do {
111 my \$output = '';
112 my \$_tt_error;
113  
114 eval { BLOCK: {
115 $block
116 } };
117 if (\$@) {
118 \$_tt_error = \$context->catch(\$@, \\\$output);
119 die \$_tt_error unless \$_tt_error->type eq 'return';
120 }
121  
122 \$output;
123 };
124 EOF
125 }
126  
127  
128 #------------------------------------------------------------------------
129 # block($blocktext)
130 #------------------------------------------------------------------------
131  
132 sub block {
133 my ($self, $block) = @_;
134 return join("\n", @{ $block || [] });
135 }
136  
137  
138 #------------------------------------------------------------------------
139 # textblock($text)
140 #------------------------------------------------------------------------
141  
142 sub textblock {
143 my ($self, $text) = @_;
144 return "$OUTPUT " . &text($self, $text) . ';';
145 }
146  
147  
148 #------------------------------------------------------------------------
149 # text($text)
150 #------------------------------------------------------------------------
151  
152 sub text {
153 my ( $self, $text ) = @_;
154  
155 return '' if !length $text;
156  
157 if ( $text =~ tr{$@\\}{} ) {
158 $text =~ s/(["\$\@\\])/\\$1/g;
159 $text =~ s/\n/\\n/g;
160 return '"' . $text . '"';
161 }
162  
163 $text =~ s{'}{\\'}g if index( $text, q{'} ) != -1;
164 return q{'} . $text . q{'};
165 }
166  
167 #------------------------------------------------------------------------
168 # quoted(\@items) "foo$bar"
169 #------------------------------------------------------------------------
170  
171 sub quoted {
172 my ($self, $items) = @_;
173 return '' unless @$items;
174 return ("('' . " . $items->[0] . ')') if scalar @$items == 1;
175 return '(' . join(' . ', @$items) . ')';
176 # my $r = '(' . join(' . ', @$items) . ' . "")';
177 # print STDERR "[$r]\n";
178 # return $r;
179 }
180  
181  
182 #------------------------------------------------------------------------
183 # ident(\@ident) foo.bar(baz)
184 #------------------------------------------------------------------------
185  
186 sub ident {
187 my ($self, $ident) = @_;
188 return "''" unless @$ident;
189 my $ns;
190  
191 # Careful! Template::Parser always creates a Template::Directive object
192 # (as of v2.22_1) so $self is usually an object. However, we used to
193 # allow Template::Directive methods to be called as class methods and
194 # Template::Namespace::Constants module takes advantage of this fact
195 # by calling Template::Directive->ident() when it needs to generate an
196 # identifier. This hack guards against Mr Fuckup from coming to town
197 # when that happens.
198  
199 if (ref $self) {
200 # trace variable usage
201 if ($self->{ TRACE_VARS }) {
202 my $root = $self->{ TRACE_VARS };
203 my $n = 0;
204 my $v;
205 while ($n < @$ident) {
206 $v = $ident->[$n];
207 for ($v) { s/^'//; s/'$// };
208 $root = $root->{ $v } ||= { };
209 $n += 2;
210 }
211 }
212  
213 # does the first element of the identifier have a NAMESPACE
214 # handler defined?
215 if (@$ident > 2 && ($ns = $self->{ NAMESPACE })) {
216 my $key = $ident->[0];
217  
218 # a faster alternate to $key =~ s/^'(.+)'$/$1/s
219 if ( index( $key, q[']) == 0 ) {
220 substr( $key, 0, 1, '' );
221 substr( $key, -1, 1, '' ); # remove the last char blindly
222 }
223  
224 if ($ns = $ns->{ $key }) {
225 return $ns->ident($ident);
226 }
227 }
228 }
229  
230 if (scalar @$ident <= 2 && ! $ident->[1]) {
231 $ident = $ident->[0];
232 }
233 else {
234 $ident = '[' . join(', ', @$ident) . ']';
235 }
236 return "\$stash->get($ident)";
237 }
238  
239 #------------------------------------------------------------------------
240 # identref(\@ident) \foo.bar(baz)
241 #------------------------------------------------------------------------
242  
243 sub identref {
244 my ($self, $ident) = @_;
245 return "''" unless @$ident;
246 if (scalar @$ident <= 2 && ! $ident->[1]) {
247 $ident = $ident->[0];
248 }
249 else {
250 $ident = '[' . join(', ', @$ident) . ']';
251 }
252 return "\$stash->getref($ident)";
253 }
254  
255  
256 #------------------------------------------------------------------------
257 # assign(\@ident, $value, $default) foo = bar
258 #------------------------------------------------------------------------
259  
260 sub assign {
261 my ($self, $var, $val, $default) = @_;
262  
263 if (ref $var) {
264 if (scalar @$var == 2 && ! $var->[1]) {
265 $var = $var->[0];
266 }
267 else {
268 $var = '[' . join(', ', @$var) . ']';
269 }
270 }
271 $val .= ', 1' if $default;
272 return "\$stash->set($var, $val)";
273 }
274  
275  
276 #------------------------------------------------------------------------
277 # args(\@args) foo, bar, baz = qux
278 #------------------------------------------------------------------------
279  
280 sub args {
281 my ($self, $args) = @_;
282 my $hash = shift @$args;
283 push(@$args, '{ ' . join(', ', @$hash) . ' }')
284 if @$hash;
285  
286 return '0' unless @$args;
287 return '[ ' . join(', ', @$args) . ' ]';
288 }
289  
290 #------------------------------------------------------------------------
291 # filenames(\@names)
292 #------------------------------------------------------------------------
293  
294 sub filenames {
295 my ($self, $names) = @_;
296 if (@$names > 1) {
297 $names = '[ ' . join(', ', @$names) . ' ]';
298 }
299 else {
300 $names = shift @$names;
301 }
302 return $names;
303 }
304  
305  
306 #------------------------------------------------------------------------
307 # get($expr) [% foo %]
308 #------------------------------------------------------------------------
309  
310 sub get {
311 my ($self, $expr) = @_;
312 return "$OUTPUT $expr;";
313 }
314  
315  
316 #------------------------------------------------------------------------
317 # call($expr) [% CALL bar %]
318 #------------------------------------------------------------------------
319  
320 sub call {
321 my ($self, $expr) = @_;
322 $expr .= ';';
323 return $expr;
324 }
325  
326  
327 #------------------------------------------------------------------------
328 # set(\@setlist) [% foo = bar, baz = qux %]
329 #------------------------------------------------------------------------
330  
331 sub set {
332 my ($self, $setlist) = @_;
333 my $output;
334 while (my ($var, $val) = splice(@$setlist, 0, 2)) {
335 $output .= &assign($self, $var, $val) . ";\n";
336 }
337 chomp $output;
338 return $output;
339 }
340  
341  
342 #------------------------------------------------------------------------
343 # default(\@setlist) [% DEFAULT foo = bar, baz = qux %]
344 #------------------------------------------------------------------------
345  
346 sub default {
347 my ($self, $setlist) = @_;
348 my $output;
349 while (my ($var, $val) = splice(@$setlist, 0, 2)) {
350 $output .= &assign($self, $var, $val, 1) . ";\n";
351 }
352 chomp $output;
353 return $output;
354 }
355  
356  
357 #------------------------------------------------------------------------
358 # insert(\@nameargs) [% INSERT file %]
359 # # => [ [ $file, ... ], \@args ]
360 #------------------------------------------------------------------------
361  
362 sub insert {
363 my ($self, $nameargs) = @_;
364 my ($file, $args) = @$nameargs;
365 $file = $self->filenames($file);
366 return "$OUTPUT \$context->insert($file);";
367 }
368  
369  
370 #------------------------------------------------------------------------
371 # include(\@nameargs) [% INCLUDE template foo = bar %]
372 # # => [ [ $file, ... ], \@args ]
373 #------------------------------------------------------------------------
374  
375 sub include {
376 my ($self, $nameargs) = @_;
377 my ($file, $args) = @$nameargs;
378 my $hash = shift @$args;
379 $file = $self->filenames($file);
380 $file .= @$hash ? ', { ' . join(', ', @$hash) . ' }' : '';
381 return "$OUTPUT \$context->include($file);";
382 }
383  
384  
385 #------------------------------------------------------------------------
386 # process(\@nameargs) [% PROCESS template foo = bar %]
387 # # => [ [ $file, ... ], \@args ]
388 #------------------------------------------------------------------------
389  
390 sub process {
391 my ($self, $nameargs) = @_;
392 my ($file, $args) = @$nameargs;
393 my $hash = shift @$args;
394 $file = $self->filenames($file);
395 $file .= @$hash ? ', { ' . join(', ', @$hash) . ' }' : '';
396 return "$OUTPUT \$context->process($file);";
397 }
398  
399  
400 #------------------------------------------------------------------------
401 # if($expr, $block, $else) [% IF foo < bar %]
402 # ...
403 # [% ELSE %]
404 # ...
405 # [% END %]
406 #------------------------------------------------------------------------
407  
408 sub if {
409 my ($self, $expr, $block, $else) = @_;
410 my @else = $else ? @$else : ();
411 $else = pop @else;
412 $block = pad($block, 1) if $PRETTY;
413  
414 my $output = "if ($expr) {\n$block\n}\n";
415  
416 foreach my $elsif (@else) {
417 ($expr, $block) = @$elsif;
418 $block = pad($block, 1) if $PRETTY;
419 $output .= "elsif ($expr) {\n$block\n}\n";
420 }
421 if (defined $else) {
422 $else = pad($else, 1) if $PRETTY;
423 $output .= "else {\n$else\n}\n";
424 }
425  
426 return $output;
427 }
428  
429  
430 #------------------------------------------------------------------------
431 # foreach($target, $list, $args, $block) [% FOREACH x = [ foo bar ] %]
432 # ...
433 # [% END %]
434 #------------------------------------------------------------------------
435  
436 sub foreach {
437 my ($self, $target, $list, $args, $block, $label) = @_;
438 $args = shift @$args;
439 $args = @$args ? ', { ' . join(', ', @$args) . ' }' : '';
440 $label ||= 'LOOP';
441  
442 my ($loop_save, $loop_set, $loop_restore, $setiter);
443 if ($target) {
444 $loop_save = 'eval { $_tt_oldloop = ' . &ident($self, ["'loop'"]) . ' }';
445 $loop_set = "\$stash->{'$target'} = \$_tt_value";
446 $loop_restore = "\$stash->set('loop', \$_tt_oldloop)";
447 }
448 else {
449 $loop_save = '$stash = $context->localise()';
450 # $loop_set = "\$stash->set('import', \$_tt_value) "
451 # . "if ref \$value eq 'HASH'";
452 $loop_set = "\$stash->get(['import', [\$_tt_value]]) "
453 . "if ref \$_tt_value eq 'HASH'";
454 $loop_restore = '$stash = $context->delocalise()';
455 }
456 $block = pad($block, 3) if $PRETTY;
457  
458 return <<EOF;
459  
460 # FOREACH
461 do {
462 my (\$_tt_value, \$_tt_error, \$_tt_oldloop);
463 my \$_tt_list = $list;
464  
465 unless (UNIVERSAL::isa(\$_tt_list, 'Template::Iterator')) {
466 \$_tt_list = Template::Config->iterator(\$_tt_list)
467 || die \$Template::Config::ERROR, "\\n";
468 }
469  
470 (\$_tt_value, \$_tt_error) = \$_tt_list->get_first();
471 $loop_save;
472 \$stash->set('loop', \$_tt_list);
473 eval {
474 $label: while (! \$_tt_error) {
475 $loop_set;
476 $block;
477 (\$_tt_value, \$_tt_error) = \$_tt_list->get_next();
478 }
479 };
480 $loop_restore;
481 die \$@ if \$@;
482 \$_tt_error = 0 if \$_tt_error && \$_tt_error eq Template::Constants::STATUS_DONE;
483 die \$_tt_error if \$_tt_error;
484 };
485 EOF
486 }
487  
488 #------------------------------------------------------------------------
489 # next() [% NEXT %]
490 #
491 # Next iteration of a FOREACH loop (experimental)
492 #------------------------------------------------------------------------
493  
494 sub next {
495 my ($self, $label) = @_;
496 $label ||= 'LOOP';
497 return <<EOF;
498 (\$_tt_value, \$_tt_error) = \$_tt_list->get_next();
499 next $label;
500 EOF
501 }
502  
503  
504 #------------------------------------------------------------------------
505 # wrapper(\@nameargs, $block) [% WRAPPER template foo = bar %]
506 # # => [ [$file,...], \@args ]
507 #------------------------------------------------------------------------
508  
509 sub wrapper {
510 my ($self, $nameargs, $block) = @_;
511 my ($file, $args) = @$nameargs;
512 my $hash = shift @$args;
513  
514 local $" = ', ';
515 # print STDERR "wrapper([@$file], { @$hash })\n";
516  
517 return $self->multi_wrapper($file, $hash, $block)
518 if @$file > 1;
519 $file = shift @$file;
520  
521 $block = pad($block, 1) if $PRETTY;
522 push(@$hash, "'content'", '$output');
523 $file .= @$hash ? ', { ' . join(', ', @$hash) . ' }' : '';
524  
525 return <<EOF;
526  
527 # WRAPPER
528 $OUTPUT do {
529 my \$output = '';
530 $block
531 \$context->include($file);
532 };
533 EOF
534 }
535  
536  
537 sub multi_wrapper {
538 my ($self, $file, $hash, $block) = @_;
539 $block = pad($block, 1) if $PRETTY;
540  
541 push(@$hash, "'content'", '$output');
542 $hash = @$hash ? ', { ' . join(', ', @$hash) . ' }' : '';
543  
544 $file = join(', ', reverse @$file);
545 # print STDERR "multi wrapper: $file\n";
546  
547 return <<EOF;
548  
549 # WRAPPER
550 $OUTPUT do {
551 my \$output = '';
552 $block
553 foreach ($file) {
554 \$output = \$context->include(\$_$hash);
555 }
556 \$output;
557 };
558 EOF
559 }
560  
561  
562 #------------------------------------------------------------------------
563 # while($expr, $block) [% WHILE x < 10 %]
564 # ...
565 # [% END %]
566 #------------------------------------------------------------------------
567  
568 sub while {
569 my ($self, $expr, $block, $label) = @_;
570 $block = pad($block, 2) if $PRETTY;
571 $label ||= 'LOOP';
572  
573 return <<EOF;
574  
575 # WHILE
576 do {
577 my \$_tt_failsafe = $WHILE_MAX;
578 $label:
579 while (($expr) && --\$_tt_failsafe >= 0) {
580 $block
581 }
582 \$context->throw('while', "WHILE loop terminated (> $WHILE_MAX iterations)")
583 if \$_tt_failsafe < 0;
584 };
585 EOF
586 }
587  
588  
589 #------------------------------------------------------------------------
590 # switch($expr, \@case) [% SWITCH %]
591 # [% CASE foo %]
592 # ...
593 # [% END %]
594 #------------------------------------------------------------------------
595  
596 sub switch {
597 my ($self, $expr, $case) = @_;
598 my @case = @$case;
599 my ($match, $block, $default);
600 my $caseblock = '';
601  
602 $default = pop @case;
603  
604 foreach $case (@case) {
605 $match = $case->[0];
606 $block = $case->[1];
607 $block = pad($block, 1) if $PRETTY;
608 $caseblock .= <<EOF;
609 \$_tt_match = $match;
610 \$_tt_match = [ \$_tt_match ] unless ref \$_tt_match eq 'ARRAY';
611 if (grep(/^\\Q\$_tt_result\\E\$/, \@\$_tt_match)) {
612 $block
613 last SWITCH;
614 }
615 EOF
616 }
617  
618 $caseblock .= $default
619 if defined $default;
620 $caseblock = pad($caseblock, 2) if $PRETTY;
621  
622 return <<EOF;
623  
624 # SWITCH
625 do {
626 my \$_tt_result = $expr;
627 my \$_tt_match;
628 SWITCH: {
629 $caseblock
630 }
631 };
632 EOF
633 }
634  
635  
636 #------------------------------------------------------------------------
637 # try($block, \@catch) [% TRY %]
638 # ...
639 # [% CATCH %]
640 # ...
641 # [% END %]
642 #------------------------------------------------------------------------
643  
644 sub try {
645 my ($self, $block, $catch) = @_;
646 my @catch = @$catch;
647 my ($match, $mblock, $default, $final, $n);
648 my $catchblock = '';
649 my $handlers = [];
650  
651 $block = pad($block, 2) if $PRETTY;
652 $final = pop @catch;
653 $final = "# FINAL\n" . ($final ? "$final\n" : '')
654 . 'die $_tt_error if $_tt_error;' . "\n" . '$output;';
655 $final = pad($final, 1) if $PRETTY;
656  
657 $n = 0;
658 foreach $catch (@catch) {
659 $match = $catch->[0] || do {
660 $default ||= $catch->[1];
661 next;
662 };
663 $mblock = $catch->[1];
664 $mblock = pad($mblock, 1) if $PRETTY;
665 push(@$handlers, "'$match'");
666 $catchblock .= $n++
667 ? "elsif (\$_tt_handler eq '$match') {\n$mblock\n}\n"
668 : "if (\$_tt_handler eq '$match') {\n$mblock\n}\n";
669 }
670 $catchblock .= "\$_tt_error = 0;";
671 $catchblock = pad($catchblock, 3) if $PRETTY;
672 if ($default) {
673 $default = pad($default, 1) if $PRETTY;
674 $default = "else {\n # DEFAULT\n$default\n \$_tt_error = '';\n}";
675 }
676 else {
677 $default = '# NO DEFAULT';
678 }
679 $default = pad($default, 2) if $PRETTY;
680  
681 $handlers = join(', ', @$handlers);
682 return <<EOF;
683  
684 # TRY
685 $OUTPUT do {
686 my \$output = '';
687 my (\$_tt_error, \$_tt_handler);
688 eval {
689 $block
690 };
691 if (\$@) {
692 \$_tt_error = \$context->catch(\$@, \\\$output);
693 die \$_tt_error if \$_tt_error->type =~ /^(return|stop)\$/;
694 \$stash->set('error', \$_tt_error);
695 \$stash->set('e', \$_tt_error);
696 if (defined (\$_tt_handler = \$_tt_error->select_handler($handlers))) {
697 $catchblock
698 }
699 $default
700 }
701 $final
702 };
703 EOF
704 }
705  
706  
707 #------------------------------------------------------------------------
708 # throw(\@nameargs) [% THROW foo "bar error" %]
709 # # => [ [$type], \@args ]
710 #------------------------------------------------------------------------
711  
712 sub throw {
713 my ($self, $nameargs) = @_;
714 my ($type, $args) = @$nameargs;
715 my $hash = shift(@$args);
716 my $info = shift(@$args);
717 $type = shift @$type; # uses same parser production as INCLUDE
718 # etc., which allow multiple names
719 # e.g. INCLUDE foo+bar+baz
720  
721 if (! $info) {
722 $args = "$type, undef";
723 }
724 elsif (@$hash || @$args) {
725 local $" = ', ';
726 my $i = 0;
727 $args = "$type, { args => [ "
728 . join(', ', $info, @$args)
729 . ' ], '
730 . join(', ',
731 (map { "'" . $i++ . "' => $_" } ($info, @$args)),
732 @$hash)
733 . ' }';
734 }
735 else {
736 $args = "$type, $info";
737 }
738  
739 return "\$context->throw($args, \\\$output);";
740 }
741  
742  
743 #------------------------------------------------------------------------
744 # clear() [% CLEAR %]
745 #
746 # NOTE: this is redundant, being hard-coded (for now) into Parser.yp
747 #------------------------------------------------------------------------
748  
749 sub clear {
750 return "\$output = '';";
751 }
752  
753 #------------------------------------------------------------------------
754 # break() [% BREAK %]
755 #
756 # NOTE: this is redundant, being hard-coded (for now) into Parser.yp
757 #------------------------------------------------------------------------
758  
759 sub OLD_break {
760 return 'last LOOP;';
761 }
762  
763 #------------------------------------------------------------------------
764 # return() [% RETURN %]
765 #------------------------------------------------------------------------
766  
767 sub return {
768 return "\$context->throw('return', '', \\\$output);";
769 }
770  
771 #------------------------------------------------------------------------
772 # stop() [% STOP %]
773 #------------------------------------------------------------------------
774  
775 sub stop {
776 return "\$context->throw('stop', '', \\\$output);";
777 }
778  
779  
780 #------------------------------------------------------------------------
781 # use(\@lnameargs) [% USE alias = plugin(args) %]
782 # # => [ [$file, ...], \@args, $alias ]
783 #------------------------------------------------------------------------
784  
785 sub use {
786 my ($self, $lnameargs) = @_;
787 my ($file, $args, $alias) = @$lnameargs;
788 $file = shift @$file; # same production rule as INCLUDE
789 $alias ||= $file;
790 $args = &args($self, $args);
791 $file .= ", $args" if $args;
792 # my $set = &assign($self, $alias, '$plugin');
793 return "# USE\n"
794 . "\$stash->set($alias,\n"
795 . " \$context->plugin($file));";
796 }
797  
798 #------------------------------------------------------------------------
799 # view(\@nameargs, $block) [% VIEW name args %]
800 # # => [ [$file, ... ], \@args ]
801 #------------------------------------------------------------------------
802  
803 sub view {
804 my ($self, $nameargs, $block, $defblocks) = @_;
805 my ($name, $args) = @$nameargs;
806 my $hash = shift @$args;
807 $name = shift @$name; # same production rule as INCLUDE
808 $block = pad($block, 1) if $PRETTY;
809  
810 if (%$defblocks) {
811 $defblocks = join(",\n", map { "'$_' => $defblocks->{ $_ }" }
812 keys %$defblocks);
813 $defblocks = pad($defblocks, 1) if $PRETTY;
814 $defblocks = "{\n$defblocks\n}";
815 push(@$hash, "'blocks'", $defblocks);
816 }
817 $hash = @$hash ? '{ ' . join(', ', @$hash) . ' }' : '';
818  
819 return <<EOF;
820 # VIEW
821 do {
822 my \$output = '';
823 my \$_tt_oldv = \$stash->get('view');
824 my \$_tt_view = \$context->view($hash);
825 \$stash->set($name, \$_tt_view);
826 \$stash->set('view', \$_tt_view);
827  
828 $block
829  
830 \$stash->set('view', \$_tt_oldv);
831 \$_tt_view->seal();
832 # \$output; # not used - commented out to avoid warning
833 };
834 EOF
835 }
836  
837  
838 #------------------------------------------------------------------------
839 # perl($block)
840 #------------------------------------------------------------------------
841  
842 sub perl {
843 my ($self, $block) = @_;
844 $block = pad($block, 1) if $PRETTY;
845  
846 return <<EOF;
847  
848 # PERL
849 \$context->throw('perl', 'EVAL_PERL not set')
850 unless \$context->eval_perl();
851  
852 $OUTPUT do {
853 my \$output = "package Template::Perl;\\n";
854  
855 $block
856  
857 local(\$Template::Perl::context) = \$context;
858 local(\$Template::Perl::stash) = \$stash;
859  
860 my \$_tt_result = '';
861 tie *Template::Perl::PERLOUT, 'Template::TieString', \\\$_tt_result;
862 my \$_tt_save_stdout = select *Template::Perl::PERLOUT;
863  
864 eval \$output;
865 select \$_tt_save_stdout;
866 \$context->throw(\$@) if \$@;
867 \$_tt_result;
868 };
869 EOF
870 }
871  
872  
873 #------------------------------------------------------------------------
874 # no_perl()
875 #------------------------------------------------------------------------
876  
877 sub no_perl {
878 my $self = shift;
879 return "\$context->throw('perl', 'EVAL_PERL not set');";
880 }
881  
882  
883 #------------------------------------------------------------------------
884 # rawperl($block)
885 #
886 # NOTE: perhaps test context EVAL_PERL switch at compile time rather than
887 # runtime?
888 #------------------------------------------------------------------------
889  
890 sub rawperl {
891 my ($self, $block, $line) = @_;
892 for ($block) {
893 s/^\n+//;
894 s/\n+$//;
895 }
896 $block = pad($block, 1) if $PRETTY;
897 $line = $line ? " (starting line $line)" : '';
898  
899 return <<EOF;
900 # RAWPERL
901 #line 1 "RAWPERL block$line"
902 $block
903 EOF
904 }
905  
906  
907  
908 #------------------------------------------------------------------------
909 # filter()
910 #------------------------------------------------------------------------
911  
912 sub filter {
913 my ($self, $lnameargs, $block) = @_;
914 my ($name, $args, $alias) = @$lnameargs;
915 $name = shift @$name;
916 $args = &args($self, $args);
917 $args = $args ? "$args, $alias" : ", undef, $alias"
918 if $alias;
919 $name .= ", $args" if $args;
920 $block = pad($block, 1) if $PRETTY;
921  
922 return <<EOF;
923  
924 # FILTER
925 $OUTPUT do {
926 my \$output = '';
927 my \$_tt_filter = \$context->filter($name)
928 || \$context->throw(\$context->error);
929  
930 $block
931  
932 &\$_tt_filter(\$output);
933 };
934 EOF
935 }
936  
937  
938 #------------------------------------------------------------------------
939 # capture($name, $block)
940 #------------------------------------------------------------------------
941  
942 sub capture {
943 my ($self, $name, $block) = @_;
944  
945 if (ref $name) {
946 if (scalar @$name == 2 && ! $name->[1]) {
947 $name = $name->[0];
948 }
949 else {
950 $name = '[' . join(', ', @$name) . ']';
951 }
952 }
953 $block = pad($block, 1) if $PRETTY;
954  
955 return <<EOF;
956  
957 # CAPTURE
958 \$stash->set($name, do {
959 my \$output = '';
960 $block
961 \$output;
962 });
963 EOF
964  
965 }
966  
967  
968 #------------------------------------------------------------------------
969 # macro($name, $block, \@args)
970 #------------------------------------------------------------------------
971  
972 sub macro {
973 my ($self, $ident, $block, $args) = @_;
974 $block = pad($block, 2) if $PRETTY;
975  
976 if ($args) {
977 my $nargs = scalar @$args;
978 $args = join(', ', map { "'$_'" } @$args);
979 $args = $nargs > 1
980 ? "\@_tt_args{ $args } = splice(\@_, 0, $nargs)"
981 : "\$_tt_args{ $args } = shift";
982  
983 return <<EOF;
984  
985 # MACRO
986 \$stash->set('$ident', sub {
987 my \$output = '';
988 my (%_tt_args, \$_tt_params);
989 $args;
990 \$_tt_params = shift;
991 \$_tt_params = { } unless ref(\$_tt_params) eq 'HASH';
992 \$_tt_params = { \%_tt_args, %\$_tt_params };
993  
994 my \$stash = \$context->localise(\$_tt_params);
995 eval {
996 $block
997 };
998 \$stash = \$context->delocalise();
999 die \$@ if \$@;
1000 return \$output;
1001 });
1002 EOF
1003  
1004 }
1005 else {
1006 return <<EOF;
1007  
1008 # MACRO
1009 \$stash->set('$ident', sub {
1010 my \$_tt_params = \$_[0] if ref(\$_[0]) eq 'HASH';
1011 my \$output = '';
1012  
1013 my \$stash = \$context->localise(\$_tt_params);
1014 eval {
1015 $block
1016 };
1017 \$stash = \$context->delocalise();
1018 die \$@ if \$@;
1019 return \$output;
1020 });
1021 EOF
1022 }
1023 }
1024  
1025  
1026 sub debug {
1027 my ($self, $nameargs) = @_;
1028 my ($file, $args) = @$nameargs;
1029 my $hash = shift @$args;
1030 $args = join(', ', @$file, @$args);
1031 $args .= @$hash ? ', { ' . join(', ', @$hash) . ' }' : '';
1032 return "$OUTPUT \$context->debugging($args); ## DEBUG ##";
1033 }
1034  
1035  
1036 1;
1037  
1038 __END__