| 1 | package DBIO::Test::Util::OverrideRequire; | ||||||||||
| 2 | # ABSTRACT: Override CORE::GLOBAL::require for testing | ||||||||||
| 3 | |||||||||||
| 4 | 2 | our $VERSION = '0.900007'; | |||||||||
| 5 | |||||||||||
| 6 | # no use/require of any kind - work bare | ||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use constant TRACE => 0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | # 0 - no trace | ||||||||||
| 10 | # 1 - just requires and return values | ||||||||||
| 11 | # 2 - neat stacktrace (assumes that the supplied $override_cref does *not* (ab)use goto) | ||||||||||
| 12 | # 3 - full stacktrace | ||||||||||
| 13 | |||||||||||
| 14 | # Takes a single coderef and replaces CORE::GLOBAL::require with it. | ||||||||||
| 15 | # | ||||||||||
| 16 | # On subsequent require() calls, the coderef will be invoked with | ||||||||||
| 17 | # two arguments - ($next_require, $module_name_copy) | ||||||||||
| 18 | # | ||||||||||
| 19 | # $next_require is a coderef closing over the module name. It needs | ||||||||||
| 20 | # to be invoked at some point without arguments for the actual | ||||||||||
| 21 | # require to take place (this way your coderef in essence becomes an | ||||||||||
| 22 | # around modifier) | ||||||||||
| 23 | # | ||||||||||
| 24 | # $module_name_copy is a string-copy of what $next_require is closing | ||||||||||
| 25 | # over. The reason for the copy is that you may trigger a side effect | ||||||||||
| 26 | # on magical values, and subsequently abort the require (e.g. | ||||||||||
| 27 | # require v.5.8.8 magic) | ||||||||||
| 28 | # | ||||||||||
| 29 | # All of this almost verbatim copied from Lexical::SealRequireHints | ||||||||||
| 30 | # Zefram++ | ||||||||||
| 31 | 0 | ▶ | sub override_global_require (&) { | ||||||||
|
Subroutine
override_global_require: not called CC=2 SCAR=17.3
Pod
undocumented
| |||||||||||
| 32 | 0 | my $override_cref = shift; | |||||||||
| 33 | |||||||||||
| 34 | our $next_require = defined(&CORE::GLOBAL::require) | ||||||||||
| 35 | ? \&CORE::GLOBAL::require | ||||||||||
| 36 | 0 | ▶ | : sub { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 37 | |||||||||||
| 38 | 0 | my ($arg) = @_; | |||||||||
| 39 | |||||||||||
| 40 | # The shenanigans with $CORE::GLOBAL::{require} | ||||||||||
| 41 | # are required because if there's a | ||||||||||
| 42 | # &CORE::GLOBAL::require when the eval is | ||||||||||
| 43 | # executed then the CORE::require in there is | ||||||||||
| 44 | # interpreted as plain require on some Perl | ||||||||||
| 45 | # versions, leading to recursion. | ||||||||||
| 46 | 0 | my $grequire = delete $CORE::GLOBAL::{require}; | |||||||||
| 47 | |||||||||||
| 48 | 0 | my $res = eval sprintf ' | |||||||||
| 49 | local $SIG{__DIE__}; | ||||||||||
| 50 | $CORE::GLOBAL::{require} = $grequire; | ||||||||||
| 51 | package %s; | ||||||||||
| 52 | CORE::require($arg); | ||||||||||
| 53 | ', scalar caller(0); # the caller already had its package replaced | ||||||||||
| 54 | |||||||||||
| 55 | 0 | ▶ | my $err = $@ if $@ ne ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | |||||||||||
| 57 | 0 | if( TRACE ) { | |||||||||
| 58 | if (TRACE == 1) { | ||||||||||
| 59 | printf STDERR "Require of '%s' (returned: '%s')\n", | ||||||||||
| 60 | (my $m_copy = $arg), | ||||||||||
| 61 | (my $r_copy = $res), | ||||||||||
| 62 | ; | ||||||||||
| 63 | } | ||||||||||
| 64 | else { | ||||||||||
| 65 | my ($fr_num, @fr, @tr, $excise); | ||||||||||
| 66 | while (@fr = caller($fr_num++)) { | ||||||||||
| 67 | |||||||||||
| 68 | # Package::Stash::XS is a cock and gets mightily confused if one | ||||||||||
| 69 | # uses a regex in the require hook. Even though it happens only | ||||||||||
| 70 | # on < 5.8.7 it's still rather embarassing (also wtf does P::S::XS | ||||||||||
| 71 | # even need to regex its own module name?!). So we do not use re :) | ||||||||||
| 72 | if (TRACE == 3 or (index($fr[1], '(eval ') != 0 and index($fr[1], __FILE__) != 0) ) { | ||||||||||
| 73 | push @tr, [@fr] | ||||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | # the caller before this would be the override site - kill it away | ||||||||||
| 77 | # if the cref writer uses goto - well tough, tracer won't work | ||||||||||
| 78 | if ($fr[3] eq 'DBIO::Test::Util::OverrideRequire::__ANON__') { | ||||||||||
| 79 | $excise ||= $tr[-2] | ||||||||||
| 80 | if TRACE == 2; | ||||||||||
| 81 | } | ||||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | my @stack = | ||||||||||
| 85 | map { "$_->[1], line $_->[2]" } | ||||||||||
| 86 | grep { ! $excise or $_->[1] ne $excise->[1] or $_->[2] ne $excise->[2] } | ||||||||||
| 87 | @tr | ||||||||||
| 88 | ; | ||||||||||
| 89 | |||||||||||
| 90 | printf STDERR "Require of '%s' (returned: '%s')\n%s\n\n", | ||||||||||
| 91 | (my $m_copy = $arg), | ||||||||||
| 92 | (my $r_copy = $res||''), | ||||||||||
| 93 | join "\n", (map { " $_" } @stack) | ||||||||||
| 94 | ; | ||||||||||
| 95 | } | ||||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | 0 | ▶ | die $err if defined $err; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | |||||||||||
| 100 | 0 | return $res; | |||||||||
| 101 | } | ||||||||||
| 102 | 0 | ▶ | ; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | |||||||||||
| 104 | # Need to suppress the redefinition warning, without | ||||||||||
| 105 | # invoking warnings.pm. | ||||||||||
| 106 | 2 | ▶ | BEGIN { ${^WARNING_BITS} = ""; } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 107 | |||||||||||
| 108 | 0 | ▶ | *CORE::GLOBAL::require = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 109 | 0 | ▶ | die "wrong number of arguments to require\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | unless @_ == 1; | ||||||||||
| 111 | |||||||||||
| 112 | # the copy is to prevent accidental overload firing (e.g. require v5.8.8) | ||||||||||
| 113 | 0 | my ($arg_copy) = our ($arg) = @_; | |||||||||
| 114 | |||||||||||
| 115 | 0 | ▶ | return $override_cref->(sub { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 116 | 0 | ▶ | die "The require delegate takes no arguments\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | if @_; | ||||||||||
| 118 | |||||||||||
| 119 | 0 | my $res = eval sprintf ' | |||||||||
| 120 | local $SIG{__DIE__}; | ||||||||||
| 121 | package %s; | ||||||||||
| 122 | $next_require->($arg); | ||||||||||
| 123 | ', scalar caller(2); # 2 for the indirection of the $override_cref around | ||||||||||
| 124 | |||||||||||
| 125 | 0 | ▶ | die $@ if $@ ne ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | |||||||||||
| 127 | 0 | return $res; | |||||||||
| 128 | |||||||||||
| 129 | 0 | }, $arg_copy); | |||||||||
| 130 | } | ||||||||||
| 131 | 0 | } | |||||||||
| 132 | |||||||||||
| 133 | 2 | 1; | |||||||||
| 134 | |||||||||||
| 135 | __END__ | ||||||||||