File Coverage

lib/Templer/Plugin/ShellCommand.pm
Criterion Covered Total %
statement 19 19 100.0
branch 2 2 100.0
condition 1 3 33.3
subroutine 4 4 100.0
pod 1 2 50.0
total 27 30 90.0


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3              
4             Templer::Plugin::ShellCommand - A plugin to execute commands.
5              
6             =cut
7              
8             =head1 SYNOPSIS
9              
10             The following is a good example use of this plugin
11              
12             title: About my site
13             hostname: run_command( hostname )
14             uptime: run_command( uptime )
15             ----
16            

This is , with uptime of

17             .

18              
19             =cut
20              
21             =head1 DESCRIPTION
22              
23             This plugin allows template variables to be set to the output of
24             executing shell-commands.
25              
26             =cut
27              
28             =head1 LICENSE
29              
30             This module is free software; you can redistribute it and/or modify it
31             under the terms of either:
32              
33             a) the GNU General Public License as published by the Free Software
34             Foundation; either version 2, or (at your option) any later version,
35             or
36              
37             b) the Perl "Artistic License".
38              
39             =cut
40              
41             =head1 AUTHOR
42              
43             Steve Kemp
44              
45             =cut
46              
47             =head1 COPYRIGHT AND LICENSE
48              
49             Copyright (C) 2012-2015 Steve Kemp .
50              
51             This library is free software. You can modify and or distribute it under
52             the same terms as Perl itself.
53              
54             =cut
55              
56             =head1 METHODS
57              
58             =cut
59              
60              
61 11     11   5020 use strict;
  11         40  
  11         270  
62 11     11   43 use warnings;
  11         16  
  11         2331  
63              
64              
65             package Templer::Plugin::ShellCommand;
66              
67              
68             =head2
69              
70             Constructor. No arguments are required/supported.
71              
72             =cut
73              
74             sub new
75             {
76 11     11 0 24 my ( $proto, %supplied ) = (@_);
77 11   33     58 my $class = ref($proto) || $proto;
78              
79 11         21 my $self = {};
80 11         22 bless( $self, $class );
81 11         109 return $self;
82             }
83              
84              
85             =head2 expand_variables
86              
87             This is the method which is called by the L
88             to expand the variables contained in a L object.
89              
90             Variables are written in the file in the form "key: value", and are
91             internally stored within the Page object as a hash.
92              
93             This method iterates over each key & value and updates any that
94             seem to refer to shell commands.
95              
96             =cut
97              
98             sub expand_variables
99             {
100 9     9 1 35 my ( $self, $site, $page, $data ) = (@_);
101              
102             #
103             # Get the page-variables in the template.
104             #
105 9         55 my %hash = %$data;
106              
107             #
108             # Look for a value of "run_command" in each key.
109             #
110 9         44 foreach my $key ( keys %hash )
111             {
112 66 100       158 if ( $hash{ $key } =~ /^run_command\((.*)\)/ )
113             {
114              
115             #
116             # Run a system command, and capture the output.
117             #
118 1         3 my $cmd = $1;
119              
120             #
121             # Strip leading/trailing whitespace.
122             #
123 1         7 $cmd =~ s/^\s+|\s+$//g;
124              
125 1         5045 $hash{ $key } = `$cmd`;
126             }
127              
128             }
129              
130             #
131             # Return.
132             #
133 9         75 return ( \%hash );
134             }
135              
136              
137             #
138             # Register the plugin.
139             #
140             Templer::Plugin::Factory->new()
141             ->register_plugin("Templer::Plugin::ShellCommand");