| 1 | # Implementation of a pure-perl on_scope_end for perls > 5.10 | ||||||||||||
| 2 | # (relies on Hash::Util:FieldHash) | ||||||||||||
| 3 | |||||||||||||
| 4 | package # hide from pause | ||||||||||||
| 5 | B::Hooks::EndOfScope::PP::FieldHash; | ||||||||||||
| 6 | |||||||||||||
| 7 | 7 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 7 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 7 | our $VERSION = '0.28'; | |||||||||||
| 11 | |||||||||||||
| 12 | 7 | ▶ | use Tie::Hash (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 7 | ▶ | use Hash::Util::FieldHash 'fieldhash'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | # Here we rely on a combination of several behaviors: | ||||||||||||
| 16 | # | ||||||||||||
| 17 | # * %^H is deallocated on scope exit, so any references to it disappear | ||||||||||||
| 18 | # * A lost weakref in a fieldhash causes the corresponding key to be deleted | ||||||||||||
| 19 | # * Deletion of a key on a tied hash triggers DELETE | ||||||||||||
| 20 | # | ||||||||||||
| 21 | # Therefore the DELETE of a tied fieldhash containing a %^H reference will | ||||||||||||
| 22 | # be the hook to fire all our callbacks. | ||||||||||||
| 23 | |||||||||||||
| 24 | 7 | fieldhash my %hh; | |||||||||||
| 25 | { | ||||||||||||
| 26 | package # hide from pause too | ||||||||||||
| 27 | B::Hooks::EndOfScope::PP::_TieHintHashFieldHash; | ||||||||||||
| 28 | 7 | our @ISA = ( 'Tie::StdHash' ); # in Tie::Hash, in core | |||||||||||
| 29 | 10 | ▶ | sub DELETE { | ||||||||||
|
Subroutine
DELETE: called CC=2 SCAR=6.9
| |||||||||||||
| 30 | 10 | my $ret = shift->SUPER::DELETE(@_); | |||||||||||
| 31 | 10 | B::Hooks::EndOfScope::PP::__invoke_callback($_) for @$ret; | |||||||||||
| 32 | 10 | $ret; | |||||||||||
| 33 | } | ||||||||||||
| 34 | } | ||||||||||||
| 35 | |||||||||||||
| 36 | 7 | ▶ | sub on_scope_end (&) { | ||||||||||
|
Subroutine
on_scope_end: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 37 | 11 | $^H |= 0x020000; | |||||||||||
| 38 | |||||||||||||
| 39 | 11 | ▶ | tie(%hh, 'B::Hooks::EndOfScope::PP::_TieHintHashFieldHash') | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 40 | unless tied %hh; | ||||||||||||
| 41 | |||||||||||||
| 42 | 11 | ▶ | push @{ $hh{\%^H} ||= [] }, $_[0]; | ||||||||||
|
Condition100%2/2
$hh{\%^H} ||= []
MC/DC100%1/1
$hh{\%^H} ||= []
$hh{\%^H}
Truth table100%2/2
A
A= $hh{\%^H}
| |||||||||||||
| 43 | } | ||||||||||||
| 44 | |||||||||||||
| 45 | 7 | 1; | |||||||||||