| 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 | <p>This is <!-- tmpl_var name='hostname' -->, with uptime of | ||||||||||||||||||||
| 17 | <!-- tmpl_var name='uptime' -->.</p> | ||||||||||||||||||||
| 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 <steve@steve.org.uk> | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | Copyright (C) 2012-2015 Steve Kemp <steve@steve.org.uk>. | ||||||||||||||||||||
| 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 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 62 | 11 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 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 | 11 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||
| 76 | 11 | my ( $proto, %supplied ) = (@_); | |||||||||||||||||||
| 77 | 11 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 11 | my $self = {}; | |||||||||||||||||||
| 80 | 11 | bless( $self, $class ); | |||||||||||||||||||
| 81 | 11 | return $self; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | =head2 expand_variables | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | This is the method which is called by the L<Templer::Plugin::Factory> | ||||||||||||||||||||
| 88 | to expand the variables contained in a L<Templer::Site::Page> 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 | 9 | ▶ | { | ||||||||||||||||||
|
Subroutine
expand_variables: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 100 | 9 | my ( $self, $site, $page, $data ) = (@_); | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | # | ||||||||||||||||||||
| 103 | # Get the page-variables in the template. | ||||||||||||||||||||
| 104 | # | ||||||||||||||||||||
| 105 | 9 | my %hash = %$data; | |||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | # | ||||||||||||||||||||
| 108 | # Look for a value of "run_command" in each key. | ||||||||||||||||||||
| 109 | # | ||||||||||||||||||||
| 110 | 9 | foreach my $key ( keys %hash ) | |||||||||||||||||||
| 111 | { | ||||||||||||||||||||
| 112 | 66 | ▶ | if ( $hash{ $key } =~ /^run_command\((.*)\)/ ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | { | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | # | ||||||||||||||||||||
| 116 | # Run a system command, and capture the output. | ||||||||||||||||||||
| 117 | # | ||||||||||||||||||||
| 118 | 1 | my $cmd = $1; | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | # | ||||||||||||||||||||
| 121 | # Strip leading/trailing whitespace. | ||||||||||||||||||||
| 122 | # | ||||||||||||||||||||
| 123 | 1 | $cmd =~ s/^\s+|\s+$//g; | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 1 | $hash{ $key } = `$cmd`; | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | # | ||||||||||||||||||||
| 131 | # Return. | ||||||||||||||||||||
| 132 | # | ||||||||||||||||||||
| 133 | 9 | return ( \%hash ); | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | # | ||||||||||||||||||||
| 138 | # Register the plugin. | ||||||||||||||||||||
| 139 | # | ||||||||||||||||||||
| 140 | 11 | Templer::Plugin::Factory->new() | |||||||||||||||||||
| 141 | ->register_plugin("Templer::Plugin::ShellCommand"); | ||||||||||||||||||||