blib/lib/maybe.pm

filter: Statementstmt 100% 47 / 47 Branchbran 81.2% 13 / 16 Conditioncond 44.4% 4 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 85.5% 71 / 83 CC 12 SCAR 22.0
CC
12
Cov
89%
import
12.3
BEGIN
1.0
BEGIN
1.0
CRAP 9.0
↑ Summary
1 #!/usr/bin/perl -c
2  
3 package maybe;
4  
5 =head1 NAME
6  
7 maybe - Use a Perl module and ignore error if can't be loaded
8  
9 =head1 SYNOPSIS
10  
11 use Getopt::Long;
12 use maybe 'Getopt::Long::Descriptive';
13 if (maybe::HAVE_GETOPT_LONG_DESCRIPTIVE) {
14 Getopt::Long::Descriptive::describe_options("usage: %c %o", @options);
15 }
16 else {
17 Getopt::Long::GetOptions(\%options, @$opt_spec);
18 }
19  
20 use maybe 'Carp' => 'confess';
21 if (maybe::HAVE_CARP) {
22 confess("Bum!");
23 }
24 else {
25 die("Bum!");
26 }
27  
28 =head1 DESCRIPTION
29  
30 This pragma loads a Perl module. If the module can't be loaded, the
31 error will be ignored. Otherwise, the module's import method is called
32 with unchanged caller stack.
33  
34 The special constant C<maybe::HAVE_I<MODULE>> is created and it can be used
35 to enable or disable block of code at compile time.
36  
37 =for readme stop
38  
39 =cut
40  
41  
42 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 3 our $VERSION = '0.0202';
48  
49  
50 ## no critic (RequireArgUnpacking)
51  
52 # Pragma handling
53 19 ▶sub import {
Subroutine import: called CC=12 SCAR=25.1
54 19 shift;
55  
56 19 my $package = shift @_;
57 19 ▶ return unless $package;
Branch: 1 branch
BranchTrueFalseTotal
unless $package 0 19 19
58  
59 19 my $macro = $package;
60 19 $macro =~ s{(::|[^A-Za-z0-9_])}{_}g;
61 19 $macro = 'HAVE_' . uc($macro);
62  
63 19 (my $file = $package . '.pm') =~ s{::}{/}g;
64  
65 19 local $SIG{__DIE__} = '';
66 19 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require $file } } or goto ERROR 12 7 19
67 19 require $file;
68 } or goto ERROR;
69  
70 # Check version if first element on list is a version number.
71 12 ▶ if (defined $_[0] and $_[0] =~ m/^\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] =~ /^\d/) 7 5 12
Condition66%2/3
defined $_[0] and $_[0] =~ /^\d/
!ll&&!rl&&r
507
MC/DC50%1/2
defined $_[0] and $_[0] =~ /^\d/
defined $_[0] $_[0] =~ /^\d/
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] =~ /^\d/
ABresult
0X0
100
111
72 7 my $version = shift @_;
73 7 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $package->VERSION($version); 1 } } or goto ERROR 3 4 7
74 7 $package->VERSION($version);
75 3 1;
76 } or goto ERROR;
77 };
78  
79 # Package is just loaded
80 {
81 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
82 8 ▶ undef *{$macro} if defined &$macro;
Branch: 1 branch
BranchTrueFalseTotal
if defined &$macro 1 7 8
83 8 *{$macro} = sub () { !! 1 };
84 };
85  
86 # Do not call import if list contains only empty string.
87 8 ▶ return if @_ == 1 and defined $_[0] and $_[0] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and defined $_[0] and $_[0] eq "" 0 8 8
Condition33%2/6
@_ == 1 and defined $_[0] and $_[0] eq ""
!ll&&!rl&&r
800
@_ == 1 and defined $_[0]
!ll&&!rl&&r
800
MC/DC0%0/3
@_ == 1 and defined $_[0] and $_[0] eq ""
@_ == 1 defined $_[0] $_[0] eq ""
Truth table25%1/4
A and B and C
A= @_ == 1B= defined $_[0]C= $_[0] eq ""
ABCresult
0XX0
10X0
1100
1111
88  
89 8 my $method = $package->can('import');
90 8 ▶ return unless $method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 8 8
91  
92 8 unshift @_, $package;
93 8 goto &$method;
94  
95 ERROR:
96 {
97 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
98 11 ▶ undef *{$macro} if defined &$macro;
Branch: 1 branch
BranchTrueFalseTotal
if defined &$macro 1 10 11
99 11 *{$macro} = sub () { !! '' };
100 };
101  
102 11 return;
103 };
104  
105  
106 3 1;
107  
108  
109 =head1 USAGE
110  
111 =over
112  
113 =item use maybe I<Module>;
114  
115 It is exactly equivalent to
116  
117 BEGIN { eval { require Module; }; Module->import; }
118  
119 except that I<Module> must be a quoted string.
120  
121 =item use maybe I<Module> => I<LIST>;
122  
123 It is exactly equivalent to
124  
125 BEGIN { eval { require Module; }; Module->import( LIST ); }
126  
127 =item use maybe I<Module> => I<version>, I<LIST>;
128  
129 It is exactly equivalent to
130  
131 BEGIN { eval { require Module; Module->VERSION(version); } Module->import( LIST ); }
132  
133 =item use maybe I<Module> => '';
134  
135 If the I<LIST> contains only one empty string, it is exactly equivalent to
136  
137 BEGIN { eval { require Module; }; }
138  
139 =back
140  
141 =head1 CONSTANTS
142  
143 =over
144  
145 =item HAVE_I<MODULE>
146  
147 This constant is set after trying to load the module. The name of constant is
148 created from uppercased module name. The "::" string and any non-alphanumeric
149 character is replaced with underscore. The constant contains the true value
150 if the module was loaded or false value otherwise.
151  
152 use maybe 'File::Spec::Win32';
153 return unless maybe::HAVE_FILE_SPEC_WIN32;
154  
155 As any constant value it can be used to enable or disable the block code at
156 compile time.
157  
158 if (maybe::HAVE_FILE_SPEC_WIN32) {
159 # This block is compiled only if File::Spec::Win32 was loaded
160 do_something;
161 }
162  
163 =back
164  
165 =head1 SEE ALSO
166  
167 L<if>, L<all>, L<first>.
168  
169 =head1 BUGS
170  
171 The Perl doesn't clean up the module if it wasn't loaded to the end, i.e.
172 because of syntax error.
173  
174 The name of constant could be the same for different modules, i.e. "Module",
175 "module" and "MODULE" generate maybe::HAVE_MODULE constant.
176  
177 If you find the bug or want to implement new features, please report it at
178 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=maybe>
179  
180 =for readme continue
181  
182 =head1 AUTHOR
183  
184 Piotr Roszatycki <dexter@cpan.org>
185  
186 =head1 COPYRIGHT
187  
188 Copyright (C) 2008, 2009 by Piotr Roszatycki <dexter@cpan.org>.
189  
190 This program is free software; you can redistribute it and/or modify it
191 under the same terms as Perl itself.
192  
193 See L<http://www.perl.com/perl/misc/Artistic.html>
↑ Summary