| 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 B::CompilerPhase::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 | BEGIN { | ||||||||||
| 16 | 9 | ▶ | $VERSION = '0.04'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 9 | $AUTHORITY = 'cpan:STEVAN'; | |||||||||
| 18 | 9 | XSLoader::load( __PACKAGE__, $VERSION ); | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | sub install { | ||||||||||
| 22 | 8 | ▶ | my ($pkg, $method, $handler) = @_; | ||||||||
|
Subroutine
install: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 23 | |||||||||||
| 24 | # It does not make any sense to create | ||||||||||
| 25 | # something that is meant to run in the | ||||||||||
| 26 | # BEGIN phase *after* that phase is done | ||||||||||
| 27 | # so catch this and error ... | ||||||||||
| 28 | 8 | ▶ | die 'Lifted keywords must be created during BEGIN time, not (' . ${^GLOBAL_PHASE}. ')' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | unless ${^GLOBAL_PHASE} eq 'START'; | ||||||||||
| 30 | |||||||||||
| 31 | # need to force a new CV each time here | ||||||||||
| 32 | # not entirely sure why, but I assume | ||||||||||
| 33 | # that perl was trying to optimize things | ||||||||||
| 34 | # which is not what I actually want. | ||||||||||
| 35 | 7 | my $cv = eval 'sub {}'; | |||||||||
| 36 | |||||||||||
| 37 | # now we need to install the stub | ||||||||||
| 38 | # we just created, but first we need to | ||||||||||
| 39 | # verify that we are the only ones using | ||||||||||
| 40 | # the typeglob we are installing into. | ||||||||||
| 41 | # This makes it easier/safer to delete | ||||||||||
| 42 | # the stub before runtime. | ||||||||||
| 43 | { | ||||||||||
| 44 | 9 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | die "Cannot install the lifted keyword ($method) into package ($pkg) when that typeglob (\*${pkg}::${method}) already exists" | ||||||||||
| 46 | 7 | ▶ | if exists ${"${pkg}::"}{$method}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 6 | *{"${pkg}::${method}"} = $cv; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | # give the handler a name so that | ||||||||||
| 51 | # it shows up sensibly in stack | ||||||||||
| 52 | # traces and the like ... | ||||||||||
| 53 | 6 | Sub::Name::subname( "${pkg}::${method}", $handler ); | |||||||||
| 54 | |||||||||||
| 55 | # install the keyword handler ... | ||||||||||
| 56 | BEGIN::Lift::Util::install_keyword_handler( | ||||||||||
| 57 | 9 | ▶ | $cv, sub { $handler->( $_[0] ? $_[0]->() : () ) } | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 58 | 6 | ); | |||||||||
| 59 | |||||||||||
| 60 | # clean things up ... | ||||||||||
| 61 | B::CompilerPhase::Hook::enqueue_UNITCHECK { | ||||||||||
| 62 | 9 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 63 | # NOTE: | ||||||||||
| 64 | # this is safe only because we | ||||||||||
| 65 | # confirmed above that there was | ||||||||||
| 66 | # no other use of this typeglob | ||||||||||
| 67 | # and so it is ok to delete | ||||||||||
| 68 | 6 | ▶ | delete ${"${pkg}::"}{$method} | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 69 | 6 | }; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 9 | 1; | |||||||||
| 73 | |||||||||||
| 74 | __END__ | ||||||||||