File Coverage

blib/lib/UNIVERSAL/ref.pm
Criterion Covered Total %
statement 38 40 95.0
branch 7 8 87.5
condition n/a
subroutine 11 12 91.6
pod n/a
total 56 60 93.3


line stmt bran cond sub pod time code
1             package UNIVERSAL::ref;
2             BEGIN {
3 1     1   28469 $UNIVERSAL::ref::VERSION = '0.14';
4             }
5 1     1   10 use strict;
  1         2  
  1         31  
6 1     1   5 use warnings;
  1         1  
  1         33  
7 1     1   1025 use B::Utils;
  1         5859  
  1         196  
8              
9             our @hooked;
10             our @needs_truth = qw(overload);
11              
12             sub import {
13 4     4   3315 my $class = caller;
14 4         5 my %unique;
15 4         9 @hooked = grep { !$unique{$_}++ } ( @hooked, $class );
  10         411  
16             }
17              
18             sub unimport {
19 0     0   0 my $class = caller;
20 0         0 @hooked = grep $_ ne $class, @hooked;
21             }
22              
23             my $DOES;
24 1 50   1   137 BEGIN { $DOES = UNIVERSAL->can('DOES') ? 'DOES' : 'isa' }
25              
26             sub _hook {
27              
28             # Below, you'll see that there is special dispensation for never
29             # hooking the function named UNIVERSAL::ref::_hook. That's why this
30             # ref() is safe from predation by this module.
31              
32             # Is this object asserting that it is an ancestor of any hooked class?
33 6     6   345742 my $is_hooked;
34 6         13 my $obj_class = CORE::ref $_[0];
35 6         9 my $caller_class = caller;
36              
37             # For any special classes needing truth, just return if we've got
38             # any of those.
39 6         16 for my $class (@needs_truth) {
40 6 100       67 if ( $caller_class->$DOES($class) ) {
41              
42             # CORE::ref
43 2         8 return $obj_class;
44             }
45             }
46              
47             #
48 4         12 for my $hooked_class (@hooked) {
49              
50             # Find only hooked ancestries that pertain this object.
51 13 100       99 next unless $obj_class->$DOES($hooked_class);
52              
53             # Check that the call wasn't made from within this object's
54             # ancestry. It has to be possible for an object to ask
55             # questions about itself without getting lies.
56 3 100       18 next if $obj_class->$DOES($caller_class);
57              
58 2         11 return $_[0]->ref;
59             }
60              
61             # CORE::ref
62 2         13 return $obj_class;
63             }
64              
65 1     1   9 use XSLoader;
  1         1  
  1         34  
66             $| = 1;
67             XSLoader::load( 'UNIVERSAL::ref', $UNIVERSAL::ref::VERSION );
68              
69 1     1   4 use B 'svref_2object';
  1         2  
  1         67  
70 1     1   5 use B::Utils 'all_roots';
  1         2  
  1         153  
71             my %roots = all_roots();
72             for my $nm ( sort keys %roots ) {
73             my $op = $roots{$nm};
74              
75             next unless $$op;
76             next if $nm eq 'UNIVERSAL::ref::_hook';
77              
78             if ( defined &$nm ) {
79             my $cv = svref_2object( \&$nm );
80             next unless ${ $cv->ROOT };
81             next unless ${ $cv->START };
82             }
83              
84             _fixupop($op);
85             }
86              
87 1     1   5 no warnings;
  1         1  
  1         56  
88             q[Let's Make Love and Listen to Death From Above];
89              
90             __END__