File Coverage

lib/Panotools/Makefile/Variable.pm
Criterion Covered Total %
statement 40 44 90.9
branch 2 4 50.0
condition 1 3 33.3
subroutine 7 9 77.7
pod 0 6 0.0
total 50 66 75.7


line stmt bran cond sub pod time code
1             package Panotools::Makefile::Variable;
2              
3             =head1 NAME
4              
5             Panotools::Makefile::Variable - Assemble Makefile Variable definitions
6              
7             =head1 SYNOPSIS
8              
9             Simple interface for generating Makefile syntax
10              
11             =head1 DESCRIPTION
12              
13             Writing Makefiles directly from perl scripts with print and "\t" etc... is
14             prone to error, this library provides a simple perl interface for assembling
15             Makefiles.
16              
17             =cut
18              
19 1     1   49232 use strict;
  1         3  
  1         30  
20 1     1   5 use warnings;
  1         2  
  1         32  
21              
22 1     1   382 use Panotools::Makefile::Utils qw/quotetarget quoteprerequisite quoteshell/;
  1         2  
  1         551  
23              
24             =head1 USAGE
25              
26             $var = new Panotools::Makefile::Variable;
27              
28             ..or define the 'variable name' at the same time:
29              
30             $var = new Panotools::Makefile::Variable ('USERS');
31              
32             ..or define the name and values at the same time:
33              
34             $var = new Panotools::Makefile::Variable ('USERS', 'Andy Pandy');
35              
36             =cut
37              
38             sub new
39             {
40 2     2 0 1278 my $class = shift;
41 2   33     14 $class = ref $class || $class;
42 2         16 my $self = bless {name => shift, value => [@_]}, $class;
43 2         7 return $self;
44             }
45              
46             =pod
47              
48             Set or query the name:
49              
50             $var->Name ('USERS');
51             $text = $var->Name;
52              
53             =cut
54              
55             sub Name
56             {
57 1     1 0 10 my $self = shift;
58 1 50       40 $self->{name} = shift if @_;
59 1         3 return $self->{name};
60             }
61              
62             sub NameRef
63             {
64 0     0 0 0 my $self = shift;
65 0         0 return '$('. $self->Name .')';
66             }
67              
68             sub NameRefShell
69             {
70 0     0 0 0 my $self = shift;
71 0         0 return '$('. $self->Name .'_SHELL)';
72             }
73              
74             =pod
75              
76             $var->Values ('James Brine', 'George Loveless');
77             $var->Values ('Thomas Standfield');
78              
79             =cut
80              
81             sub Values
82             {
83 4     4 0 15 my $self = shift;
84 4         6 push @{$self->{value}}, @_;
  4         12  
85 4         10 return $self->{value};
86             }
87              
88             =pod
89              
90             Construct a text fragment that defines this variable suitable for use in a
91             Makefile like so:
92              
93             $text = $var->Assemble;
94              
95             =cut
96              
97             sub Assemble
98             {
99 8     8 0 19 my $self = shift;
100 8 50       19 return '' unless defined $self->{name};
101              
102 8         8 my $text;
103 8         21 $text .= quotetarget ($self->{name});
104 8         14 $text .= ' = ';
105 8         10 $text .= join ' ', (map { quotetarget ($_)} grep /./, @{$self->{value}});
  24         52  
  8         38  
106 8         20 $text .= "\n";
107 8         81 $text .= quotetarget ($self->{name} .'_SHELL');
108 8         11 $text .= ' = ';
109 8         11 my @items = map { quoteshell ($_)} grep /./, @{$self->{value}};
  24         61  
  8         35  
110 8         13 @items = map {s/\$\(([^)]+)\)/\$($1_SHELL)/g; $_} @items;
  24         33  
  24         40  
111 8         19 $text .= join ' ', @items;
112 8         12 $text .= "\n";
113 8         50 return $text;
114             }
115              
116             1;