File Coverage

lib/Templer/Plugin/Perl.pm
Criterion Covered Total %
statement 24 26 92.3
branch 2 4 50.0
condition 1 3 33.3
subroutine 7 7 100.0
pod 3 3 100.0
total 37 43 86.0


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3            
4             Templer::Plugin::Perl - A simple inline-perl plugin
5            
6             =cut
7              
8             =head1 DESCRIPTION
9            
10             This class implements a formatter plugin for C<templer> which allows
11             inline code to be executed as the page is generated.
12            
13             This is carried out via the L<Text::Template> module, and allows input
14             such as this to be executed:
15            
16             =for example begin
17            
18             Title: This is my page
19             format: perl
20             name: Steve
21             ----
22             <p>The sum of 1 + 1 is { 1 + 1 }.</p>
23             <p>My name is { $name }</p>
24            
25             =for example end
26            
27             =cut
28              
29             =head1 LICENSE
30            
31             This module is free software; you can redistribute it and/or modify it
32             under the terms of either:
33            
34             a) the GNU General Public License as published by the Free Software
35             Foundation; either version 2, or (at your option) any later version,
36             or
37            
38             b) the Perl "Artistic License".
39            
40             =cut
41              
42             =head1 AUTHOR
43            
44             Steve Kemp <steve@steve.org.uk>
45            
46             =cut
47              
48             =head1 COPYRIGHT AND LICENSE
49            
50             Copyright (C) 2012-2015 Steve Kemp <steve@steve.org.uk>.
51            
52             This library is free software. You can modify and or distribute it under
53             the same terms as Perl itself.
54            
55             =cut
56              
57             =head1 METHODS
58            
59             =cut
60              
61              
62 11     11   3970 use strict;
  11         13  
  11         252  
63 11     11   32 use warnings;
  11         11  
  11         1703  
64              
65              
66             package Templer::Plugin::Perl;
67              
68              
69              
70             =head2 new
71            
72             Constructor. No arguments are supported/expected.
73            
74             =cut
75              
76             sub new
77             {
78 5     5 1 7     my ( $proto, %supplied ) = (@_);
79 5   33     19     my $class = ref($proto) || $proto;
80              
81 5         6     my $self = {};
82 5         7     bless( $self, $class );
83 5         9     return $self;
84             }
85              
86              
87             =head2 available
88            
89             This plugin is available if we've got the L<Text::Template> module.
90            
91             =cut
92              
93             sub available
94             {
95 2     2 1 3     my $str = "use Text::Template;";
96              
97             ## no critic (Eval)
98 2     1   93     eval($str);
  1     1   549  
  1         2306  
  1         27  
  1         4  
  1         1  
  1         25  
99             ## use critic
100              
101 2 50       8     return ( $@ ? undef : 1 );
102             }
103              
104              
105             =head2 format
106            
107             Format the given text.
108            
109             =cut
110              
111             sub format
112             {
113 1     1 1 1     my ( $self, $str, $data ) = (@_);
114              
115 1 50       3     if ( $self->available() )
116                 {
117 1         4         my $template =
118                       Text::Template->new( TYPE => "STRING",
119                                            SOURCE => $str );
120 1         74         return ( $template->fill_in( HASH => $data ) );
121                 }
122                 else
123                 {
124 0         0         warn
125                       "Perl formatting disabled as the Text::Template module isn't present.\n";
126 0         0         $str;
127                 }
128             }
129              
130             Templer::Plugin::Factory->new()
131               ->register_formatter( "perl", "Templer::Plugin::Perl" );
132