File Coverage

blib/lib/Make/Functions.pm
Criterion Covered Total %
statement 39 39 100.0
branch 2 2 100.0
condition n/a
subroutine 10 10 100.0
pod 8 8 100.0
total 59 59 100.0


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