blib/lib/only/matching.pm

filter: Statementstmt 95.8% 70 / 73 Branchbran 75.0% 12 / 16 Conditioncond 66.6% 8 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 18 / 18 Podpod n/a totaltotal 87.3% 110 / 126 CC 13 SCAR 18.5
CC
13
Cov
89%
import
13.3
BEGIN
1.0
BEGIN
1.0
CRAP 6.3
↑ Summary
1 package only::matching;
2  
3 =pod
4  
5 =head1 NAME
6  
7 only::matching - Check that two Perl files are version-locked
8  
9 =head1 SYNOPSIS
10  
11 At the start of your application's main module...
12  
13 package Foo;
14
15 use strict;
16 use vars qw{$VERSION};
17 BEGIN {
18 $VERSION = '1.00';
19 }
20  
21 # ...code...
22  
23 And at the top of your front-end script that loads the module...
24  
25 #!/usr/bin/perl
26
27 use strict;
28 use vars qw{$VERSION};
29 BEGIN {
30 $VERSION = '1.00';
31 }
32
33 # Load our matching module
34 use only::matching 'Foo';
35
36 # ...code...
37  
38 =head1 DESCRIPTION
39  
40 The L<only> module provides a great deal of interesting and rich
41 functionality, allowing you to install multiple copies of modules
42 and limit the version of a module you load to various arbitrary
43 patterns.
44  
45 However, installing it creates some additional directories to your
46 library tree for the multi-version support, and you have to be
47 explicit about the versions you want to be compatible with.
48  
49 This means for the case where you have a script and a module
50 and it is important that no matter what happens with system paths
51 or @INC paths, the script B<always> loads the matching module,
52 you need to change the code each revision to refer to the new
53 version, or you have to do something like...
54  
55 use only 'Foo' => $VERSION;
56  
57 Like L<only::latest>, B<only::matching> is a task-specific version
58 of L<only> for the specific case of having version-locked script
59 to module loading.
60  
61 Instead of the above, you say...
62  
63 use only::matching 'Foo';
64  
65 ... and you are guaranteed to get the correctly matching module
66 version.
67  
68 Because it only needs such limited and specific functionality
69 B<only::matching> also removes the multiversion support and is
70 contained entirely in one small .pm file, to make bundling it a
71 little easier as well.
72  
73 =head2 Providing Params
74  
75 The syntax for B<only::matching> is the same as for L<only>,
76 except without the version number string.
77  
78 Thus to load a module with default imports:
79  
80 # These are equivalent
81 use Foo;
82 use only::matching 'Foo';
83  
84 To load a module passing params
85  
86 # These are also equivalent
87 use Foo => 'bar', 'baz';
88 use only::matching Foo => 'bar', 'baz';
89  
90 To load a module explicitly without calling import
91  
92 # And these are equivalent
93 use Foo ();
94 use only::matching 'Foo', [];
95  
96 Other than this, there's very little that you need to know.
97  
98 =cut
99  
100 2 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
103 2 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
104 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
105 2 $VERSION = '0.02';
106 }
107  
108 12 ▶sub import {
Subroutine import: called CC=13 SCAR=25.9
109 12 my $class = shift;
110  
111 # What are we loading
112 12 my($mod, @imports) = @_;
113 12 ▶ @imports = () unless @imports;
Branch: 1 branch
BranchTrueFalseTotal
unless @imports 10 2 12
114  
115 # What called us
116 12 my $pkg = caller();
117  
118 SCOPE: {
119 # eval sometimes interferes with $!
120 12 local ($!);
121  
122 12 ▶ if ( @imports == 1 and $imports[0] =~ /^\d+(?:\.\d+)?$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@imports == 1 and $imports[0] =~ /^\d+(?:\.\d+)?$/) { } 0 12 12
elsif (@imports == 1 and ref $imports[0] eq 'ARRAY' and @{$imports[0];} == 0) { } 1 11 12
Condition66%2/3
@imports == 1 and $imports[0] =~ /^\d+(?:\.\d+)?$/
!ll&&!rl&&r
1110
MC/DC0%0/2
@imports == 1 and $imports[0] =~ /^\d+(?:\.\d+)?$/
@imports == 1 $imports[0] =~ /^\d+(?:\.\d+)?$/
Truth table66%2/3
A and B
A= @imports == 1B= $imports[0] =~ /^\d+(?:\.\d+)?$/
ABresult
0X0
100
111
123 # probably a version check. Perl needs to see the bare number
124 # for it to work with non-Exporter based modules.
125 0 eval <<"END_PERL";
126 package $pkg;
127 use $mod $imports[0];
128 END_PERL
129  
130 1 ▶ } elsif ( @imports == 1 and ref($imports[0]) eq 'ARRAY' and @{$imports[0]} == 0 ) {
Condition66%4/6
@imports == 1 and ref $imports[0] eq "ARRAY" and @{$imports[0];} == 0
!ll&&!rl&&r
1101
@imports == 1 and ref $imports[0] eq "ARRAY"
!ll&&!rl&&r
1101
MC/DC33%1/3
@imports == 1 and ref $imports[0] eq "ARRAY" and @{$imports[0];} == 0
@imports == 1 ref $imports[0] eq "ARRAY" @{$imports[0];} == 0
Truth table50%2/4
A and B and C
A= @imports == 1B= ref $imports[0] eq "ARRAY"C= @{$imports[0];} == 0
ABCresult
0XX0
10X0
1100
1111
131 # Called with [], which turns into "use module ();"
132 1 ▶ eval <<"END_PERL";
Subroutine BEGIN: called CC=1 SCAR=0.0
133 package $pkg;
134 use $mod ();
135 END_PERL
136  
137 } else {
138 # Just a regular call, pass on imports
139 11 ▶ eval <<"END_PERL";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
140 package $pkg;
141 use $mod \@imports;
142 END_PERL
143 }
144 }
145  
146 # Rethrow any errors
147 12 ▶ Carp::croak($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 12 12
148  
149 # Get the versions of caller and module via official channels
150 12 my $pkg_version = $pkg->VERSION;
151 7 my $mod_version = $mod->VERSION;
152  
153 # Check that both exist and are the same thing.
154 # Since they should be IDENTICAL, check with BOTH of == and eq
155 7 ▶ unless ( defined $pkg_version ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pkg_version) 1 6 7
156 1 Carp::croak("Calling package $pkg does not have a version");
157 }
158 6 ▶ unless ( defined $mod_version ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mod_version) 0 6 6
159 0 Carp::croak("$mod does not have a version");
160 }
161 6 ▶ unless ( ref($pkg_version) eq ref($mod_version) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $pkg_version eq ref $mod_version) 0 6 6
162 0 Carp::croak("Caller $pkg and module $mod version ref type mismatch");
163 }
164 6 local $^W = 0;
165 6 ▶ unless ( $pkg_version eq $mod_version and $pkg_version == $mod_version ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkg_version eq $mod_version and $pkg_version == $mod_version) 3 3 6
Condition66%2/3
$pkg_version eq $mod_version and $pkg_version == $mod_version
!ll&&!rl&&r
303
MC/DC50%1/2
$pkg_version eq $mod_version and $pkg_version == $mod_version
$pkg_version eq $mod_version $pkg_version == $mod_version
Truth table66%2/3
A and B
A= $pkg_version eq $mod_versionB= $pkg_version == $mod_version
ABresult
0X0
100
111
166 3 Carp::croak("$mod version $mod_version does not match caller $pkg $pkg_version");
167 }
168  
169 # Looks good
170 3 return 1;
171 }
172  
173 2 1;
174  
175 =pod
176  
177 =head1 SUPPORT
178  
179 This module is stored in an Open Repository at the following address.
180  
181 L<http://svn.ali.as/cpan/trunk/only-matching>
182  
183 Write access to the repository is made available automatically to any
184 published CPAN author, and to most other volunteers on request.
185  
186 If you are able to submit your bug report in the form of new (failing)
187 unit tests, or can apply your fix directly instead of submitting a patch,
188 you are B<strongly> encouraged to do so as the author currently maintains
189 over 100 modules and it can take some time to deal with non-Critcal bug
190 reports or patches.
191  
192 This will guarentee that your issue will be addressed in the next
193 release of the module.
194  
195 If you cannot provide a direct test or fix, or don't have time to do so,
196 then regular bug reports are still accepted and appreciated via the CPAN
197 bug tracker.
198  
199 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=only-matching>
200  
201 For other issues, for commercial enhancement or support, or to have your
202 write access enabled for the repository, contact the author at the email
203 address above.
204  
205 =head1 AUTHORS
206  
207 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
208  
209 =head1 SEE ALSO
210  
211 L<only>, L<only::latest>
212  
213 =head1 COPYRIGHT
214  
215 Copyright 2006 Adam Kennedy.
216  
217 This program is free software; you can redistribute
218 it and/or modify it under the same terms as Perl itself.
219  
220 The full text of the license can be found in the
221 LICENSE file included with this module.
222  
223 =cut
↑ Summary