File Coverage

blib/lib/Data/Thunk/Object.pm
Criterion Covered Total %
statement 44 53 83.0
branch 8 16 50.0
condition 0 3 0.0
subroutine 14 15 93.3
pod 0 5 0.0
total 66 92 71.7


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package Data::Thunk::Object;
4             BEGIN {
5 1     1   34 $Data::Thunk::Object::AUTHORITY = 'cpan:NUFFIN';
6             }
7             BEGIN {
8 1     1   22 $Data::Thunk::Object::VERSION = '0.07';
9             }
10 1     1   6 use base qw(Data::Thunk::Code);
  1         2  
  1         108  
11              
12 1     1   6 use strict;
  1         1  
  1         34  
13 1     1   22 use warnings;
  1         2  
  1         43  
14              
15 1     1   6 use Scalar::Util qw(blessed reftype);
  1         1  
  1         69  
16              
17 1     1   6 use namespace::clean;
  1         2  
  1         7  
18              
19 1     1   181 use UNIVERSAL::ref;
  1         2  
  1         7  
20              
21             our $get_field = sub {
22             my ( $obj, $field ) = @_;
23              
24             my $thunk_class = blessed($obj) or return;
25             bless $obj, "Data::Thunk::NoOverload";
26              
27             my $exists = exists $obj->{$field};
28             my $value = $obj->{$field};
29              
30             bless $obj, $thunk_class;
31              
32             # ugly, but it works
33             return ( wantarray
34             ? ( $exists, $value )
35             : $value );
36             };
37              
38             sub ref {
39 1     1 0 6 my ( $self, @args ) = @_;
40              
41 1 50       3 if ( my $class = $self->$get_field("class") ) {
42 1         7 return $class;
43             } else {
44 0         0 return $self->SUPER::ref(@args);
45             }
46             }
47              
48              
49             foreach my $sym (keys %UNIVERSAL::) {
50 1     1   273 no strict 'refs';
  1         2  
  1         474  
51              
52             next if $sym eq 'ref::';
53             next if defined &$sym;
54              
55             local $@;
56              
57 2 50   2 0 1616 eval "sub $sym {
  2 0   0 0 8  
  0 100   6 0 0  
  2 100   3 0 56  
  0         0  
  0         0  
  0         0  
  0         0  
  6         3319  
  6         22  
  1         16  
  5         245  
  3         9  
  3         11  
  1         17  
  2         56  
58             my ( \$self, \@args ) = \@_;
59              
60             if ( my \$class = \$self->\$get_field('class') ) {
61             return \$class->$sym(\@args);
62             } else {
63             return \$self->SUPER::$sym(\@args);
64             }
65             }; 1" || warn $@;
66             }
67              
68             sub AUTOLOAD {
69 1     1   3 my ( $self, @args ) = @_;
70 1         10 my ( $method ) = ( our $AUTOLOAD =~ /([^:]+)$/ );
71              
72 1 50       11 if ( $method !~ qr/^(?: class | code )$/ ) {
73 1         4 my ( $exists, $value ) = $self->$get_field($method);
74              
75 1 50       6 if ( $exists ) {
76 0 0 0     0 if ( CORE::ref($value) && reftype($value) eq 'CODE' ) {
77 0         0 return $self->$value(@args);
78             } else {
79 0         0 return $value;
80             }
81             }
82             }
83              
84 1         8 unshift @_, $method;
85 1         7 goto $Data::Thunk::Code::vivify_and_call;
86             }
87              
88             1;
89              
90             __END__