| 1 | package BEGIN::Lift; | ||||||||||
| 2 | # ABSTRACT: Lift subroutine calls into the BEGIN phase | ||||||||||
| 3 | |||||||||||
| 4 | 9 | ▶ | use strict; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 9 | ▶ | use warnings; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 9 | our $VERSION; | |||||||||
| 8 | 9 | our $AUTHORITY; | |||||||||
| 9 | |||||||||||
| 10 | 9 | ▶ | use Sub::Name (); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 9 | ▶ | use Devel::Hook (); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 9 | ▶ | use Devel::CallParser; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 9 | ▶ | use XSLoader; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 9 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 9 | $VERSION = '0.07'; | |||||||||
| 17 | 9 | $AUTHORITY = 'cpan:STEVAN'; | |||||||||
| 18 | 9 | XSLoader::load( __PACKAGE__, $VERSION ); | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | 8 | ▶ | sub install { | ||||||||
|
Subroutine
install: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 22 | 8 | my ($pkg, $method, $handler) = @_; | |||||||||
| 23 | |||||||||||
| 24 | # need to force a new CV each time here | ||||||||||
| 25 | # not entirely sure why, but I assume | ||||||||||
| 26 | # that perl was trying to optimize things | ||||||||||
| 27 | # which is not what I actually want. | ||||||||||
| 28 | 8 | my $cv = eval 'sub {}'; | |||||||||
| 29 | |||||||||||
| 30 | # now we need to install the stub | ||||||||||
| 31 | # we just created, but first we need to | ||||||||||
| 32 | # verify that we are the only ones using | ||||||||||
| 33 | # the typeglob we are installing into. | ||||||||||
| 34 | # This makes it easier/safer to delete | ||||||||||
| 35 | # the stub before runtime. | ||||||||||
| 36 | { | ||||||||||
| 37 | 9 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | die "Cannot install the lifted keyword ($method) into package ($pkg) when that typeglob (\*${pkg}::${method}) already exists" | ||||||||||
| 39 | 8 | ▶ | if exists ${"${pkg}::"}{$method}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 7 | *{"${pkg}::${method}"} = $cv; | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | # give the handler a name so that | ||||||||||
| 44 | # it shows up sensibly in stack | ||||||||||
| 45 | # traces and the like ... | ||||||||||
| 46 | 7 | Sub::Name::subname( "${pkg}::${method}", $handler ); | |||||||||
| 47 | |||||||||||
| 48 | # install the keyword handler ... | ||||||||||
| 49 | BEGIN::Lift::Util::install_keyword_handler( | ||||||||||
| 50 | 10 | ▶ | $cv, sub { $handler->( $_[0] ? $_[0]->() : () ) } | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 51 | 7 | ); | |||||||||
| 52 | |||||||||||
| 53 | # clean things up ... | ||||||||||
| 54 | 7 | ▶ | Devel::Hook->unshift_UNITCHECK_hook(sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 55 | 9 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 56 | # NOTE: | ||||||||||
| 57 | # this is safe only because we | ||||||||||
| 58 | # confirmed above that there was | ||||||||||
| 59 | # no other use of this typeglob | ||||||||||
| 60 | # and so it is ok to delete | ||||||||||
| 61 | 7 | delete ${"${pkg}::"}{$method} | |||||||||
| 62 | 7 | }); | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 9 | 1; | |||||||||
| 66 | |||||||||||
| 67 | __END__ | ||||||||||