| 1 | package Tenjin::Template; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use Fcntl qw/:flock/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 8 | our $VERSION = "1.000000"; | |||||||||||||||||||||||||||
| 9 | 8 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Tenjin::Template - A Tenjin template object, either built from a file or from memory. | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # mostly used internally, but you can manipulate | ||||||||||||||||||||||||||||
| 18 | # templates like so | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | my $template = Tenjin::Template->new('/path/to/templates/template.html'); | ||||||||||||||||||||||||||||
| 21 | my $context = { scalar => 'scalar', arrayref => ['one', 2, "3"] }; | ||||||||||||||||||||||||||||
| 22 | $template->render($context); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | This module is in charge of the task of compiling Tenjin templates. | ||||||||||||||||||||||||||||
| 27 | Templates in Tenjin are compiled into standard Perl code (combined with | ||||||||||||||||||||||||||||
| 28 | any Perl code used inside the templates themselves). Rendering a template | ||||||||||||||||||||||||||||
| 29 | means C<eval>uating that Perl code and returning its output. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | The Tenjin engine reads a template file or a template string, and creates | ||||||||||||||||||||||||||||
| 32 | a Template object from it. Then the object compiles itself by traversing | ||||||||||||||||||||||||||||
| 33 | the template, parsing Tenjin macros like 'include' and 'start_capture', | ||||||||||||||||||||||||||||
| 34 | replaces Tenjin expressions (i.e. C<[== $expr =]> or C<[= $expr =]>) with the | ||||||||||||||||||||||||||||
| 35 | appropriate Perl code, etc. This module ties a template object with | ||||||||||||||||||||||||||||
| 36 | a context object, but all context manipulation (and the actual C<eval>uation | ||||||||||||||||||||||||||||
| 37 | of the Perl code) is done by L<Tenjin::Context>. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | If you're planning on using this module by itself (i.e. without the L<Tenjin> | ||||||||||||||||||||||||||||
| 40 | engine), keep in mind that template caching and layout templates are not | ||||||||||||||||||||||||||||
| 41 | handled by this module. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | our $MACRO_HANDLER_TABLE = { | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | 'include' => sub { my $arg = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 47 | 0 | " \$_buf .= \$_context->{'_engine'}->render($arg, \$_context, 0);"; | |||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||
| 49 | 1 | ▶ | 'start_capture' => sub { my $arg = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | 1 | " my \$_buf_bkup=\$_buf; \$_buf=''; my \$_capture_varname=$arg;"; | |||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||
| 52 | 1 | ▶ | 'stop_capture' => sub { my $arg = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 1 | " \$_context->{\$_capture_varname}=\$_buf; \$_buf=\$_buf_bkup;"; | |||||||||||||||||||||||||||
| 54 | }, | ||||||||||||||||||||||||||||
| 55 | 2 | ▶ | 'start_placeholder' => sub { my $arg = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | 2 | " if (\$_context->{$arg}) { \$_buf .= \$_context->{$arg}; } else {"; | |||||||||||||||||||||||||||
| 57 | }, | ||||||||||||||||||||||||||||
| 58 | 2 | ▶ | 'stop_placeholder' => sub { my $arg = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | 2 | " }"; | |||||||||||||||||||||||||||
| 60 | }, | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | 'echo' => sub { my $arg = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 62 | 0 | " \$_buf .= $arg;"; | |||||||||||||||||||||||||||
| 63 | }, | ||||||||||||||||||||||||||||
| 64 | 8 | }; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head1 METHODS | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head2 new( [$filename, \%opts] ) | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | Creates a new Tenjin::Template object, possibly from a file on the file | ||||||||||||||||||||||||||||
| 71 | system (in which case C<$filename> must be provided and be an absolute | ||||||||||||||||||||||||||||
| 72 | path to a template file). Optionally, a hash-ref of options can be | ||||||||||||||||||||||||||||
| 73 | passed to set some customizations. Available options are 'escapefunc', | ||||||||||||||||||||||||||||
| 74 | which will be in charge of escaping expressions (from C<[= $expr =]>) instead | ||||||||||||||||||||||||||||
| 75 | of the internal method (which uses L<HTML::Entities>); and 'rawclass', | ||||||||||||||||||||||||||||
| 76 | which can be used to prevent variables and objects of a certain class | ||||||||||||||||||||||||||||
| 77 | from being escaped, in which case the variable must be a hash-ref | ||||||||||||||||||||||||||||
| 78 | that has a key named 'str', which will be used instead. So, for example, | ||||||||||||||||||||||||||||
| 79 | if you have a variable named C<$var> which is a hash-ref, and 'rawclass' | ||||||||||||||||||||||||||||
| 80 | is set as 'HASH', then writing C<[= $var =]> on your templates will replace | ||||||||||||||||||||||||||||
| 81 | C<$var> with C<< $var->{str} >>. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | sub new { | ||||||||||||||||||||||||||||
| 86 | 18 | ▶ | my ($class, $filename, $template_name, $opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 18 | ▶ | my $escapefunc = defined($opts) && exists($opts->{escapefunc}) ? $opts->{escapefunc} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $opts and exists $opts->{"escapefunc"}
MC/DC0%0/2
defined $opts and exists $opts->{"escapefunc"}
defined $opts
exists $opts->{"escapefunc"}
Truth table33%1/3
A and B
A= defined $optsB= exists $opts->{"escapefunc"}
| |||||||||||||||||||||||||||||
| 89 | 18 | ▶ | my $rawclass = defined($opts) && exists($opts->{rawclass}) ? $opts->{rawclass} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $opts and exists $opts->{"rawclass"}
MC/DC0%0/2
defined $opts and exists $opts->{"rawclass"}
defined $opts
exists $opts->{"rawclass"}
Truth table33%1/3
A and B
A= defined $optsB= exists $opts->{"rawclass"}
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 18 | my $self = bless { | |||||||||||||||||||||||||||
| 92 | 'filename' => $filename, | ||||||||||||||||||||||||||||
| 93 | 'name' => $template_name, | ||||||||||||||||||||||||||||
| 94 | 'script' => undef, | ||||||||||||||||||||||||||||
| 95 | 'escapefunc' => $escapefunc, | ||||||||||||||||||||||||||||
| 96 | 'rawclass' => $rawclass, | ||||||||||||||||||||||||||||
| 97 | 'timestamp' => undef, | ||||||||||||||||||||||||||||
| 98 | 'args' => undef, | ||||||||||||||||||||||||||||
| 99 | }, $class; | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 18 | ▶ | $self->convert_file($filename) if $filename; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 18 | return $self; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head2 render( [$_context] ) | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Renders the template, possibly with a context hash-ref, and returns the | ||||||||||||||||||||||||||||
| 109 | rendered output. If errors have occured when rendering the template (which | ||||||||||||||||||||||||||||
| 110 | might happen since templates have and are Perl code), then this method | ||||||||||||||||||||||||||||
| 111 | will croak. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | sub render { | ||||||||||||||||||||||||||||
| 116 | 23 | ▶ | my ($self, $_context) = @_; | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=6 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 23 | ▶ | $_context ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_context ||= {}
MC/DC0%0/1
$_context ||= {}
$_context
Truth table50%1/2
A
A= $_context
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 23 | ▶ | if ($self->{func}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | return $self->{func}->($_context); | |||||||||||||||||||||||||||
| 122 | } else { | ||||||||||||||||||||||||||||
| 123 | 23 | ▶ | $_context = $Tenjin::CONTEXT_CLASS->new($_context) if ref $_context eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 23 | my $script = $self->{script}; | |||||||||||||||||||||||||||
| 126 | 23 | ▶ | $script = $_context->_build_decl() . $script unless $self->{args}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # rendering is actually done inside the context object | ||||||||||||||||||||||||||||
| 129 | # with the evaluate method. We pass either the name of | ||||||||||||||||||||||||||||
| 130 | # the template or the filename of the template for debug | ||||||||||||||||||||||||||||
| 131 | # purposes | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 23 | ▶ | return $_context->evaluate($script, $self->{filename} || $self->{name}); | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{'filename'} || $self->{'name'}
MC/DC0%0/2
$self->{'filename'} || $self->{'name'}
$self->{'filename'}
$self->{'name'}
Truth table33%1/3
A || B
A= $self->{'filename'}B= $self->{'name'}
| |||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 INTERNAL METHODS | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head2 convert_file( $filename ) | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Receives an absolute path to a template file, converts that file | ||||||||||||||||||||||||||||
| 142 | to Perl code by calling L<convert()|convert( $input, $filename )> and | ||||||||||||||||||||||||||||
| 143 | returns that code. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | sub convert_file { | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | my ($self, $filename) = @_; | ||||||||||||||||||||||||||
|
Subroutine
convert_file: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | return $self->convert($self->_read_file($filename, 1), $filename); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head2 convert( $input, [$filename] ) | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | Receives a text of a template (i.e. the template itself) and possibly | ||||||||||||||||||||||||||||
| 156 | an absolute path to the template file (if the template comes from a file), | ||||||||||||||||||||||||||||
| 157 | and converts the template into Perl code, which is later C<eval>uated | ||||||||||||||||||||||||||||
| 158 | for rendering. Conversion is done by parsing the statements in the | ||||||||||||||||||||||||||||
| 159 | template (see L<parse_stmt()|parse_stmt( $bufref, $input )>). | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =cut | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | sub convert { | ||||||||||||||||||||||||||||
| 164 | 18 | ▶ | my ($self, $input, $filename) = @_; | ||||||||||||||||||||||||||
|
Subroutine
convert: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 18 | $self->{filename} = $filename; | |||||||||||||||||||||||||||
| 167 | 18 | my @buf = ('my $_buf = ""; my $_V; ', ); | |||||||||||||||||||||||||||
| 168 | 18 | $self->parse_stmt(\@buf, $input); | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 18 | return $self->{script} = $buf[0] . " \$_buf;\n"; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head2 compile_stmt_pattern( $pl ) | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Receives a string which denotes the Perl code delimiter which is used | ||||||||||||||||||||||||||||
| 176 | inside templates. Tenjin uses 'C<< <?pl ... ?> >>' and 'C<< <?PL ... ?> >>' | ||||||||||||||||||||||||||||
| 177 | (the latter for preprocessing), so C<$pl> will be 'pl'. This method | ||||||||||||||||||||||||||||
| 178 | returns a tranlsation regular expression which will be used for reading | ||||||||||||||||||||||||||||
| 179 | embedded Perl code. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =cut | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | sub compile_stmt_pattern { | ||||||||||||||||||||||||||||
| 184 | 18 | ▶ | my $pl = shift; | ||||||||||||||||||||||||||
|
Subroutine
compile_stmt_pattern: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 18 | my $pat = '((^[ \t]*)?<\?'.$pl.'( |\t|\r?\n)(.*?) ?\?>([ \t]*\r?\n)?)'; | |||||||||||||||||||||||||||
| 187 | 18 | return qr/$pat/sm; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =head2 stmt_pattern | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | Returns the default pattern (which uses 'pl') with the | ||||||||||||||||||||||||||||
| 193 | L<previous_method|compile_stmt_pattern( $pl )>. | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =cut | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | sub stmt_pattern { | ||||||||||||||||||||||||||||
| 198 | 18 | ▶ | return compile_stmt_pattern('pl'); | ||||||||||||||||||||||||||
|
Subroutine
stmt_pattern: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =head2 expr_pattern() | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | Defines how expressions are written in Tenjin templates (C<[== $expr =]> | ||||||||||||||||||||||||||||
| 204 | and C<[= $expr =]>). | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =cut | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | sub expr_pattern { | ||||||||||||||||||||||||||||
| 209 | 23 | ▶ | return qr/\[=(=?)(.*?)(=?)=\]/s; | ||||||||||||||||||||||||||
|
Subroutine
expr_pattern: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =head2 parse_stmt( $bufref, $input ) | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | Receives a buffer which is used for saving a template's expressions | ||||||||||||||||||||||||||||
| 215 | and the template's text, parses all expressions in the templates and | ||||||||||||||||||||||||||||
| 216 | pushes them to the buffer. | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =cut | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | sub parse_stmt { | ||||||||||||||||||||||||||||
| 221 | 18 | ▶ | my ($self, $bufref, $input) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse_stmt: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 18 | my $pos = 0; | |||||||||||||||||||||||||||
| 224 | 18 | my $pat = $self->stmt_pattern(); | |||||||||||||||||||||||||||
| 225 | 18 | ▶ | while ($input =~ /$pat/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 14 | my ($pi, $lspace, $mspace, $stmt, $rspace) = ($1, $2, $3, $4, $5); | |||||||||||||||||||||||||||
| 227 | 14 | my $start = $-[0]; | |||||||||||||||||||||||||||
| 228 | 14 | my $text = substr($input, $pos, $start - $pos); | |||||||||||||||||||||||||||
| 229 | 14 | $pos = $start + length($pi); | |||||||||||||||||||||||||||
| 230 | 14 | ▶ | $self->parse_expr($bufref, $text) if $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 14 | ▶ | $mspace = '' if $mspace eq ' '; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | 14 | $stmt = $self->hook_stmt($stmt); | |||||||||||||||||||||||||||
| 233 | 14 | ▶ | $stmt .= $rspace if $rspace; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | 14 | ▶ | $stmt = $mspace . $stmt if $mspace; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 14 | ▶ | $stmt = $lspace . $stmt if $lspace; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 14 | $self->add_stmt($bufref, $stmt); | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | 18 | ▶ | my $rest = $pos == 0 ? $input : substr($input, $pos); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 18 | ▶ | $self->parse_expr($bufref, $rest) if $rest; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | =head2 hook_stmt( $stmt ) | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =cut | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | sub hook_stmt { | ||||||||||||||||||||||||||||
| 247 | 14 | ▶ | my ($self, $stmt) = @_; | ||||||||||||||||||||||||||
|
Subroutine
hook_stmt: called CC=10 SCAR=35.6
Pod
documented
| |||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | ## macro expantion | ||||||||||||||||||||||||||||
| 250 | 14 | ▶ | if ($stmt =~ /\A(\s*)(\w+)\((.*?)\);?(\s*)\Z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 7 | my ($lspace, $funcname, $arg, $rspace) = ($1, $2, $3, $4); | |||||||||||||||||||||||||||
| 252 | 7 | my $s = $self->expand_macro($funcname, $arg); | |||||||||||||||||||||||||||
| 253 | 7 | ▶ | return $lspace . $s . $rspace if defined($s); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | ## template arguments | ||||||||||||||||||||||||||||
| 257 | 8 | ▶ | unless ($self->{args}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | 8 | ▶ | if ($stmt =~ m/\A(\s*)\#\@ARGS\s+(.*)(\s*)\Z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | 0 | my ($lspace, $argstr, $rspace) = ($1, $2, $3); | |||||||||||||||||||||||||||
| 260 | 0 | my @args = (); | |||||||||||||||||||||||||||
| 261 | 0 | my @declares = (); | |||||||||||||||||||||||||||
| 262 | 0 | foreach my $arg (split(/,/, $argstr)) { | |||||||||||||||||||||||||||
| 263 | 0 | $arg =~ s/(^\s+|\s+$)//g; | |||||||||||||||||||||||||||
| 264 | 0 | ▶ | next unless $arg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | 0 | ▶ | $arg =~ m/\A([\$\@\%])?([a-zA-Z_]\w*)\Z/ or croak "[Tenjin] $arg: invalid template argument."; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | 0 | ▶ | croak "[Tenjin] $arg: only '\$var' is available for template argument." unless (!$1 || $1 eq '$'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $1 or $1 eq "\$"
MC/DC0%0/2
not $1 or $1 eq "\$"
not $1
$1 eq "\$"
Truth table0%0/3
not(A) or B
A= not $1B= $1 eq "\$"
| |||||||||||||||||||||||||||||
| 267 | 0 | my $name = $2; | |||||||||||||||||||||||||||
| 268 | 0 | push(@args, $name); | |||||||||||||||||||||||||||
| 269 | 0 | push(@declares, "my \$$name = \$_context->{$name}; "); | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | 0 | $self->{args} = \@args; | |||||||||||||||||||||||||||
| 272 | 0 | return $lspace . join('', @declares) . $rspace; | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 8 | return $stmt; | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =head2 expand_macro( $funcname, $arg ) | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | This method is in charge of invoking macro functions which might be used | ||||||||||||||||||||||||||||
| 282 | inside templates. The following macros are available: | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | =over | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | =item * C<include( $filename )> | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | Includes another template, whose name is C<$filename>, inside the current | ||||||||||||||||||||||||||||
| 289 | template. The included template will be placed inside the template as if | ||||||||||||||||||||||||||||
| 290 | they were one unit, so the context variable applies to both. | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | =item * C<start_capture( $name )> and C<end_capture()> | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | Tells Tenjin to capture the output of the rendered template from the point | ||||||||||||||||||||||||||||
| 295 | where C<start_capture()> was called to the point where C<end_capture()> | ||||||||||||||||||||||||||||
| 296 | was called. You must provide a name for the captured portion, which will be | ||||||||||||||||||||||||||||
| 297 | made available in the context as C<< $_context->{$name} >> for immediate | ||||||||||||||||||||||||||||
| 298 | usage. Note that the captured portion will not be printed unless you do | ||||||||||||||||||||||||||||
| 299 | so explicilty with C<< $_context->{$name} >>. | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | =item * C<start_placeholder( $var )> and C<end_placeholder()> | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | This is a special method which can be used for making your templates a bit | ||||||||||||||||||||||||||||
| 304 | cleaner. Suppose your context might have a variable whose name is defined | ||||||||||||||||||||||||||||
| 305 | in C<$var>. If that variable exists in the context, you simply want to print | ||||||||||||||||||||||||||||
| 306 | it, but if it's not, you want to print and/or perform other things. In that | ||||||||||||||||||||||||||||
| 307 | case you can call C<start_placeholder( $var )> with the name of the context | ||||||||||||||||||||||||||||
| 308 | variable you want printed, and if it's not, anything you do between | ||||||||||||||||||||||||||||
| 309 | C<start_placeholder()> and C<end_placeholder()> will be printed instead. | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | =item * echo( $exr ) | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | Just prints the provided expression. You might want to use it if you're | ||||||||||||||||||||||||||||
| 314 | a little too comfortable with PHP. | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | =back | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | =cut | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | sub expand_macro { | ||||||||||||||||||||||||||||
| 321 | 7 | ▶ | my ($self, $funcname, $arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
expand_macro: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 7 | my $handler = $MACRO_HANDLER_TABLE->{$funcname}; | |||||||||||||||||||||||||||
| 324 | 7 | ▶ | return $handler ? $handler->($arg) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | =head2 get_expr_and_escapeflag( $not_escape, $expr, $delete_newline ) | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | =cut | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | ## ex. get_expr_and_escapeflag('=', '$item->{name}', '') => 1, '$item->{name}', 0 | ||||||||||||||||||||||||||||
| 332 | sub get_expr_and_escapeflag { | ||||||||||||||||||||||||||||
| 333 | 17 | ▶ | my ($self, $not_escape, $expr, $delete_newline) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_expr_and_escapeflag: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 17 | return $expr, $not_escape eq '', $delete_newline eq '='; | |||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | =head2 parse_expr( $bufref, $input ) | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | =cut | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | sub parse_expr { | ||||||||||||||||||||||||||||
| 343 | 23 | ▶ | my ($self, $bufref, $input) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse_expr: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 23 | my $pos = 0; | |||||||||||||||||||||||||||
| 346 | 23 | $self->start_text_part($bufref); | |||||||||||||||||||||||||||
| 347 | 23 | my $pat = $self->expr_pattern(); | |||||||||||||||||||||||||||
| 348 | 23 | ▶ | while ($input =~ /$pat/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 349 | 17 | my $start = $-[0]; | |||||||||||||||||||||||||||
| 350 | 17 | my $text = substr($input, $pos, $start - $pos); | |||||||||||||||||||||||||||
| 351 | 17 | my ($expr, $flag_escape, $delete_newline) = $self->get_expr_and_escapeflag($1, $2, $3); | |||||||||||||||||||||||||||
| 352 | 17 | $pos = $+[0]; | |||||||||||||||||||||||||||
| 353 | 17 | ▶ | $self->add_text($bufref, $text) if $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 354 | 17 | ▶ | $self->add_expr($bufref, $expr, $flag_escape) if $expr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 355 | 17 | ▶ | if ($delete_newline) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 356 | 4 | my $end = $+[0]; | |||||||||||||||||||||||||||
| 357 | 4 | ▶ | if (substr($input, $end, 1) eq "\n") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | 1 | $bufref->[0] .= "\n"; | |||||||||||||||||||||||||||
| 359 | 1 | $pos++; | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||
| 363 | 23 | ▶ | my $rest = $pos == 0 ? $input : substr($input, $pos); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 364 | 23 | $self->add_text($bufref, $rest); | |||||||||||||||||||||||||||
| 365 | 23 | $self->stop_text_part($bufref); | |||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | =head2 start_text_part( $bufref ) | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | =cut | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | sub start_text_part { | ||||||||||||||||||||||||||||
| 373 | 23 | ▶ | my ($self, $bufref) = @_; | ||||||||||||||||||||||||||
|
Subroutine
start_text_part: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 23 | $bufref->[0] .= ' $_buf .= '; | |||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | =head2 stop_text_part( $bufref ) | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | =cut | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | sub stop_text_part { | ||||||||||||||||||||||||||||
| 383 | 23 | ▶ | my ($self, $bufref) = @_; | ||||||||||||||||||||||||||
|
Subroutine
stop_text_part: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | 23 | $bufref->[0] .= '; '; | |||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | =head2 add_text( $bufref, $text ) | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | =cut | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | sub add_text { | ||||||||||||||||||||||||||||
| 393 | 34 | ▶ | my ($self, $bufref, $text) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_text: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 34 | ▶ | return unless $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 396 | 30 | $text =~ s/([`\\])/\\$1/g; | |||||||||||||||||||||||||||
| 397 | 30 | my $is_start = $bufref->[0] =~ / \$_buf \.= \Z/; | |||||||||||||||||||||||||||
| 398 | 30 | ▶ | $bufref->[0] .= $is_start ? "q`$text`" : " . q`$text`"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =head2 add_stmt( $bufref, $stmt ) | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =cut | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | sub add_stmt { | ||||||||||||||||||||||||||||
| 406 | 14 | ▶ | my ($self, $bufref, $stmt) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_stmt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | 14 | $bufref->[0] .= $stmt; | |||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | =head2 add_expr( $bufref, $expr, $flag_escape ) | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | =cut | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | sub add_expr { | ||||||||||||||||||||||||||||
| 416 | 17 | ▶ | my ($self, $bufref, $expr, $flag_escape) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_expr: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | 17 | ▶ | my $dot = $bufref->[0] =~ / \$_buf \.= \Z/ ? '' : ' . '; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 419 | 17 | ▶ | $bufref->[0] .= $dot . ($flag_escape ? $self->escaped_expr($expr) : "($expr)"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | =head2 defun( $funcname, @args ) | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | =cut | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | sub defun { ## (experimental) | ||||||||||||||||||||||||||||
| 427 | 0 | ▶ | my ($self, $funcname, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
defun: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | 0 | ▶ | unless ($funcname) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 430 | 0 | my $funcname = $self->{filename}; | |||||||||||||||||||||||||||
| 431 | 0 | ▶ | if ($funcname) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 432 | 0 | $funcname =~ s/\.\w+$//; | |||||||||||||||||||||||||||
| 433 | 0 | $funcname =~ s/[^\w]/_/g; | |||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||
| 435 | 0 | $funcname = 'render_' . $funcname; | |||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | 0 | my $str = "sub $funcname { my (\$_context) = \@_; "; | |||||||||||||||||||||||||||
| 439 | 0 | foreach (@args) { | |||||||||||||||||||||||||||
| 440 | 0 | $str .= "my \$$_ = \$_context->{'$_'}; "; | |||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||
| 442 | 0 | $str .= $self->{script}; | |||||||||||||||||||||||||||
| 443 | 0 | $str .= "}\n"; | |||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | 0 | return $str; | |||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | =head2 compile() | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | =cut | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | ## compile $self->{script} into closure. | ||||||||||||||||||||||||||||
| 453 | sub compile { | ||||||||||||||||||||||||||||
| 454 | 18 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
compile: called CC=2 SCAR=8.4
Pod
documented
| |||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | 18 | ▶ | if ($self->{args}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 457 | 0 | $self->{func} = $Tenjin::CONTEXT_CLASS->to_func($self->{script}, $self->{name}); | |||||||||||||||||||||||||||
| 458 | 0 | return $self->{func}; | |||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||
| 460 | 18 | return; | |||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | =head2 escaped_expr( $expr ) | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | Receives a Perl expression (from C<[= $expr =]>) and escapes it. This will | ||||||||||||||||||||||||||||
| 466 | happen in one of three ways: with the escape function defined in | ||||||||||||||||||||||||||||
| 467 | C<< $opts->{escapefunc} >> (if defined), with a scalar string (if | ||||||||||||||||||||||||||||
| 468 | C<< $opts->{rawclass} >> is defined), or with C<escape_xml()> from | ||||||||||||||||||||||||||||
| 469 | L<Tenjin::Util>, which uses L<HTML::Entites>. | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | =cut | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | sub escaped_expr { | ||||||||||||||||||||||||||||
| 474 | 0 | ▶ | my ($self, $expr) = @_; | ||||||||||||||||||||||||||
|
Subroutine
escaped_expr: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | 0 | ▶ | return "$self->{escapefunc}($expr)" if $self->{escapefunc}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 0 | ▶ | return "(ref(\$_V = ($expr)) eq '$self->{rawclass}' ? \$_V->{str} : escape_xml($expr)" if $self->{rawclass}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 0 | return "escape_xml($expr)"; | |||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | =head2 _read_file( $filename, [$lock_required] ) | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | Receives an absolute path to a template file, reads its content and | ||||||||||||||||||||||||||||
| 486 | returns it. If C<$lock_required> is passed (and has a true value), the | ||||||||||||||||||||||||||||
| 487 | file will be locked for reading. | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | =cut | ||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | sub _read_file { | ||||||||||||||||||||||||||||
| 492 | 18 | ▶ | my ($self, $filename, $lock_required) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_read_file: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | 18 | ▶ | open(IN, "<:encoding($Tenjin::ENCODING)", $filename) | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 495 | or croak "[Tenjin] Can't open $filename for reading: $!"; | ||||||||||||||||||||||||||||
| 496 | 18 | ▶ | flock(IN, LOCK_SH) if $lock_required; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | 18 | read(IN, my $content, -s $filename); | |||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | 18 | close(IN); | |||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | 18 | return $content; | |||||||||||||||||||||||||||
| 503 | } | ||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | =head2 _write_file( $filename, $content, [$lock_required] ) | ||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | Receives an absolute path to a template file and the templates contents, | ||||||||||||||||||||||||||||
| 508 | and creates the file (or truncates it, if existing) with that contents. | ||||||||||||||||||||||||||||
| 509 | If C<$lock_required> is passed (and has a true value), the file will be | ||||||||||||||||||||||||||||
| 510 | locked exclusively when writing. | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | =cut | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | sub _write_file { | ||||||||||||||||||||||||||||
| 515 | 16 | ▶ | my ($self, $filename, $content, $lock_required) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_write_file: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | 16 | ▶ | my $enc = $Tenjin::ENCODING eq 'UTF-8' ? '>:utf8' : ">:encoding($Tenjin::ENCODING)"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | 16 | ▶ | open(OUT, $enc, $filename) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 520 | or croak "[Tenjin] Can't open $filename for writing: $!"; | ||||||||||||||||||||||||||||
| 521 | 16 | ▶ | flock(OUT, LOCK_EX) if $lock_required; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 522 | 16 | print OUT $content; | |||||||||||||||||||||||||||
| 523 | 16 | close(OUT); | |||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | 8 | 1; | |||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | L<Tenjin>. | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | =head1 AUTHOR, LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | See L<Tenjin>. | ||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||
| 536 | =cut | ||||||||||||||||||||||||||||