File Coverage

lib/Devel/Required.pm
Criterion Covered Total %
statement 65 73 89.0
branch 33 44 75.0
condition 4 4 100.0
subroutine 9 9 100.0
pod n/a
total 111 130 85.3


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