blib/lib/App/RecordStream/OptionalRequire.pm

filter: Statementstmt 86.6% 39 / 45 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 4 totaltotal 84.7% 61 / 72 CC 5 SCAR 3.2
CC
5
Cov
87%
optional_use_with_caller
3.0
use_module
2.1
require_done
2.1
CRAP 1.4
« blib/lib/App/RecordStream/Test/UniqConcatHelper.pm ↑ Summary blib/lib/App/RecordStream/Aggregator/Percentile.pm »
1 package App::RecordStream::OptionalRequire;
2  
3 =head1 NAME
4  
5 App::RecordStream::OptionalRequire
6  
7 =head1 AUTHOR
8  
9 Benjamin Bernard <perlhacker@benjaminbernard.com>
10 Keith Amling <keith.amling@gmail.com>
11  
12 =head1 DESCRIPTION
13  
14 Class for optionally requiring a set of modules
15  
16 =head1 SYNOPSIS
17  
18 BEGIN {
19 use App::RecordStream::OptionalRequire qw(optional_require);
20 optional_require(qw(Foo::Bar Biz::Zip));
21 }
22  
23 =cut
24  
25 54 our $VERSION = "4.0.25";
26  
27 70 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 59 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # Set to this 0 if you don't want the warnings printed
31 55 our $PRINT_WARNING = 1;
32  
33 54 my @missing_modules;
34  
35 55 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
36 55 my $class = shift;
37 52 my $calling_package = (caller())[0];
38 52 return optional_use_with_caller($calling_package, @_);
39 }
40  
41 # For testing and calling outside of other things... CHECK will not work in this case...
42 6 ▶sub optional_use {
Subroutine optional_use: called CC=1 SCAR=0.0
Pod undocumented
43 6 my $calling_package = (caller())[0];
44 3 return optional_use_with_caller($calling_package, @_);
45 }
46  
47 56 ▶sub optional_use_with_caller {
Subroutine optional_use_with_caller: called CC=3 SCAR=11.0
Pod undocumented
48 55 my $calling_package = shift;
49  
50 56 my $loaded;
51  
52 55 $loaded = use_module($calling_package, @_);
53 55 my $module_name = $_[0];
54  
55 56 ▶ unless ( $loaded ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loaded) 41 14 55
56 42 ▶ warn "$0 requires missing module $module_name\n" if ( $PRINT_WARNING );
Branch: 1 branch
BranchTrueFalseTotal
if $App::RecordStream::OptionalRequire::PRINT_WARNING 36 5 41
57 42 push @missing_modules, $module_name;
58 42 return 0;
59 }
60  
61 15 return 1;
62 }
63  
64 # CHECK runs after BEGIN blocks
65 21 ▶sub require_done {
Subroutine require_done: called CC=2 SCAR=7.2
Pod undocumented
66 21 ▶ if ( @missing_modules ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@missing_modules) 20 0 20
67 # NB: The exact phrasing of this exception is checked for in multiple
68 # places. Please grep accordingly if you plan to change it below.
69 21 die "Please install missing modules above to use this script\n";
70 }
71 }
72  
73 56 ▶sub use_module {
Subroutine use_module: called CC=2 SCAR=7.4
Pod undocumented
74 56 my $calling_package = shift;
75 56 my $module = shift;
76 56 my $args = join(' ', @_);
77 55 ▶ if ( $args ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args) 15 40 55
78 15 $args = " qw($args)";
79 }
80  
81 # Must use use here to invoke import
82 0 ▶ eval <<EVAL;
Subroutine BEGIN: called CC=1 SCAR=0.9
Subroutine BEGIN: called CC=1 SCAR=0.9
Subroutine BEGIN: called CC=1 SCAR=0.9
Subroutine BEGIN: called CC=1 SCAR=0.9
Subroutine BEGIN: called CC=1 SCAR=0.9
Subroutine BEGIN: called CC=1 SCAR=0.9
Subroutine BEGIN: called CC=1 SCAR=0.9
Subroutine BEGIN: called CC=1 SCAR=0.9
83 package $calling_package;
84 use $module $args;
85 EVAL
86 55 return not $@;
87 }
88  
89 54 1;
« blib/lib/App/RecordStream/Test/UniqConcatHelper.pm ↑ Summary blib/lib/App/RecordStream/Aggregator/Percentile.pm »