| 1 | 2 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Hash::Objectify; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # ABSTRACT: Create objects from hashes on the fly | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | our $VERSION = '0.006'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Sub::Install; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Scalar::Util qw/blessed/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | my %CACHE; | |||||||||||||||||||||||||||
| 16 | 2 | my $COUNTER = 0; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub import { | ||||||||||||||||||||||||||||
| 19 | 2 | ▶ | my ($class) = @_; | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 2 | my $caller = caller; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Sub::Install::install_sub( | ||||||||||||||||||||||||||||
| 23 | { | ||||||||||||||||||||||||||||
| 24 | code => sub { | ||||||||||||||||||||||||||||
| 25 | 11 | ▶ | my ( $ref, $package ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 26 | 11 | my $type = ref $ref; | |||||||||||||||||||||||||||
| 27 | 11 | ▶ | unless ( $type eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 3 | ▶ | $type = | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 29 | $type eq '' ? "a scalar value" | ||||||||||||||||||||||||||||
| 30 | : blessed($ref) ? "an object of class $type" | ||||||||||||||||||||||||||||
| 31 | : "a reference of type $type"; | ||||||||||||||||||||||||||||
| 32 | 3 | croak "Error: Can't objectify $type"; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 8 | ▶ | if ( defined $package ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | @{ $package . '::ISA' } = 'Hash::Objectified' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | unless $package->isa('Hash::Objectified'); | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | else { | ||||||||||||||||||||||||||||
| 40 | 6 | my ( $caller, undef, $line ) = caller; | |||||||||||||||||||||||||||
| 41 | 6 | my $cachekey = join "", sort keys %$ref; | |||||||||||||||||||||||||||
| 42 | 6 | ▶ | if ( !defined $CACHE{$caller}{$line}{$cachekey} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 5 | $package = $CACHE{$caller}{$line}{$cachekey} = "Hash::Objectified$COUNTER"; | |||||||||||||||||||||||||||
| 45 | 5 | $COUNTER++; | |||||||||||||||||||||||||||
| 46 | 5 | @{ $package . '::ISA' } = 'Hash::Objectified'; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | else { | ||||||||||||||||||||||||||||
| 49 | 1 | $package = $CACHE{$caller}{$line}{$cachekey}; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 8 | bless {%$ref}, $package; | |||||||||||||||||||||||||||
| 53 | }, | ||||||||||||||||||||||||||||
| 54 | 2 | into => $caller, | |||||||||||||||||||||||||||
| 55 | as => 'objectify', | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | package Hash::Objectified; | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | ▶ | use Class::XSAccessor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | our $AUTOLOAD; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub can { | ||||||||||||||||||||||||||||
| 67 | 5 | ▶ | my ( $self, $key ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
can: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 68 | 5 | ▶ | return undef unless ref $self && exists $self->{$key}; ## no critic | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $self and exists $self->{$key}
MC/DC50%1/2
ref $self and exists $self->{$key}
ref $self
exists $self->{$key}
Truth table66%2/3
A and B
A= ref $selfB= exists $self->{$key}
| |||||||||||||||||||||||||||||
| 69 | 4 | $self->$key; # install accessor if not installed | |||||||||||||||||||||||||||
| 70 | 4 | return $self->SUPER::can($key); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | sub AUTOLOAD { | ||||||||||||||||||||||||||||
| 74 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 75 | 6 | my $method = $AUTOLOAD; | |||||||||||||||||||||||||||
| 76 | 6 | $method =~ s/.*:://; | |||||||||||||||||||||||||||
| 77 | 6 | ▶ | if ( ref $self && exists $self->{$method} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $self and exists $self->{$method}
MC/DC50%1/2
ref $self and exists $self->{$method}
ref $self
exists $self->{$method}
Truth table66%2/3
A and B
A= ref $selfB= exists $self->{$method}
| |||||||||||||||||||||||||||||
| 78 | 5 | Class::XSAccessor->import( | |||||||||||||||||||||||||||
| 79 | accessors => { $method => $method }, | ||||||||||||||||||||||||||||
| 80 | class => ref $self | ||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | else { | ||||||||||||||||||||||||||||
| 84 | 1 | ▶ | my $class = ref $self || $self; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 85 | 1 | die qq{Can't locate object method "$method" via package "$class"}; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 5 | return $self->$method(@_); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | ▶ | sub DESTROY { } # because we AUTOLOAD, we need this too | |||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 2 | 1; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # vim: ts=4 sts=4 sw=4 et: | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | __END__ | ||||||||||||||||||||||||||||