line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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> 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
|
8
|
|
|
8
|
|
7018
|
use 5.006; |
|
8
|
|
|
|
|
28
|
|
|
8
|
|
|
|
|
314
|
|
43
|
|
|
|
|
|
|
|
44
|
8
|
|
|
8
|
|
46
|
use strict; |
|
8
|
|
|
|
|
15
|
|
|
8
|
|
|
|
|
268
|
|
45
|
8
|
|
|
8
|
|
42
|
use warnings; |
|
8
|
|
|
|
|
15
|
|
|
8
|
|
|
|
|
2120
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
our $VERSION = '0.0202'; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
## no critic (RequireArgUnpacking) |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Pragma handling |
53
|
|
|
|
|
|
|
sub import { |
54
|
32
|
|
|
32
|
|
85141
|
shift; |
55
|
|
|
|
|
|
|
|
56
|
32
|
|
|
|
|
72
|
my $package = shift @_; |
57
|
32
|
100
|
|
|
|
129
|
return unless $package; |
58
|
|
|
|
|
|
|
|
59
|
31
|
|
|
|
|
56
|
my $macro = $package; |
60
|
31
|
|
|
|
|
553
|
$macro =~ s{(::|[^A-Za-z0-9_])}{_}g; |
61
|
31
|
|
|
|
|
80
|
$macro = 'HAVE_' . uc($macro); |
62
|
|
|
|
|
|
|
|
63
|
31
|
|
|
|
|
131
|
(my $file = $package . '.pm') =~ s{::}{/}g; |
64
|
|
|
|
|
|
|
|
65
|
31
|
|
|
|
|
155
|
local $SIG{__DIE__} = ''; |
66
|
31
|
100
|
|
|
|
57
|
eval { |
67
|
31
|
|
|
|
|
14928
|
require $file; |
68
|
|
|
|
|
|
|
} or goto ERROR; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Check version if first element on list is a version number. |
71
|
20
|
100
|
100
|
|
|
360
|
if (defined $_[0] and $_[0] =~ m/^\d/) { |
72
|
7
|
|
|
|
|
20
|
my $version = shift @_; |
73
|
7
|
100
|
|
|
|
14
|
eval { |
74
|
7
|
|
|
|
|
180
|
$package->VERSION($version); |
75
|
3
|
|
|
|
|
17
|
1; |
76
|
|
|
|
|
|
|
} or goto ERROR; |
77
|
|
|
|
|
|
|
}; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# Package is just loaded |
80
|
|
|
|
|
|
|
{ |
81
|
8
|
|
|
8
|
|
48
|
no strict 'refs'; |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
1227
|
|
|
16
|
|
|
|
|
29
|
|
82
|
16
|
100
|
|
|
|
95
|
undef *{$macro} if defined &$macro; |
|
1
|
|
|
|
|
5
|
|
83
|
16
|
|
|
|
|
28
|
*{$macro} = sub () { !! 1 }; |
|
16
|
|
|
|
|
68
|
|
84
|
|
|
|
|
|
|
}; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Do not call import if list contains only empty string. |
87
|
16
|
100
|
100
|
|
|
123
|
return if @_ == 1 and defined $_[0] and $_[0] eq ''; |
|
|
|
100
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
13
|
|
|
|
|
137
|
my $method = $package->can('import'); |
90
|
13
|
100
|
|
|
|
117
|
return unless $method; |
91
|
|
|
|
|
|
|
|
92
|
10
|
|
|
|
|
29
|
unshift @_, $package; |
93
|
10
|
|
|
|
|
67
|
goto &$method; |
94
|
|
|
|
|
|
|
|
95
|
8
|
|
|
|
|
1183
|
ERROR: |
96
|
|
|
|
|
|
|
{ |
97
|
8
|
|
|
8
|
|
50
|
no strict 'refs'; |
|
8
|
|
|
|
|
105
|
|
|
15
|
|
|
|
|
330
|
|
98
|
15
|
100
|
|
|
|
472
|
undef *{$macro} if defined &$macro; |
|
1
|
|
|
|
|
5
|
|
99
|
15
|
|
|
|
|
36
|
*{$macro} = sub () { !! '' }; |
|
15
|
|
|
|
|
77
|
|
100
|
|
|
|
|
|
|
}; |
101
|
|
|
|
|
|
|
|
102
|
15
|
|
|
|
|
212
|
return; |
103
|
|
|
|
|
|
|
}; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
1; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 USAGE |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=over |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=item use maybe I; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
It is exactly equivalent to |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
BEGIN { eval { require Module; }; Module->import; } |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
except that I must be a quoted string. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=item use maybe I => I; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
It is exactly equivalent to |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
BEGIN { eval { require Module; }; Module->import( LIST ); } |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item use maybe I => I, I; |
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 => ''; |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
If the I 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 |
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, L, L. |
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 |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=for readme continue |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 AUTHOR |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Piotr Roszatycki |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 COPYRIGHT |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Copyright (C) 2008, 2009 by Piotr Roszatycki . |
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 |