« blib/lib/App/RecordStream/Deaggregator/Unhash.pm
↑ Summary
blib/lib/App/RecordStream/Test/DistinctCountHelper.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 | 53 | our $VERSION = "4.0.23"; | |||||||||
| 26 | |||||||||||
| 27 | 66 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 56 | ▶ | 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 | 53 | our $PRINT_WARNING = 1; | |||||||||
| 32 | |||||||||||
| 33 | 53 | my @missing_modules; | |||||||||
| 34 | |||||||||||
| 35 | sub import { | ||||||||||
| 36 | 31 | ▶ | my $class = shift; | ||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 30 | my $calling_package = (caller())[0]; | |||||||||
| 38 | 30 | 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 | sub optional_use { | ||||||||||
| 43 | 4 | ▶ | my $calling_package = (caller())[0]; | ||||||||
|
Subroutine
optional_use: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 44 | 3 | return optional_use_with_caller($calling_package, @_); | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | sub optional_use_with_caller { | ||||||||||
| 48 | 33 | ▶ | my $calling_package = shift; | ||||||||
|
Subroutine
optional_use_with_caller: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 49 | |||||||||||
| 50 | 33 | my $loaded; | |||||||||
| 51 | |||||||||||
| 52 | 33 | $loaded = use_module($calling_package, @_); | |||||||||
| 53 | 33 | my $module_name = $_[0]; | |||||||||
| 54 | |||||||||||
| 55 | 33 | ▶ | unless ( $loaded ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 23 | ▶ | warn "$0 requires missing module $module_name\n" if ( $PRINT_WARNING ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 23 | push @missing_modules, $module_name; | |||||||||
| 58 | 23 | return 0; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 10 | return 1; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | # CHECK runs after BEGIN blocks | ||||||||||
| 65 | sub require_done { | ||||||||||
| 66 | 12 | ▶ | if ( @missing_modules ) { | ||||||||
|
Subroutine
require_done: called CC=2 SCAR=7.2
Pod
undocumented
Branch: 1 branch
| |||||||||||
| 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 | 12 | die "Please install missing modules above to use this script\n"; | |||||||||
| 70 | } | ||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | sub use_module { | ||||||||||
| 74 | 33 | ▶ | my $calling_package = shift; | ||||||||
|
Subroutine
use_module: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 75 | 33 | my $module = shift; | |||||||||
| 76 | 33 | my $args = join(' ', @_); | |||||||||
| 77 | 33 | ▶ | if ( $args ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 8 | $args = " qw($args)"; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | # Must use use here to invoke import | ||||||||||
| 82 | 15 | ▶ | eval <<EVAL; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 83 | package $calling_package; | ||||||||||
| 84 | use $module $args; | ||||||||||
| 85 | EVAL | ||||||||||
| 86 | 33 | return not $@; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 53 | 1; | |||||||||