File Coverage

blib/lib/Devel/Required.pm
Criterion Covered Total %
statement 70 76 92.1
branch 33 44 75.0
condition 4 4 100.0
subroutine 10 10 100.0
pod n/a
total 117 134 87.3


line stmt bran cond sub pod time code
1             package Devel::Required;
2              
3             # set version information
4             $VERSION= '0.17';
5              
6             # make sure we do everything by the book from now on
7 3     3   497409 use strict;
  3         7  
  3         131  
8 3     3   36 use warnings;
  3         7  
  3         155  
9 3     3   2924 use ExtUtils::MakeMaker ();
  3         512951  
  3         236  
10              
11             # initializations
12             my @TEXT; # list with text-file conversion
13             my @POD; # list with pod-file conversion
14             my $INSTALLATION; # any installation information
15              
16             # replace WriteMakefile with our own copy
17             BEGIN {
18 3     3   31 no warnings 'redefine';
  3         5  
  3         205  
19 3     3   22 no strict 'refs';
  3         9  
  3         2435  
20 3     3   17 my $subname= 'ExtUtils::MakeMaker::WriteMakefile';
21 3         7 my $old= \&{$subname};
  3         20  
22             *$subname= sub {
23              
24             # perform the old sub with parameters
25 5     5   517827 @_= @_; # quick fix for brokennes in 5.9.5, as suggested by rgs
26 5         58 $old->( @_ );
27              
28             # initializations
29 5         3347734 my $pod; # pod filename to change
30             my $modules; # hash reference to the module info
31 5         0 my $required; # required text to replace
32 5         0 my $version; # version text to replace
33              
34             # each key and value pair passed to original WriteMakefile
35 5         53 while (@_) {
36 14         62 my ( $key, $value )= ( shift, shift );
37              
38             # main module file
39 14 100       73 if ( $key eq 'VERSION_FROM' ) {
    100          
40 5         16 $pod= $value;
41             }
42              
43             # required modules hash ref
44             elsif ($key eq 'PREREQ_PM') {
45 4         16 $modules= $value;
46             }
47              
48             =for Explanation:
49             Anything we don't handle is simply ignored.
50              
51             =cut
52              
53             }
54              
55             # use E::M's logic to obtain version information
56 5         48 ($version)= _slurp('Makefile') =~ m#\nVERSION = (\d+\.\d+)#s;
57              
58             # text to insert
59             $required= join $/,
60 6   100     68 map {" $_ (".($modules->{$_} || 'any').")"}
61 3         19 sort {lc $a cmp lc $b}
62 5 100       55 keys %{$modules}
  4         41  
63             if $modules;
64 5   100     35 $required ||= " (none)";
65              
66             # convert all text files that matter
67 5 100       34 foreach ( grep { -s } @TEXT ? @TEXT : 'README' ) {
  5         96  
68 5 50       88 _convert( $_, "Version:$/", " $version", "$/$/" )
69             if $version;
70 5         45 _convert( $_, "Required Modules:$/", $required, "$/$/" );
71 5 100       45 _convert( $_, "Installation:$/", $INSTALLATION, "$/$/$/" )
72             if $INSTALLATION;
73             }
74              
75             # convert all pod files that matter
76 5 50       32 foreach ( grep { -s } @POD ? @POD : ($pod ? ($pod) : () ) ) {
  5 100       89  
77 5 50       60 _convert(
78             $_,
79             "=head1 VERSION$/",
80             "$/This documentation describes version $version.$/",
81             "$/="
82             ) if $version;
83 5         78 _convert( $_, "=head1 REQUIRED MODULES$/", "$/$required$/", "$/=" );
84 5 100       74 _convert( $_, "=head1 INSTALLATION$/", "$/$INSTALLATION$/", "$/=")
85             if $INSTALLATION;
86             }
87 3         2597 };
88             } #BEGIN
89              
90             # satisfy -require-
91             1;
92              
93             #-------------------------------------------------------------------------------
94             #
95             # Standard Perl features
96             #
97             #-------------------------------------------------------------------------------
98             # IN: 1 class (ignored)
99             # 2..N key/value pairs
100              
101             sub import {
102              
103             # lose the class
104 4     4   6546 shift;
105              
106             # for all key value pairs
107 4         61 while (@_) {
108 4         18 my ( $type, $value )= ( shift, shift );
109              
110             # set up text file processing
111 4 100       30 if ( $type eq 'text' ) {
    100          
    100          
112 1 50       40 push @TEXT, ref $value ? @{$value} : ($value);
  0         0  
113             }
114              
115             # set up pod file processing
116             elsif ( $type eq 'pod' ) {
117 1 50       6 push @POD,ref $value ? @{$value} : ($value);
  0         0  
118             }
119              
120             # set up maint/blead installation information
121             elsif ( $type eq 'maint_blead' ) {
122 1         13 $INSTALLATION= <<"INSTALLATION";
123             This distribution contains two versions of the code: one maintenance version
124             for versions of perl < $value (known as 'maint'), and the version currently in
125             development (known as 'blead'). The standard build for your perl version is:
126              
127             perl Makefile.PL
128             make
129             make test
130             make install
131              
132             This will try to test and install the "blead" version of the code. If the
133             Perl version does not support the "blead" version, then the running of the
134             Makefile.PL will *fail*. In such a case, one can force the installing of
135             the "maint" version of the code by doing:
136              
137             perl Makefile.PL maint
138              
139             Alternately, if you want automatic selection behavior, you can set the
140             AUTO_SELECT_MAINT_OR_BLEAD environment variable to a true value. On Unix-like
141             systems like so:
142              
143             AUTO_SELECT_MAINT_OR_BLEAD=1 perl Makefile.PL
144              
145             If your perl does not support the "blead" version of the code, then it will
146             automatically install the "maint" version of the code.
147              
148             Please note that any additional parameters will simply be passed on to the
149             underlying Makefile.PL processing.
150             INSTALLATION
151 1         63 chomp $INSTALLATION;
152             }
153              
154             # huh?
155             else {
156 1         9 die qq{Don't know how to handle "$type"\n};
157             }
158             }
159             } #import
160              
161             #-------------------------------------------------------------------------------
162             #
163             # Internal subroutines
164             #
165             #-------------------------------------------------------------------------------
166             # _convert
167             #
168             # Perform the indicated conversion on the specified file
169             #
170             # IN: 1 filename
171             # 2 string before to match
172             # 3 string to insert between before and after
173             # 4 string to match with after
174              
175             sub _convert {
176 22     22   68 my ( $filename, $before, $text, $after )= @_;
177 22         29 local $_;
178              
179             =for Explanation:
180             We want to make sure that this also runs on pre 5.6 perl's, so we're
181             using old style open()
182              
183             =cut
184              
185             # there is something to process
186 22 50       45 if ( my $contents= $_= _slurp($filename) ) {
187              
188             # found and replaced text
189 22 50       712 if ( s#$before(?:.*?)$after#$before$text$after#s ) {
190              
191             # same as before (no action)
192 22 100       1175 if ($_ eq $contents) {
    50          
193             }
194              
195             # successfully saved file with changes
196             elsif ( open( OUT, ">$filename" ) ) {
197 16         81 print OUT $_;
198 16 50       1673 close OUT
199             or die qq{Problem flushing "$filename": $!\n};
200 16 50       339 die qq{Did not properly install "$filename"\n}
201             unless -s $filename == length;
202             }
203              
204             # could not save file
205             else {
206 0         0 warn qq{Could not open "$filename" for writing: $!\n};
207             }
208             }
209              
210             # couldn't replace
211             else {
212 0         0 $before =~ s#\s+$##s;
213 0         0 warn qq{Could not find text marker "$before" in "$filename"\n};
214             }
215             }
216             } #_convert
217              
218             #-------------------------------------------------------------------------------
219             # _slurp
220             #
221             # Return contents of given filename, a poor man's perl6 slurp(). Warns if
222             # it could not open the specified file
223             #
224             # IN: 1 filename
225             # OUT: 1 file contents
226              
227             sub _slurp {
228 27     27   49 my ($filename)= @_;
229 27         36 my $contents;
230              
231             # there is something to process
232 27 50       766 if ( open( IN, $filename ) ) {
233 27         46 $contents= do { local $/; };
  27         90  
  27         924  
234 27         251 close IN;
235             }
236              
237             # couldn't read file
238             else {
239 0         0 warn qq{Could not open "$filename" for reading: $!\n};
240             }
241              
242 27         178 return $contents;
243             } #_slurp
244              
245             #-------------------------------------------------------------------------------
246              
247             __END__