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