File: FieldHash.pm

package info (click to toggle)
libb-hooks-endofscope-perl 0.28-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 356 kB
  • sloc: perl: 497; makefile: 13
file content (45 lines) | stat: -rw-r--r-- 1,118 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Implementation of a pure-perl on_scope_end for perls > 5.10
# (relies on Hash::Util:FieldHash)

package # hide from pause
  B::Hooks::EndOfScope::PP::FieldHash;

use strict;
use warnings;

our $VERSION = '0.28';

use Tie::Hash ();
use Hash::Util::FieldHash 'fieldhash';

# Here we rely on a combination of several behaviors:
#
# * %^H is deallocated on scope exit, so any references to it disappear
# * A lost weakref in a fieldhash causes the corresponding key to be deleted
# * Deletion of a key on a tied hash triggers DELETE
#
# Therefore the DELETE of a tied fieldhash containing a %^H reference will
# be the hook to fire all our callbacks.

fieldhash my %hh;
{
  package # hide from pause too
    B::Hooks::EndOfScope::PP::_TieHintHashFieldHash;
  our @ISA = ( 'Tie::StdHash' );  # in Tie::Hash, in core
  sub DELETE {
    my $ret = shift->SUPER::DELETE(@_);
    B::Hooks::EndOfScope::PP::__invoke_callback($_) for @$ret;
    $ret;
  }
}

sub on_scope_end (&) {
  $^H |= 0x020000;

  tie(%hh, 'B::Hooks::EndOfScope::PP::_TieHintHashFieldHash')
    unless tied %hh;

  push @{ $hh{\%^H} ||= [] }, $_[0];
}

1;