File Coverage

blib/lib/Make/Functions.pm
Criterion Covered Total %
statement 42 42 100.0
branch 2 2 100.0
condition n/a
subroutine 11 11 100.0
pod 9 9 100.0
total 64 64 100.0


line stmt bran cond sub pod time code
1             package Make::Functions;
2              
3 1     1   7 use strict;
  1         2  
  1         32  
4 1     1   5 use warnings;
  1         2  
  1         584  
5              
6             our $VERSION = '2.011';
7              
8             my @temp_handles; # so they don't get destroyed before end of program
9              
10             sub wildcard {
11 1     1 1 20 my ( $fsmap, @args ) = @_;
12             ## no critic (BuiltinFunctions::RequireBlockMap)
13 1         17 return map $fsmap->{glob}->($_), @args;
14             ## use critic
15             }
16              
17             sub shell {
18 2     2 1 6 my ( $fsmap, @args ) = @_;
19 2         11470 my $value = `@args`;
20 2         46 chomp $value;
21 2         167 return split "\n", $value;
22             }
23              
24             sub addprefix {
25 1     1 1 14 my ( $fsmap, $prefix, $text_input ) = @_;
26             ## no critic (BuiltinFunctions::RequireBlockMap)
27 1         9 return map $prefix . $_, @{ Make::tokenize($text_input) };
  1         18  
28             ## use critic
29             }
30              
31             sub addsuffix {
32 1     1 1 18 my ( $fsmap, $suffix, $text_input ) = @_;
33             ## no critic (BuiltinFunctions::RequireBlockMap)
34 1         8 return map $_ . $suffix, @{ Make::tokenize($text_input) };
  1         6  
35             ## use critic
36             }
37              
38             sub notdir {
39 1     1 1 5 my ( $fsmap, $text_input ) = @_;
40 1         9 my @files = @{ Make::tokenize($text_input) };
  1         12  
41 1         16 s#^.*/## for @files;
42 1         11 return @files;
43             }
44              
45             sub dir {
46 1     1 1 5 my ( $fsmap, $text_input ) = @_;
47 1         2 my @files = @{ Make::tokenize($text_input) };
  1         5  
48 1         4 foreach (@files) {
49 3 100       31 $_ = './' unless s#^(.*)/[^/]*$#$1#;
50             }
51 1         12 return @files;
52             }
53              
54             sub subst {
55 2     2 1 6 my ( $fsmap, $from, $to, $value ) = @_;
56 2         4 $from = quotemeta $from;
57 2         30 $value =~ s/$from/$to/g;
58 2         9 return $value;
59             }
60              
61             sub patsubst {
62 1     1 1 4 my ( $fsmap, $from, $to, $value ) = @_;
63 1         2 $from = quotemeta $from;
64 1         24 $value =~ s/$from(?=(?:\s|\z))/$to/g;
65 1         6 return $value;
66             }
67              
68             sub mktmp {
69 1     1 1 4 my ( $fsmap, $text_input ) = @_;
70 1         45 my $fh = File::Temp->new; # default UNLINK = 1
71 1         1342 push @temp_handles, $fh;
72 1         7 print $fh $text_input;
73 1         15 return $fh->filename;
74             }
75              
76             =head1 NAME
77              
78             Make::Functions - Functions in Makefile macros
79              
80             =head1 SYNOPSIS
81              
82             require Make::Functions;
83             my ($dir) = Make::Functions::dir($fsmap, "x/y");
84             # $dir now "x"
85              
86             =head1 DESCRIPTION
87              
88             Package that contains the various functions used by L.
89              
90             =head1 FUNCTIONS
91              
92             Implements GNU-make style functions. The call interface for all these
93             Perl functions is:
94              
95             my @return_list = func($fsmap, @args);
96              
97             The args will have been extracted from the Makefile, comma-separated,
98             as in GNU make. The first arg is a L.
99              
100             =head2 wildcard
101              
102             Returns all its args expanded using C.
103              
104             =head2 shell
105              
106             Runs the command, returns the output with all newlines replaced by spaces.
107              
108             =head2 addprefix
109              
110             Prefixes each word in the second arg with first arg:
111              
112             $(addprefix x/,1 2)
113             # becomes x/1 x/2
114              
115             =head2 addsuffix
116              
117             Suffixes each word in the second arg with first arg:
118              
119             $(addprefix /x,1 2)
120             # becomes 1/x 2/x
121              
122             =head2 notdir
123              
124             Returns everything after last C.
125              
126             =head2 dir
127              
128             Returns everything up to last C. If no C, returns C<./>.
129              
130             =head2 subst
131              
132             In the third arg, replace every instance of first arg with second. E.g.:
133              
134             $(subst .o,.c,a.o b.o c.o)
135             # becomes a.c b.c c.c
136              
137             Since, as with GNU make, all whitespace gets ignored in the expression
138             I, and the commas cannot be quoted, you need to use variable
139             expansion for some scenarios:
140              
141             comma = ,
142             empty =
143             space = $(empty) $(empty)
144             foo = a b c
145             bar = $(subst $(space),$(comma),$(foo))
146             # bar is now "a,b,c"
147              
148             =head2 patsubst
149              
150             Like L, but only operates when the pattern is at the end of
151             a word.
152              
153             =head2 mktmp
154              
155             Like the dmake macro, but does not support a file argument straight
156             after the macro-name.
157              
158             The text after further whitespace is inserted in a temporary file,
159             whose name is returned. E.g.:
160              
161             $(mktmp $(shell echo hi))
162             # becomes a temporary filename, and that file contains "hi"
163              
164             =head1 COPYRIGHT AND LICENSE
165              
166             Copyright (c) 1996-1999 Nick Ing-Simmons.
167              
168             This program is free software; you can redistribute it and/or
169             modify it under the same terms as Perl itself.
170              
171             =cut
172              
173             1;