| 1 | package Sub::Private; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # Minimum Perl version: 5.14 (${^GLOBAL_PHASE}, needed to protect subs in | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # packages loaded after CHECK) | ||||||||||||||||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use Attribute::Handlers; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | # Variable::Magic, the XS backend of B::Hooks::EndOfScope, crashes the | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # process as soon as it is loaded on Windows Perl 5.42 (an access | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # violation, exit status 5), so use the pure-Perl backend there. A | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # backend the user chose through the environment is left alone, and | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # %ENV is only changed while the module loads. | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 15 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=4 SCAR=17.1
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | my $avoid_xs = ($^O eq 'MSWin32') && ($] >= 5.042) | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 15 | ▶ | && !defined($ENV{B_HOOKS_ENDOFSCOPE_IMPLEMENTATION}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
$^O eq 'MSWin32' && $] >= 5.042 && !defined($ENV{'B_HOOKS_ENDOFSCOPE_IMPLEMENTATION'})
$^O eq "MSWin32" and $] >= "5.042"
MC/DC0%0/3
$^O eq 'MSWin32' && $] >= 5.042 && !defined($ENV{'B_HOOKS_ENDOFSCOPE_IMPLEMENTATION'})
$^O eq "MSWin32"
$] >= "5.042"
!defined($ENV{'B_HOOKS_ENDOFSCOPE_IMPLEMENTATION'})
Truth table25%1/4
A and B && not(C)
A= $^O eq "MSWin32"B= $] >= "5.042"C= !defined($ENV{'B_HOOKS_ENDOFSCOPE_IMPLEMENTATION'})
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 15 | ▶ | local $ENV{B_HOOKS_ENDOFSCOPE_IMPLEMENTATION} = 'PP' if $avoid_xs; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 15 | require B::Hooks::EndOfScope; | |||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 15 | ▶ | use Carp qw(croak carp); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 15 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 15 | ▶ | use Params::Validate::Strict 0.33 qw(validate_strict); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 15 | ▶ | use Return::Set qw(set_return); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 15 | ▶ | use Sub::Identify qw(get_code_info); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | # namespace::clean is used as a class method only; import nothing. | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 15 | ▶ | use namespace::clean qw(); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | Sub::Private - Private subroutines and methods | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | Version 0.09 | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 15 | our $VERSION = '0.09'; | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 43 | # Mode-name constants. Using Readonly prevents accidental overwriting. | ||||||||||||||||||||||||||||||||||||||||||
| 44 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 15 | Readonly::Scalar my $MODE_NAMESPACE => 'namespace'; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 15 | Readonly::Scalar my $MODE_ENFORCE => 'enforce'; | |||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | # Config-key constants -- avoids bare magic strings in %config lookups. | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 15 | Readonly::Scalar my $KEY_MODE => 'mode'; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 15 | Readonly::Scalar my $KEY_HARNESS_BYPASS => 'harness_bypass'; | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | # The phase name the attribute handler passes to itself when it finishes, | ||||||||||||||||||||||||||||||||||||||||||
| 54 | # at the end of the enclosing scope, a sub compiled after CHECK. | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 15 | Readonly::Scalar my $PHASE_SCOPE_END => 'SCOPE_END'; | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | # Self-referential constant: the canonical name of this package. | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 15 | Readonly::Scalar my $SELF => __PACKAGE__; | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | # Validation schema for a single Perl sub name passed to import(). | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 15 | Readonly::Scalar my $SUB_NAME_SCHEMA => { | |||||||||||||||||||||||||||||||||||||||||
| 62 | name => { | ||||||||||||||||||||||||||||||||||||||||||
| 63 | type => 'string', | ||||||||||||||||||||||||||||||||||||||||||
| 64 | regex => qr/\A[_a-zA-Z]\w*\z/, | ||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | package Foo; | ||||||||||||||||||||||||||||||||||||||||||
| 71 | use Sub::Private; | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | sub foo { return 42 } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | sub bar :Private { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | return foo() + 1; | ||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | sub baz { | ||||||||||||||||||||||||||||||||||||||||||
| 80 | return bar() + 1; | ||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | Enforces strictly private access on subroutines. A subroutine decorated | ||||||||||||||||||||||||||||||||||||||||||
| 86 | with C<:Private> (or named in C<use Sub::Private qw(...)> when in enforce | ||||||||||||||||||||||||||||||||||||||||||
| 87 | mode) may only be called from within its defining package. Subclasses do | ||||||||||||||||||||||||||||||||||||||||||
| 88 | not inherit access: private means I<this package only>. | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | =head2 Two enforcement modes | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | =item C<namespace> mode (default, backward-compatible) | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | Removes the subroutine from the package symbol table using | ||||||||||||||||||||||||||||||||||||||||||
| 97 | L<namespace::clean>. Direct (non-method) function calls compiled before | ||||||||||||||||||||||||||||||||||||||||||
| 98 | cleanup still work because Perl optimises them to direct opcode references. | ||||||||||||||||||||||||||||||||||||||||||
| 99 | OO method dispatch (C<$self->name>) does not work for private subs in this | ||||||||||||||||||||||||||||||||||||||||||
| 100 | mode because method lookup uses the symbol table at runtime. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | This is the default mode and is backward-compatible with all existing code. | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | =item C<enforce> mode (OO-safe, opt-in) | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | Replaces the subroutine with a wrapper closure that checks C<caller> at | ||||||||||||||||||||||||||||||||||||||||||
| 107 | call time and either delegates (owner package) or croaks (anyone else). | ||||||||||||||||||||||||||||||||||||||||||
| 108 | Works correctly with OO dispatch (C<$self->_helper>). | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | Enable it for your package only, in the C<use> line: | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | package MyClass; | ||||||||||||||||||||||||||||||||||||||||||
| 113 | use Sub::Private { mode => 'enforce' }; | ||||||||||||||||||||||||||||||||||||||||||
| 114 | sub _helper :Private { ... } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | This is the recommended form for modules. Other packages keep the mode | ||||||||||||||||||||||||||||||||||||||||||
| 117 | they chose, or the global C<$Sub::Private::config{mode}>. | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | The older form sets the mode for every package in the program, and must | ||||||||||||||||||||||||||||||||||||||||||
| 120 | stay set until C<CHECK>, so it changes how other modules' C<:Private> | ||||||||||||||||||||||||||||||||||||||||||
| 121 | subs are handled. Use it only in a program, not in a module: | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | BEGIN { $Sub::Private::config{mode} = 'enforce' } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | package MyClass; | ||||||||||||||||||||||||||||||||||||||||||
| 125 | use Sub::Private; | ||||||||||||||||||||||||||||||||||||||||||
| 126 | sub _helper :Private { ... } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | =head2 Behaviour of wrapped subs (enforce mode) | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | =item * The wrapper hands over with C<goto &sub>, so it does not appear | ||||||||||||||||||||||||||||||||||||||||||
| 135 | on the call stack: C<caller> inside a private sub sees its real caller. | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | =item * Arguments, C<@_> aliasing and calling context (list, scalar or | ||||||||||||||||||||||||||||||||||||||||||
| 138 | void) are passed through unchanged. | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | =item * C<$_> is left untouched, whether the call is allowed or blocked. | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | =item * A blocked call croaks; it does not fall through to C<AUTOLOAD>. | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | =item * Works with L<Moo> and L<Moose> classes; use the declarative form | ||||||||||||||||||||||||||||||||||||||||||
| 145 | after C<use Moo> or C<use Moose>. | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | =head2 Loading at run time | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | Packages that use C<Sub::Private> may be loaded at run time (C<require>, | ||||||||||||||||||||||||||||||||||||||||||
| 152 | a plugin loader, a string C<eval>), and C<Sub::Private> itself may be | ||||||||||||||||||||||||||||||||||||||||||
| 153 | loaded for the first time that way. The private subs are then wrapped | ||||||||||||||||||||||||||||||||||||||||||
| 154 | (or removed, in C<namespace> mode) when the enclosing scope, normally the | ||||||||||||||||||||||||||||||||||||||||||
| 155 | rest of the file, has been compiled. | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | =head2 Bypass for testing | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | Either condition alone (OR logic) disables all access checks in enforce | ||||||||||||||||||||||||||||||||||||||||||
| 160 | mode: | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | =item * C<$Sub::Private::BYPASS> set to a true value. Use C<local> in | ||||||||||||||||||||||||||||||||||||||||||
| 165 | tests. | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | =item * C<$ENV{HARNESS_ACTIVE}> set (the convention used by | ||||||||||||||||||||||||||||||||||||||||||
| 168 | L<Test::Harness>/prove). | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | C<$Sub::Private::BYPASS> is the recommended form for new test code. | ||||||||||||||||||||||||||||||||||||||||||
| 173 | The C<HARNESS_ACTIVE> bypass can be disabled: | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | $Sub::Private::config{harness_bypass} = 0; | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | =head2 Configuration | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | use Sub::Private { mode => 'enforce' }; -- this package only (recommended) | ||||||||||||||||||||||||||||||||||||||||||
| 180 | $Sub::Private::config{mode} -- 'namespace' (default) or 'enforce', | ||||||||||||||||||||||||||||||||||||||||||
| 181 | for packages that chose no mode | ||||||||||||||||||||||||||||||||||||||||||
| 182 | $Sub::Private::config{harness_bypass} -- 1 (default); set to 0 to test enforcement | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | =head2 Error message format (enforce mode) | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | bar() is a private subroutine of Foo and cannot be called from Bar | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | # Public bypass flag. Use C<local $Sub::Private::BYPASS = 1> in test code. | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 15 | our $BYPASS = 0; | |||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | # Module configuration. //= preserves any value a caller set in a BEGIN | ||||||||||||||||||||||||||||||||||||||||||
| 194 | # block before this module body runs. | ||||||||||||||||||||||||||||||||||||||||||
| 195 | 15 | our %config; | |||||||||||||||||||||||||||||||||||||||||
| 196 | 15 | ▶ | $config{$KEY_MODE} //= $MODE_NAMESPACE; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$Sub::Private::config{$KEY_MODE} //= $MODE_NAMESPACE
MC/DC0%0/2
$Sub::Private::config{$KEY_MODE} //= $MODE_NAMESPACE
$Sub::Private::config{$KEY_MODE}
$MODE_NAMESPACE
Truth table66%2/3
A //= B
A= $Sub::Private::config{$KEY_MODE}B= $MODE_NAMESPACE
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 15 | ▶ | $config{$KEY_HARNESS_BYPASS} //= 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$Sub::Private::config{$KEY_HARNESS_BYPASS} //= 1
MC/DC0%0/1
$Sub::Private::config{$KEY_HARNESS_BYPASS} //= 1
$Sub::Private::config{$KEY_HARNESS_BYPASS}
Truth table50%1/2
A
A= $Sub::Private::config{$KEY_HARNESS_BYPASS}
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | # Pending (owner_pkg, sub_name) pairs to be wrapped at CHECK time. | ||||||||||||||||||||||||||||||||||||||||||
| 200 | # Populated by import(); consumed and cleared by the CHECK block. | ||||||||||||||||||||||||||||||||||||||||||
| 201 | 15 | my @_pending; | |||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | # Modes chosen per package with "use Sub::Private { mode => ... }", keyed by | ||||||||||||||||||||||||||||||||||||||||||
| 204 | # package name. A package not listed uses $config{mode}. | ||||||||||||||||||||||||||||||||||||||||||
| 205 | my %_package_mode; | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | # True once CHECK has run, so wrapping must happen without it. Set by our | ||||||||||||||||||||||||||||||||||||||||||
| 208 | # own CHECK block, or here when this module is first loaded after CHECK | ||||||||||||||||||||||||||||||||||||||||||
| 209 | # (run-time require, plugin loader, string eval). | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 15 | ▶ | my $_post_check = (${^GLOBAL_PHASE} ne 'START') ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | # ------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 213 | # ATTRIBUTE HANDLER | ||||||||||||||||||||||||||||||||||||||||||
| 214 | # ------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | # Install :Private in UNIVERSAL so every package can use it after a | ||||||||||||||||||||||||||||||||||||||||||
| 217 | # single "use Sub::Private", with no per-package setup required. | ||||||||||||||||||||||||||||||||||||||||||
| 218 | # ATTR(CODE,CHECK) fires at CHECK time, after all subs are compiled. | ||||||||||||||||||||||||||||||||||||||||||
| 219 | # The BEGIN phase covers code compiled after CHECK, when the CHECK-phase | ||||||||||||||||||||||||||||||||||||||||||
| 220 | # call never comes. | ||||||||||||||||||||||||||||||||||||||||||
| 221 | 138 | ▶ | sub UNIVERSAL::Private :ATTR(CODE,BEGIN,CHECK) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Private: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 138 | my ($package, $symbol, $referent, $attr, $data, $phase) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | 138 | ▶ | if ($phase eq 'BEGIN') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | # Before CHECK, the CHECK-phase call does the work. | ||||||||||||||||||||||||||||||||||||||||||
| 226 | 68 | ▶ | return unless $_post_check; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | # After CHECK the sub is still being compiled and may have no name | ||||||||||||||||||||||||||||||||||||||||||
| 229 | # yet, so finish the job when the enclosing scope is compiled. | ||||||||||||||||||||||||||||||||||||||||||
| 230 | # on_scope_end works here because the user's code is compiling. | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 5 | ▶ | B::Hooks::EndOfScope::on_scope_end(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 5 | my ($pkg, $name) = get_code_info($referent); | |||||||||||||||||||||||||||||||||||||||||
| 233 | 15 | ▶ | no strict 'refs'; ## no critic (ProhibitNoStrict) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 5 | UNIVERSAL::Private($pkg, \*{"${pkg}::$name"}, $referent, $attr, $data, $PHASE_SCOPE_END); | |||||||||||||||||||||||||||||||||||||||||
| 235 | 5 | }); | |||||||||||||||||||||||||||||||||||||||||
| 236 | 5 | return; | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | # Once its own CHECK block has run, Attribute::Handlers also makes the | ||||||||||||||||||||||||||||||||||||||||||
| 240 | # CHECK-phase call straight away for code compiled later, before the sub | ||||||||||||||||||||||||||||||||||||||||||
| 241 | # is named or installed ($symbol is then 'ANON'). Acting on it removed | ||||||||||||||||||||||||||||||||||||||||||
| 242 | # the half-compiled sub in namespace mode, and installed a wrapper as | ||||||||||||||||||||||||||||||||||||||||||
| 243 | # main::ANON in enforce mode. The BEGIN-phase call above deals with | ||||||||||||||||||||||||||||||||||||||||||
| 244 | # that code, so only the real CHECK phase is used here. | ||||||||||||||||||||||||||||||||||||||||||
| 245 | 70 | ▶ | return if $phase eq 'CHECK' && ${^GLOBAL_PHASE} ne 'CHECK'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$phase eq "CHECK" and ${^GLOBAL_PHASE} ne "CHECK"
MC/DC100%2/2
$phase eq "CHECK" and ${^GLOBAL_PHASE} ne "CHECK"
$phase eq "CHECK"
${^GLOBAL_PHASE} ne "CHECK"
Truth table100%3/3
A and B
A= $phase eq "CHECK"B= ${^GLOBAL_PHASE} ne "CHECK"
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 68 | my $sub_name = *{$symbol}{NAME}; | |||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | # The mode chosen by the sub's own package, or else the global one. | ||||||||||||||||||||||||||||||||||||||||||
| 250 | 68 | my $mode = _mode_for($package); | |||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | # Reject unrecognised mode values early rather than silently misbehaving. | ||||||||||||||||||||||||||||||||||||||||||
| 253 | 68 | _assert_known_mode($mode); | |||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | 68 | ▶ | if ($mode eq $MODE_ENFORCE) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | # Enforce mode: replace the stash entry with an access-checking wrapper. | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 15 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 60 | *{$symbol} = _wrap($package, $sub_name, $referent); | |||||||||||||||||||||||||||||||||||||||||
| 259 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 260 | # Namespace mode: remove the sub from the stash entirely. | ||||||||||||||||||||||||||||||||||||||||||
| 261 | # on_scope_end does NOT work from a CHECK-phase callback, so we call | ||||||||||||||||||||||||||||||||||||||||||
| 262 | # clean_subroutines() directly here. | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 8 | namespace::clean->clean_subroutines( get_code_info($referent) ); | |||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||
| 265 | 68 | return; | |||||||||||||||||||||||||||||||||||||||||
| 266 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | # ------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 269 | # PUBLIC INTERFACE | ||||||||||||||||||||||||||||||||||||||||||
| 270 | # ------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | =head1 PUBLIC INTERFACE | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | =head2 import | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | use Sub::Private; # attribute form -- no arguments | ||||||||||||||||||||||||||||||||||||||||||
| 277 | use Sub::Private qw(_a _b _c); # declarative form (enforce mode only) | ||||||||||||||||||||||||||||||||||||||||||
| 278 | use Sub::Private { mode => 'enforce' }; # mode for this package | ||||||||||||||||||||||||||||||||||||||||||
| 279 | use Sub::Private { mode => 'enforce' }, qw(_a _b); # both | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | =head3 Purpose | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | Called automatically by C<use Sub::Private>. | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | With B<no arguments>: makes the C<:Private> attribute globally available | ||||||||||||||||||||||||||||||||||||||||||
| 286 | via C<UNIVERSAL>. No other action is taken. | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | With B<a hash reference first>: sets options for the calling package | ||||||||||||||||||||||||||||||||||||||||||
| 289 | only. The one option is C<mode>, C<'namespace'> or C<'enforce'>. It | ||||||||||||||||||||||||||||||||||||||||||
| 290 | decides what happens to that package's C<:Private> subs and whether it | ||||||||||||||||||||||||||||||||||||||||||
| 291 | may use the declarative form, whatever C<$Sub::Private::config{mode}> | ||||||||||||||||||||||||||||||||||||||||||
| 292 | says. The global mode is not changed, so other packages are not | ||||||||||||||||||||||||||||||||||||||||||
| 293 | affected. The mode applies to subs in the package that made the C<use> | ||||||||||||||||||||||||||||||||||||||||||
| 294 | call, so put the C<use> line inside the package. | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | With B<one or more sub names>: registers those named subs in the calling | ||||||||||||||||||||||||||||||||||||||||||
| 297 | package for access-enforcement wrapping at C<CHECK> time. If C<CHECK> | ||||||||||||||||||||||||||||||||||||||||||
| 298 | has already fired (for example the package is loaded with C<require> at | ||||||||||||||||||||||||||||||||||||||||||
| 299 | run time), the subs are wrapped as soon as the enclosing scope (normally | ||||||||||||||||||||||||||||||||||||||||||
| 300 | the rest of the file) has been compiled, so the C<use> line can still | ||||||||||||||||||||||||||||||||||||||||||
| 301 | come before the subs it names. A direct C<< Sub::Private->import(...) >> | ||||||||||||||||||||||||||||||||||||||||||
| 302 | call at run time wraps immediately. Requires the package's mode (see | ||||||||||||||||||||||||||||||||||||||||||
| 303 | above), or else C<$Sub::Private::config{mode}>, to equal C<'enforce'>; | ||||||||||||||||||||||||||||||||||||||||||
| 304 | croaks otherwise. | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | =head3 Arguments | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | =item C<\%options> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | A hash reference, before any sub names. The only key allowed is | ||||||||||||||||||||||||||||||||||||||||||
| 313 | C<mode>, whose value must be C<'namespace'> or C<'enforce'>. | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | =item C<@subs> (optional) | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | Zero or more Perl sub names. Each must be a defined, non-reference scalar | ||||||||||||||||||||||||||||||||||||||||||
| 318 | matching C</\A[_a-zA-Z]\w*\z/>. C<undef>, references, empty strings, and | ||||||||||||||||||||||||||||||||||||||||||
| 319 | names starting with a digit or containing hyphens are all rejected. | ||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | =head3 Returns | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | The class name (C<'Sub::Private'>) as a plain string in all cases. | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | =head3 Side effects | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | =item * With C<\%options>: records the mode for the calling package. A | ||||||||||||||||||||||||||||||||||||||||||
| 332 | later call from the same package replaces it. | ||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | =item * Pre-CHECK: appends C<[$owner_pkg, $sub_name]> pairs to the | ||||||||||||||||||||||||||||||||||||||||||
| 335 | internal C<@_pending> list. | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | =item * Post-CHECK, during compilation: installs wrapper closures in the | ||||||||||||||||||||||||||||||||||||||||||
| 338 | calling package's stash when the enclosing scope has been compiled. | ||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | =item * Post-CHECK, at run time: installs wrapper closures directly in the | ||||||||||||||||||||||||||||||||||||||||||
| 341 | calling package's stash. | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | =head3 Example | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | BEGIN { $Sub::Private::config{mode} = 'enforce' } | ||||||||||||||||||||||||||||||||||||||||||
| 348 | package MyClass; | ||||||||||||||||||||||||||||||||||||||||||
| 349 | use Sub::Private qw(_helper _init); | ||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | sub new { bless {}, shift } | ||||||||||||||||||||||||||||||||||||||||||
| 352 | sub _helper { ... } # wrapped at CHECK time | ||||||||||||||||||||||||||||||||||||||||||
| 353 | sub _init { ... } # wrapped at CHECK time | ||||||||||||||||||||||||||||||||||||||||||
| 354 | sub run { my $s = shift; $s->_helper; $s->_init } | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | Enforce mode for one package, leaving every other package alone: | ||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | package MyClass; | ||||||||||||||||||||||||||||||||||||||||||
| 359 | use Sub::Private { mode => 'enforce' }; | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | sub new { bless {}, shift } | ||||||||||||||||||||||||||||||||||||||||||
| 362 | sub _helper :Private { ... } # wrapped at CHECK time | ||||||||||||||||||||||||||||||||||||||||||
| 363 | sub run { (shift)->_helper } | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | # No-argument form: always valid. | ||||||||||||||||||||||||||||||||||||||||||
| 370 | Sub::Private->import(); | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | # Options, first in the list (optional): | ||||||||||||||||||||||||||||||||||||||||||
| 373 | { | ||||||||||||||||||||||||||||||||||||||||||
| 374 | options => { | ||||||||||||||||||||||||||||||||||||||||||
| 375 | type => 'hashref', | ||||||||||||||||||||||||||||||||||||||||||
| 376 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 377 | schema => { | ||||||||||||||||||||||||||||||||||||||||||
| 378 | mode => { | ||||||||||||||||||||||||||||||||||||||||||
| 379 | type => 'string', | ||||||||||||||||||||||||||||||||||||||||||
| 380 | memberof => [ 'namespace', 'enforce' ], | ||||||||||||||||||||||||||||||||||||||||||
| 381 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 382 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 383 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | # Declarative form (enforce mode only): | ||||||||||||||||||||||||||||||||||||||||||
| 388 | { | ||||||||||||||||||||||||||||||||||||||||||
| 389 | subs => { | ||||||||||||||||||||||||||||||||||||||||||
| 390 | type => 'array', | ||||||||||||||||||||||||||||||||||||||||||
| 391 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 392 | element => { | ||||||||||||||||||||||||||||||||||||||||||
| 393 | type => 'string', | ||||||||||||||||||||||||||||||||||||||||||
| 394 | regex => qr/\A[_a-zA-Z]\w*\z/, | ||||||||||||||||||||||||||||||||||||||||||
| 395 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | { type => 'string' } # returns the class name 'Sub::Private' | ||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||
| 403 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||
| 405 | Message Meaning / Action | ||||||||||||||||||||||||||||||||||||||||||
| 406 | --------------------------------------------------- ----------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 407 | "Sub::Private->import: declarative form requires use Sub::Private qw(...) was called while | ||||||||||||||||||||||||||||||||||||||||||
| 408 | mode => 'enforce'" the package's mode is not 'enforce'. Use | ||||||||||||||||||||||||||||||||||||||||||
| 409 | use Sub::Private { mode => 'enforce' }, | ||||||||||||||||||||||||||||||||||||||||||
| 410 | qw(...) instead. | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | "Sub::Private->import: unknown option 'KEY'" The options hash has a key other than mode. | ||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||
| 414 | "Sub::Private: unknown mode 'MODE' -- use The mode option, or $config{mode}, is not | ||||||||||||||||||||||||||||||||||||||||||
| 415 | 'namespace' or 'enforce'" one of the two modes. | ||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||
| 417 | "Sub::Private->import: 'NAME' is not a valid The sub name failed the identifier regex. | ||||||||||||||||||||||||||||||||||||||||||
| 418 | Perl identifier" Check for typos, hyphens, leading digits, | ||||||||||||||||||||||||||||||||||||||||||
| 419 | undef, or reference values in the import list. | ||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||
| 421 | "Sub::Private: PKG::NAME is not defined" The named sub was not found in the stash at | ||||||||||||||||||||||||||||||||||||||||||
| 422 | wrap time. Define the sub before import() | ||||||||||||||||||||||||||||||||||||||||||
| 423 | runs, before CHECK fires, or (after CHECK) | ||||||||||||||||||||||||||||||||||||||||||
| 424 | before the end of the enclosing scope. | ||||||||||||||||||||||||||||||||||||||||||
| 425 | After CHECK, the location reported is the | ||||||||||||||||||||||||||||||||||||||||||
| 426 | "use Sub::Private" line. | ||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | 154 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=12 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 431 | 154 | my ($class, @subs) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 432 | 154 | my ($owner_pkg, $file, $line) = caller; | |||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | # A leading hashref holds options for the calling package only. | ||||||||||||||||||||||||||||||||||||||||||
| 435 | 154 | ▶ | _set_package_options($owner_pkg, shift @subs) if ref($subs[0]) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | # No sub names: the :Private attribute is always available via UNIVERSAL. | ||||||||||||||||||||||||||||||||||||||||||
| 438 | 150 | ▶ | return set_return($class, { type => 'string' }) unless @subs; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | # Declarative form is only meaningful in enforce mode. | ||||||||||||||||||||||||||||||||||||||||||
| 441 | 72 | ▶ | croak "$SELF->import: declarative form requires mode => '$MODE_ENFORCE'" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 442 | if _mode_for($owner_pkg) ne $MODE_ENFORCE; | ||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||
| 444 | # Validate every name before touching the stash (fail-fast, all-or-nothing). | ||||||||||||||||||||||||||||||||||||||||||
| 445 | 64 | for my $sub_name (@subs) { | |||||||||||||||||||||||||||||||||||||||||
| 446 | # Coerce invalid types (undef, ref) to empty string before schema check. | ||||||||||||||||||||||||||||||||||||||||||
| 447 | 77 | ▶ | my $check = (defined $sub_name && !ref $sub_name) ? $sub_name : q{}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $sub_name and not ref $sub_name
MC/DC100%2/2
defined $sub_name and not ref $sub_name
defined $sub_name
not ref $sub_name
Truth table100%3/3
A and not(B)
A= defined $sub_nameB= not ref $sub_name
| |||||||||||||||||||||||||||||||||||||||||||
| 448 | 77 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 449 | 77 | validate_strict( | |||||||||||||||||||||||||||||||||||||||||
| 450 | schema => $SUB_NAME_SCHEMA, | ||||||||||||||||||||||||||||||||||||||||||
| 451 | input => { name => $check }, | ||||||||||||||||||||||||||||||||||||||||||
| 452 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 453 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 454 | 77 | ▶ | croak "$SELF->import: '$check' is not a valid Perl identifier" if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 455 | } | ||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||
| 457 | # Schedule or immediately apply wrapping depending on compile phase. | ||||||||||||||||||||||||||||||||||||||||||
| 458 | 32 | ▶ | if ($_post_check) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 459 | 22 | ▶ | if (defined $^S) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 460 | # Called at run time: the subs already exist, wrap them now. | ||||||||||||||||||||||||||||||||||||||||||
| 461 | 19 | _process_one($owner_pkg, $_) for @subs; | |||||||||||||||||||||||||||||||||||||||||
| 462 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 463 | # "use" after CHECK (e.g. a run-time require): the subs below the | ||||||||||||||||||||||||||||||||||||||||||
| 464 | # "use" line are not compiled yet, so wrap them when the | ||||||||||||||||||||||||||||||||||||||||||
| 465 | # enclosing scope has been compiled. | ||||||||||||||||||||||||||||||||||||||||||
| 466 | 3 | ▶ | B::Hooks::EndOfScope::on_scope_end(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 467 | # Report errors at the "use" line, not inside the hook. | ||||||||||||||||||||||||||||||||||||||||||
| 468 | 3 | ▶ | eval { _process_one($owner_pkg, $_) for @subs; 1 } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 469 | 1 | (my $err = $@) =~ s/ at .+? line \d+\.?\n\z//s; | |||||||||||||||||||||||||||||||||||||||||
| 470 | 1 | die "$err at $file line $line.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 471 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 472 | 3 | }); | |||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||
| 474 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 475 | 10 | push @_pending, [ $owner_pkg, $_ ] for @subs; | |||||||||||||||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||
| 478 | 27 | return set_return($class, { type => 'string' }); | |||||||||||||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||
| 481 | # ------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 482 | # CHECK-TIME PROCESSING | ||||||||||||||||||||||||||||||||||||||||||
| 483 | # ------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||
| 485 | # Process all declarative wraps queued during import(). | ||||||||||||||||||||||||||||||||||||||||||
| 486 | # After this fires, $_post_check=1 so future import() calls wrap without it. | ||||||||||||||||||||||||||||||||||||||||||
| 487 | # When this module is loaded after CHECK the block cannot run, which is | ||||||||||||||||||||||||||||||||||||||||||
| 488 | # expected; silence "Too late to run CHECK block". | ||||||||||||||||||||||||||||||||||||||||||
| 489 | { | ||||||||||||||||||||||||||||||||||||||||||
| 490 | 15 | ▶ | no warnings 'void'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 491 | 14 | ▶ | CHECK { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
CHECK: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 492 | 14 | $_post_check = 1; | |||||||||||||||||||||||||||||||||||||||||
| 493 | 14 | _process_one(@$_) for @_pending; | |||||||||||||||||||||||||||||||||||||||||
| 494 | 14 | @_pending = (); | |||||||||||||||||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||
| 498 | # ------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 499 | # PRIVATE SUBROUTINES | ||||||||||||||||||||||||||||||||||||||||||
| 500 | # ------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | # _assert_known_mode | ||||||||||||||||||||||||||||||||||||||||||
| 503 | # Purpose : Validate that $config{mode} is a recognised string. | ||||||||||||||||||||||||||||||||||||||||||
| 504 | # Entry : $mode -- the value to validate | ||||||||||||||||||||||||||||||||||||||||||
| 505 | # Exit status : Returns normally for 'namespace' or 'enforce'; croaks | ||||||||||||||||||||||||||||||||||||||||||
| 506 | # with a descriptive message for any other value. | ||||||||||||||||||||||||||||||||||||||||||
| 507 | 15 | ▶ | sub _assert_known_mode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_assert_known_mode: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 508 | 90 | my ($mode) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 509 | 90 | ▶ | return if $mode eq $MODE_NAMESPACE || $mode eq $MODE_ENFORCE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$mode eq $MODE_NAMESPACE or $mode eq $MODE_ENFORCE
MC/DC100%2/2
$mode eq $MODE_NAMESPACE or $mode eq $MODE_ENFORCE
$mode eq $MODE_NAMESPACE
$mode eq $MODE_ENFORCE
Truth table100%3/3
A or B
A= $mode eq $MODE_NAMESPACEB= $mode eq $MODE_ENFORCE
| |||||||||||||||||||||||||||||||||||||||||||
| 510 | 11 | croak "$SELF: unknown mode '$mode'" | |||||||||||||||||||||||||||||||||||||||||
| 511 | . " -- use '$MODE_NAMESPACE' or '$MODE_ENFORCE'"; | ||||||||||||||||||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||||||||
| 514 | # _mode_for | ||||||||||||||||||||||||||||||||||||||||||
| 515 | # Purpose : Find the mode that applies to the subs of a package. | ||||||||||||||||||||||||||||||||||||||||||
| 516 | # Entry : $pkg -- a package name | ||||||||||||||||||||||||||||||||||||||||||
| 517 | # Exit status : Returns the mode the package chose with | ||||||||||||||||||||||||||||||||||||||||||
| 518 | # "use Sub::Private { mode => ... }", or else $config{mode}. | ||||||||||||||||||||||||||||||||||||||||||
| 519 | # Side effects : None. | ||||||||||||||||||||||||||||||||||||||||||
| 520 | 140 | ▶ | sub _mode_for { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mode_for: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 521 | 140 | my ($pkg) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||
| 523 | 140 | ▶ | return $_package_mode{$pkg} // $config{$KEY_MODE}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$_package_mode{$pkg} // $Sub::Private::config{$KEY_MODE}
MC/DC0%0/2
$_package_mode{$pkg} // $Sub::Private::config{$KEY_MODE}
$_package_mode{$pkg}
$Sub::Private::config{$KEY_MODE}
Truth table66%2/3
A // B
A= $_package_mode{$pkg}B= $Sub::Private::config{$KEY_MODE}
| |||||||||||||||||||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||
| 526 | # _set_package_options | ||||||||||||||||||||||||||||||||||||||||||
| 527 | # Purpose : Record the options a package gave in its "use" line. | ||||||||||||||||||||||||||||||||||||||||||
| 528 | # Entry : $pkg -- the package that called import() | ||||||||||||||||||||||||||||||||||||||||||
| 529 | # $options -- hashref; the only key allowed is "mode" | ||||||||||||||||||||||||||||||||||||||||||
| 530 | # Exit status : Returns normally; croaks on an unknown key or mode. | ||||||||||||||||||||||||||||||||||||||||||
| 531 | # Side effects : Sets the package's entry in %_package_mode. The global | ||||||||||||||||||||||||||||||||||||||||||
| 532 | # $config{mode} is not touched, so other packages are not | ||||||||||||||||||||||||||||||||||||||||||
| 533 | # affected. | ||||||||||||||||||||||||||||||||||||||||||
| 534 | 13 | ▶ | sub _set_package_options { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_package_options: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 535 | 13 | my ($pkg, $options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||
| 537 | # Check every key before changing anything (all-or-nothing). | ||||||||||||||||||||||||||||||||||||||||||
| 538 | 13 | for my $key (sort keys %{$options}) { | |||||||||||||||||||||||||||||||||||||||||
| 539 | 11 | ▶ | croak "$SELF->import: unknown option '$key'" if $key ne $KEY_MODE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||||||||||||||||
| 541 | 11 | ▶ | if (exists $options->{$KEY_MODE}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 542 | 9 | ▶ | my $mode = $options->{$KEY_MODE} // q{}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{$KEY_MODE} // ""
MC/DC100%1/1
$options->{$KEY_MODE} // ""
$options->{$KEY_MODE}
Truth table100%2/2
A
A= $options->{$KEY_MODE}
| |||||||||||||||||||||||||||||||||||||||||||
| 543 | 9 | _assert_known_mode($mode); | |||||||||||||||||||||||||||||||||||||||||
| 544 | 7 | $_package_mode{$pkg} = $mode; | |||||||||||||||||||||||||||||||||||||||||
| 545 | } | ||||||||||||||||||||||||||||||||||||||||||
| 546 | 9 | return; | |||||||||||||||||||||||||||||||||||||||||
| 547 | } | ||||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||
| 549 | # _process_one | ||||||||||||||||||||||||||||||||||||||||||
| 550 | # Purpose : Look up a named sub in a package stash and install a wrapper. | ||||||||||||||||||||||||||||||||||||||||||
| 551 | # Entry : $owner_pkg -- the package that declared the sub | ||||||||||||||||||||||||||||||||||||||||||
| 552 | # $sub_name -- the unqualified sub name to wrap | ||||||||||||||||||||||||||||||||||||||||||
| 553 | # Exit status : Returns normally; the stash entry is replaced with a wrapper. | ||||||||||||||||||||||||||||||||||||||||||
| 554 | # Side effects : Modifies the package stash for $owner_pkg. | ||||||||||||||||||||||||||||||||||||||||||
| 555 | # Notes : Guarded by _assert_private_caller -- external calls croak. | ||||||||||||||||||||||||||||||||||||||||||
| 556 | 49 | ▶ | sub _process_one { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_one: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 557 | 49 | my ($owner_pkg, $sub_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||||||||||
| 559 | # Guard: only Sub::Private itself may call this. | ||||||||||||||||||||||||||||||||||||||||||
| 560 | _assert_private_caller('_process_one') | ||||||||||||||||||||||||||||||||||||||||||
| 561 | 49 | ▶ | unless $BYPASS || ($config{$KEY_HARNESS_BYPASS} && $ENV{HARNESS_ACTIVE}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$Sub::Private::BYPASS or $Sub::Private::config{$KEY_HARNESS_BYPASS} and $ENV{"HARNESS_ACTIVE"}
$Sub::Private::config{$KEY_HARNESS_BYPASS} and $ENV{"HARNESS_ACTIVE"}
MC/DC100%3/3
$Sub::Private::BYPASS or $Sub::Private::config{$KEY_HARNESS_BYPASS} and $ENV{"HARNESS_ACTIVE"}
$Sub::Private::BYPASS
$Sub::Private::config{$KEY_HARNESS_BYPASS}
$ENV{"HARNESS_ACTIVE"}
Truth table100%4/4
A or B and C
A= $Sub::Private::BYPASSB= $Sub::Private::config{$KEY_HARNESS_BYPASS}C= $ENV{"HARNESS_ACTIVE"}
| |||||||||||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||||||||||
| 563 | 15 | ▶ | no strict 'refs'; ## no critic (ProhibitNoStrict) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||||
| 565 | # Ensure the target sub exists in the stash before wrapping. | ||||||||||||||||||||||||||||||||||||||||||
| 566 | croak "$SELF: ${owner_pkg}::${sub_name} is not defined" | ||||||||||||||||||||||||||||||||||||||||||
| 567 | 46 | ▶ | unless defined &{"${owner_pkg}::${sub_name}"}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||||||||||||||||
| 569 | 38 | my $code = \&{"${owner_pkg}::${sub_name}"}; | |||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||
| 571 | # Replace the stash entry with the enforcement wrapper. | ||||||||||||||||||||||||||||||||||||||||||
| 572 | 15 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 573 | 38 | *{"${owner_pkg}::${sub_name}"} = _wrap($owner_pkg, $sub_name, $code); | |||||||||||||||||||||||||||||||||||||||||
| 574 | 38 | return; | |||||||||||||||||||||||||||||||||||||||||
| 575 | } | ||||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||
| 577 | # _wrap | ||||||||||||||||||||||||||||||||||||||||||
| 578 | # Purpose : Build an enforcement wrapper closure around a coderef. | ||||||||||||||||||||||||||||||||||||||||||
| 579 | # Entry : $owner_pkg -- the package that owns the private sub | ||||||||||||||||||||||||||||||||||||||||||
| 580 | # $sub_name -- the unqualified sub name (for error messages) | ||||||||||||||||||||||||||||||||||||||||||
| 581 | # $code -- the original coderef to delegate to | ||||||||||||||||||||||||||||||||||||||||||
| 582 | # Exit status : Returns a new coderef that enforces the private-access rule. | ||||||||||||||||||||||||||||||||||||||||||
| 583 | # Side effects : none (variables captured by closure) | ||||||||||||||||||||||||||||||||||||||||||
| 584 | # Notes : goto &$code is used rather than $code->(@_) so that caller() | ||||||||||||||||||||||||||||||||||||||||||
| 585 | # inside the private sub sees the real caller, not Sub::Private. | ||||||||||||||||||||||||||||||||||||||||||
| 586 | # This is load-bearing: removing it breaks tests that inspect | ||||||||||||||||||||||||||||||||||||||||||
| 587 | # caller() inside a private sub. Guarded by _assert_private_caller. | ||||||||||||||||||||||||||||||||||||||||||
| 588 | 110 | ▶ | sub _wrap { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_wrap: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 589 | 110 | my ($owner_pkg, $sub_name, $code) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||||||
| 591 | # Guard: only Sub::Private itself may call this. | ||||||||||||||||||||||||||||||||||||||||||
| 592 | _assert_private_caller('_wrap') | ||||||||||||||||||||||||||||||||||||||||||
| 593 | 110 | ▶ | unless $BYPASS || ($config{$KEY_HARNESS_BYPASS} && $ENV{HARNESS_ACTIVE}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$Sub::Private::BYPASS or $Sub::Private::config{$KEY_HARNESS_BYPASS} and $ENV{"HARNESS_ACTIVE"}
$Sub::Private::config{$KEY_HARNESS_BYPASS} and $ENV{"HARNESS_ACTIVE"}
MC/DC100%3/3
$Sub::Private::BYPASS or $Sub::Private::config{$KEY_HARNESS_BYPASS} and $ENV{"HARNESS_ACTIVE"}
$Sub::Private::BYPASS
$Sub::Private::config{$KEY_HARNESS_BYPASS}
$ENV{"HARNESS_ACTIVE"}
Truth table100%4/4
A or B and C
A= $Sub::Private::BYPASSB= $Sub::Private::config{$KEY_HARNESS_BYPASS}C= $ENV{"HARNESS_ACTIVE"}
| |||||||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||||||
| 595 | # Capture the three args in the closure; the wrapper has no mutable state. | ||||||||||||||||||||||||||||||||||||||||||
| 596 | 198 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 597 | 198 | Sub::Private::_check_access($owner_pkg, $sub_name); | |||||||||||||||||||||||||||||||||||||||||
| 598 | 109 | goto &$code; ## no critic (ControlStructures::ProhibitGoto) | |||||||||||||||||||||||||||||||||||||||||
| 599 | 104 | }; | |||||||||||||||||||||||||||||||||||||||||
| 600 | } | ||||||||||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||||||||||
| 602 | # _check_access | ||||||||||||||||||||||||||||||||||||||||||
| 603 | # Purpose : Enforce the private-access invariant at call time. | ||||||||||||||||||||||||||||||||||||||||||
| 604 | # Entry : $owner_pkg -- the package that owns the private sub | ||||||||||||||||||||||||||||||||||||||||||
| 605 | # $sub_name -- unqualified sub name (for error messages) | ||||||||||||||||||||||||||||||||||||||||||
| 606 | # Exit status : Returns normally if the immediate non-Sub::Private caller is | ||||||||||||||||||||||||||||||||||||||||||
| 607 | # the owner package. Croaks if any other package is found first. | ||||||||||||||||||||||||||||||||||||||||||
| 608 | # Notes : Unlike Sub::Protected there is NO ->isa check. Private means | ||||||||||||||||||||||||||||||||||||||||||
| 609 | # the owner package ONLY; subclasses are blocked. | ||||||||||||||||||||||||||||||||||||||||||
| 610 | # The stack walk skips Sub::Private frames so the wrapper is | ||||||||||||||||||||||||||||||||||||||||||
| 611 | # transparent to the check. | ||||||||||||||||||||||||||||||||||||||||||
| 612 | 209 | ▶ | sub _check_access { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_access: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 613 | 209 | my ($owner_pkg, $sub_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||||
| 615 | # Fast bypass paths: either condition alone disables all checks (OR logic). | ||||||||||||||||||||||||||||||||||||||||||
| 616 | 209 | ▶ | return if $BYPASS; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 617 | 197 | ▶ | return if $config{$KEY_HARNESS_BYPASS} && $ENV{HARNESS_ACTIVE}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$Sub::Private::config{$KEY_HARNESS_BYPASS} and $ENV{"HARNESS_ACTIVE"}
MC/DC100%2/2
$Sub::Private::config{$KEY_HARNESS_BYPASS} and $ENV{"HARNESS_ACTIVE"}
$Sub::Private::config{$KEY_HARNESS_BYPASS}
$ENV{"HARNESS_ACTIVE"}
Truth table100%3/3
A and B
A= $Sub::Private::config{$KEY_HARNESS_BYPASS}B= $ENV{"HARNESS_ACTIVE"}
| |||||||||||||||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||||||||||||
| 619 | # Walk the call stack, skipping Sub::Private wrapper frames. | ||||||||||||||||||||||||||||||||||||||||||
| 620 | 192 | my $frame = 0; | |||||||||||||||||||||||||||||||||||||||||
| 621 | 192 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 622 | 377 | my $pkg = (caller($frame))[0]; | |||||||||||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||||||||||||
| 624 | # Reached the bottom of the stack with no valid caller found. | ||||||||||||||||||||||||||||||||||||||||||
| 625 | 377 | ▶ | if (!defined $pkg) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 626 | 1 | croak "${sub_name}() is a private subroutine of ${owner_pkg}" | |||||||||||||||||||||||||||||||||||||||||
| 627 | . ' and cannot be called from outside any package'; | ||||||||||||||||||||||||||||||||||||||||||
| 628 | } | ||||||||||||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||||||||||||
| 630 | # Skip any Sub::Private frames (e.g., the wrapper closure itself). | ||||||||||||||||||||||||||||||||||||||||||
| 631 | 376 | ▶ | $frame++, next if $pkg eq $SELF; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||||||||||||
| 633 | # The first non-Sub::Private caller must be the owner; everyone else | ||||||||||||||||||||||||||||||||||||||||||
| 634 | # is blocked -- no isa allowance, unlike Sub::Protected. | ||||||||||||||||||||||||||||||||||||||||||
| 635 | 191 | ▶ | return if $pkg eq $owner_pkg; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 636 | 95 | croak "${sub_name}() is a private subroutine of ${owner_pkg}" | |||||||||||||||||||||||||||||||||||||||||
| 637 | . " and cannot be called from ${pkg}"; | ||||||||||||||||||||||||||||||||||||||||||
| 638 | } | ||||||||||||||||||||||||||||||||||||||||||
| 639 | } | ||||||||||||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||||||||||||
| 641 | # _assert_private_caller | ||||||||||||||||||||||||||||||||||||||||||
| 642 | # Purpose : Croak if a guarded private method was called from outside | ||||||||||||||||||||||||||||||||||||||||||
| 643 | # Sub::Private. | ||||||||||||||||||||||||||||||||||||||||||
| 644 | # Entry : $method_name -- the guarded method name (for error messages) | ||||||||||||||||||||||||||||||||||||||||||
| 645 | # Exit status : Returns normally if caller(1) is Sub::Private; croaks | ||||||||||||||||||||||||||||||||||||||||||
| 646 | # otherwise with a descriptive message. | ||||||||||||||||||||||||||||||||||||||||||
| 647 | # Notes : caller(1) is the package that called the guarded method, | ||||||||||||||||||||||||||||||||||||||||||
| 648 | # which in turn called this function. | ||||||||||||||||||||||||||||||||||||||||||
| 649 | 31 | ▶ | sub _assert_private_caller { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_assert_private_caller: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 650 | 31 | my ($method_name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||||||||||
| 652 | # caller(1): the package one frame above the guarded method. | ||||||||||||||||||||||||||||||||||||||||||
| 653 | 31 | ▶ | my $caller = (caller(1))[0] // q{}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
(&CORE::GLOBAL::caller(1))[0] // ""
MC/DC100%1/1
(&CORE::GLOBAL::caller(1))[0] // ""
(&CORE::GLOBAL::caller(1))[0]
Truth table100%2/2
A
A= (&CORE::GLOBAL::caller(1))[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||||||||||||
| 655 | # Only calls originating within Sub::Private itself are permitted. | ||||||||||||||||||||||||||||||||||||||||||
| 656 | 31 | ▶ | return if $caller eq $SELF; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||||||||||||||||
| 658 | 13 | croak "${method_name}() is a private method of $SELF" | |||||||||||||||||||||||||||||||||||||||||
| 659 | . " and cannot be called from ${caller}"; | ||||||||||||||||||||||||||||||||||||||||||
| 660 | } | ||||||||||||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||||||||||||
| 662 | 15 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||||||||||||
| 664 | __END__ | ||||||||||||||||||||||||||||||||||||||||||