| 1 | 2 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
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 Object::Adhoc; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | our $AUTHORITY = 'cpan:TOBYINK'; | |||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = '0.006'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Digest::MD5 qw( md5_hex ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Exporter::Shiny qw( object make_class ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | our @EXPORT = qw( object ); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | BEGIN { | ||||||||||||||||||||||||||||
| 15 | *USE_XS = eval 'use Class::XSAccessor 1.19 (); 1' | ||||||||||||||||||||||||||||
| 16 | ? sub () { !!1 } | ||||||||||||||||||||||||||||
| 17 | 2 | ▶ | : sub () { !!0 }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | }; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | BEGIN { | ||||||||||||||||||||||||||||
| 21 | 2 | ▶ | require Hash::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 22 | *lock_ref_keys = 'Hash::Util'->can('lock_ref_keys') | ||||||||||||||||||||||||||||
| 23 | 2 | ▶ | || sub { return; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
'Hash::Util'->can('lock_ref_keys') || sub {
return;
}
MC/DC0%0/2
'Hash::Util'->can('lock_ref_keys') || sub {
return;
}
'Hash::Util'->can('lock_ref_keys')
sub {
return;
}
Truth table33%1/3
A || B
A= 'Hash::Util'->can('lock_ref_keys')B= sub {
return;
}
| |||||||||||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 2 | our $RESERVED_REGEXP; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # Yes, you can push extra methods onto this array if you need to, | ||||||||||||||||||||||||||||
| 29 | # but if you do that, then set $RESERVED_REGEXP to undef so that | ||||||||||||||||||||||||||||
| 30 | # make_class will rebuild it! | ||||||||||||||||||||||||||||
| 31 | # | ||||||||||||||||||||||||||||
| 32 | 2 | our @RESERVED_METHODS = qw( | |||||||||||||||||||||||||||
| 33 | import unimport | ||||||||||||||||||||||||||||
| 34 | DESTROY | ||||||||||||||||||||||||||||
| 35 | AUTOLOAD | ||||||||||||||||||||||||||||
| 36 | isa DOES does can VERSION | ||||||||||||||||||||||||||||
| 37 | meta new | ||||||||||||||||||||||||||||
| 38 | TO_JSON | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | # | ||||||||||||||||||||||||||||
| 41 | # Note that tie-related stuff isn't on the list of reserved methods | ||||||||||||||||||||||||||||
| 42 | # because people using those names isn't likely to cause any actual | ||||||||||||||||||||||||||||
| 43 | # harm. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sub object { | ||||||||||||||||||||||||||||
| 46 | 4 | ▶ | my ($data, $keys) = @_; | ||||||||||||||||||||||||||
|
Subroutine
object: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 4 | ▶ | $keys ||= [ keys %$data ]; | ||||||||||||||||||||||||||
|
Condition100%2/2
$keys ||= [keys %$data]
MC/DC100%1/1
$keys ||= [keys %$data]
$keys
Truth table100%2/2
A
A= $keys
| |||||||||||||||||||||||||||||
| 48 | 4 | bless $data, make_class($keys); | |||||||||||||||||||||||||||
| 49 | 4 | lock_ref_keys($data, @$keys); | |||||||||||||||||||||||||||
| 50 | 4 | $data; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 2 | my %made; | |||||||||||||||||||||||||||
| 54 | sub make_class { | ||||||||||||||||||||||||||||
| 55 | 4 | ▶ | my ($keys) = @_; | ||||||||||||||||||||||||||
|
Subroutine
make_class: called CC=9 SCAR=24.4
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 4 | my $joined = join "|", sort(@$keys); | |||||||||||||||||||||||||||
| 57 | 4 | ▶ | return $made{$joined} if $made{$joined}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 3 | my $class = sprintf('%s::__ANON__::%s', __PACKAGE__, md5_hex($joined)); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 3 | my %getters = map(+($_ => $_), @$keys); | |||||||||||||||||||||||||||
| 62 | 3 | ▶ | my %predicates = map(+((/^_/?"_has$_":"has_$_")=> $_), @$keys); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 3 | ▶ | $RESERVED_REGEXP ||= do { | ||||||||||||||||||||||||||
|
Condition66%2/3
$Object::Adhoc::RESERVED_REGEXP ||= do {
my $re = join("|", map(quotemeta($_), @RESERVED_METHODS));
qr/\A(?:$re)\z/
}
MC/DC0%0/2
$Object::Adhoc::RESERVED_REGEXP ||= do {
my $re = join("|", map(quotemeta($_), @RESERVED_METHODS));
qr/\A(?:$re)\z/
}
$Object::Adhoc::RESERVED_REGEXP
do {
my $re = join("|", map(quotemeta($_), @RESERVED_METHODS));
qr/\A(?:$re)\z/
}
Truth table66%2/3
A ||= B
A= $Object::Adhoc::RESERVED_REGEXPB= do {
my $re = join("|", map(quotemeta($_), @RESERVED_METHODS));
qr/\A(?:$re)\z/
}
| |||||||||||||||||||||||||||||
| 65 | 2 | my $re = join "|", map quotemeta($_), @RESERVED_METHODS; | |||||||||||||||||||||||||||
| 66 | 2 | qr/\A(?:$re)\z/; | |||||||||||||||||||||||||||
| 67 | }; | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 3 | for my $key (@$keys) { | |||||||||||||||||||||||||||
| 70 | 5 | ▶ | if (exists $predicates{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | delete $predicates{$key}; | |||||||||||||||||||||||||||
| 72 | 0 | require Carp; | |||||||||||||||||||||||||||
| 73 | 0 | Carp::carp("Ambiguous method '$key' is getter, not predicate"); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 5 | ▶ | if ($key !~ /^[^\W0-9]\w*$/s or $key =~ $RESERVED_REGEXP) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $key =~ /^[^\W0-9]\w*$/s or $key =~ /$RESERVED_REGEXP/
MC/DC0%0/2
not $key =~ /^[^\W0-9]\w*$/s or $key =~ /$RESERVED_REGEXP/
not $key =~ /^[^\W0-9]\w*$/s
$key =~ /$RESERVED_REGEXP/
Truth table33%1/3
not(A) or B
A= not $key =~ /^[^\W0-9]\w*$/sB= $key =~ /$RESERVED_REGEXP/
| |||||||||||||||||||||||||||||
| 76 | 0 | require Carp; | |||||||||||||||||||||||||||
| 77 | 0 | Carp::carp("Key '$key' would be bad method name, not generating methods"); | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | my $predicate = ($key =~ /^_/) ? "_has$key" : "has_$key"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | delete $getters{$key}; | |||||||||||||||||||||||||||
| 80 | 0 | delete $predicates{$predicate}; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 3 | if (USE_XS) { | |||||||||||||||||||||||||||
| 85 | 3 | 'Class::XSAccessor'->import( | |||||||||||||||||||||||||||
| 86 | class => $class, | ||||||||||||||||||||||||||||
| 87 | getters => \%getters, | ||||||||||||||||||||||||||||
| 88 | exists_predicates => \%predicates, | ||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | else { | ||||||||||||||||||||||||||||
| 92 | require B; | ||||||||||||||||||||||||||||
| 93 | my $code = "package $class;\n"; | ||||||||||||||||||||||||||||
| 94 | while (my ($predicate, $key) = each %predicates) { | ||||||||||||||||||||||||||||
| 95 | my $qkey = B::perlstring($key); | ||||||||||||||||||||||||||||
| 96 | $code .= "sub $predicate :method { &Object::Adhoc::_usage if \@_ > 1; CORE::exists \$_[0]{$qkey} }\n"; | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | while (my ($getter, $key) = each %getters) { | ||||||||||||||||||||||||||||
| 99 | my $qkey = B::perlstring($key); | ||||||||||||||||||||||||||||
| 100 | $code .= "sub $getter :method { &Object::Adhoc::_usage if \@_ > 1; \$_[0]{$qkey} }\n"; | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | $code .= "1;\n"; | ||||||||||||||||||||||||||||
| 103 | eval($code) or do { require Carp; Carp::croak($@) }; | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 3 | do { | |||||||||||||||||||||||||||
| 107 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 108 | 3 | *{"$class\::DOES"} = \&_DOES; | |||||||||||||||||||||||||||
| 109 | 3 | *{"$class\::does"} = \&_DOES; | |||||||||||||||||||||||||||
| 110 | 3 | *{"$class\::VERSION"} = \$VERSION; | |||||||||||||||||||||||||||
| 111 | 3 | *{"$class\::TO_JSON"} = \&_TO_JSON; | |||||||||||||||||||||||||||
| 112 | }; | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 3 | $made{$joined} = $class; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | sub _usage { | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | my $caller = (caller(1))[3]; | ||||||||||||||||||||||||||
|
Subroutine
_usage: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 119 | 0 | require Carp; | |||||||||||||||||||||||||||
| 120 | 0 | local $Carp::CarpLevel = 1 + $Carp::CarpLevel; | |||||||||||||||||||||||||||
| 121 | 0 | Carp::croak("Usage: $caller\(self)"); # mimic XS usage message | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | sub _DOES { | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | return !!1 if $_[1] eq __PACKAGE__; | ||||||||||||||||||||||||||
|
Subroutine
_DOES: not called CC=3 SCAR=24.8
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | return !!1 if $_[1] eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | shift->isa(@_); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | sub _TO_JSON { | ||||||||||||||||||||||||||||
| 131 | 1 | ▶ | my %hash = %{ +shift }; | ||||||||||||||||||||||||||
|
Subroutine
_TO_JSON: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 132 | 1 | \%hash; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 2 | 1; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | __END__ | ||||||||||||||||||||||||||||