Anonymous.pm

filter: Statementstmt 98.4% 64 / 65 Branchbran 91.6% 11 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 93.7% 15 / 16 Podpod 100% 4 / 4 totaltotal 93.1% 95 / 102 CC 8 SCAR 8.0
CC
8
Cov
95%
__ANON__
5.0
__ANON__
3.0
__ANON__
2.0
CRAP 2.2
« Anonymous/Utils.pm ↑ Summary Anonymous/Instance.pm »
1 package Class::Anonymous;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = '0.03';
7 4 $VERSION = eval $VERSION;
8  
9 4 ▶use Class::Anonymous::Instance;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 our @EXPORT = qw/class extend via/;
13  
14 4 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Scalar::Util 'refaddr';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 my $bless = eval {
18 4 require Package::Anon;
19 4 my $stash = Package::Anon->new;
20 4 $stash->add_method(AUTOLOAD => \&Class::Anonymous::Instance::AUTOLOAD);
21 4 $stash->add_method(DESTROY => \&Class::Anonymous::Instance::DESTROY);
22 4 $stash->add_method(can => \&Class::Anonymous::Instance::can);
23 4 $stash->add_method(isa => \&Class::Anonymous::Instance::isa);
24 17 ▶ sub { $stash->bless($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
25 0 ▶} || sub { bless $_[0], 'Class::Anonymous::Instance' };
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
eval { do { require Package::Anon; my $stash = 'Package::Anon'->new; $stash->add_method('AUTOLOAD', \&Class::Anonymous::Instance::AUTOLOAD); $stash->add_method('DESTROY', \&Class::Anonymous::Instance::DESTROY); $stash->add_method('can', \&Class::Anonymous::Instance::can); $stash->add_method('isa', \&Class::Anonymous::Instance::isa); sub { $stash->bless($_[0]); } } } || sub { bless $_[0], 'Class::Anonymous::Instance'; }
l!l&&r!l&&!r
400
MC/DC0%0/2
eval { do { require Package::Anon; my $stash = 'Package::Anon'->new; $stash->add_method('AUTOLOAD', \&Class::Anonymous::Instance::AUTOLOAD); $stash->add_method('DESTROY', \&Class::Anonymous::Instance::DESTROY); $stash->add_method('can', \&Class::Anonymous::Instance::can); $stash->add_method('isa', \&Class::Anonymous::Instance::isa); sub { $stash->bless($_[0]); } } } || sub { bless $_[0], 'Class::Anonymous::Instance'; }
eval { do { require Package::Anon; my $stash = 'Package::Anon'->new; $stash->add_method('AUTOLOAD', \&Class::Anonymous::Instance::AUTOLOAD); $stash->add_method('DESTROY', \&Class::Anonymous::Instance::DESTROY); $stash->add_method('can', \&Class::Anonymous::Instance::can); $stash->add_method('isa', \&Class::Anonymous::Instance::isa); sub { $stash->bless($_[0]); } } } sub { bless $_[0], 'Class::Anonymous::Instance'; }
Truth table33%1/3
A || B
A= eval { do { require Package::Anon; my $stash = 'Package::Anon'->new; $stash->add_method('AUTOLOAD', \&Class::Anonymous::Instance::AUTOLOAD); $stash->add_method('DESTROY', \&Class::Anonymous::Instance::DESTROY); $stash->add_method('can', \&Class::Anonymous::Instance::can); $stash->add_method('isa', \&Class::Anonymous::Instance::isa); sub { $stash->bless($_[0]); } } }B= sub { bless $_[0], 'Class::Anonymous::Instance'; }
ABresult
1X1
011
000
26  
27 4 our $CURRENT;
28  
29 8 ▶my $new = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
30 8 my $class = shift;
31 8 my @isa = $class->isa();
32 8 push @isa, $class;
33 8 my $self = instance(@isa);
34 8 local $CURRENT = $self;
35 8 $_->('BUILD')->($self, @_) for @isa;
36 8 return $self;
37 4 };
38  
39 17 ▶sub instance {
Subroutine instance: called CC=1 SCAR=0.0
Pod documented
40 17 my @isa = @_;
41 17 my %methods;
42  
43 20 ▶ my $isa = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
44 20 my $self = shift;
45 20 ▶ return @isa unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 13 7 20
46 7 my $class = shift;
47 7 ▶ return unless ref $class;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $class 1 6 7
48 6 my $addr = refaddr $class;
49 6 ▶ return first { $addr == refaddr $_ } reverse @isa;
Subroutine __ANON__: called CC=1 SCAR=0.0
50 17 };
51  
52 87 ▶ return $bless->(sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
53 87 ▶ return unless my $name = shift;
Branch: 1 branch
BranchTrueFalseTotal
unless my $name = shift() 0 87 87
54 87 ▶ return $isa if $name eq 'isa';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "isa" 20 67 87
55 67 ▶ return $new if $name eq 'new';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "new" 8 59 67
56 59 ▶ $methods{$name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 24 35 59
57 59 return $methods{$name};
58 17 });
59 };
60  
61 9 ▶sub class (&) {
Subroutine class: called CC=1 SCAR=0.0
Pod documented
62 9 my $builder = shift;
63 9 my $class = instance(@_);
64 9 $class->(BUILD => $builder);
65 9 return $class;
66 }
67  
68 5 ▶sub extend {
Subroutine extend: called CC=1 SCAR=0.0
Pod documented
69 5 my ($class, $extension) = @_;
70 5 my @isa = $class->isa();
71 5 return &class($extension, @isa, $class);
72 }
73  
74 5 ▶sub via (&) { $_[0] }
Subroutine via: called CC=1 SCAR=0.0
Pod documented
75  
76 4 1;
77  
78 =head1 NAME
79  
80 Class::Anonymous - Truly private classes with private data for Perl5
81  
82 =head1 SYNOPSIS
83  
84 use feature 'say';
85 use Class::Anonymous;
86 use Class::Anonymous::Utils ':all';
87  
88 my $lifeform = class {
89 my ($self, $name) = @_;
90 method greeting => sub { "My name is $name" };
91 };
92  
93 my $mortal = extend $lifeform => via {
94 my ($self, $name, $age) = @_;
95 around greeting => sub {
96 my $orig = shift;
97 $orig->() . " and I'm $age years old";
98 };
99 };
100  
101 my $bob = $mortal->new('Bob', 40);
102 say $bob->greeting;
103 say 'Bob is mortal' if $bob->isa($mortal);
104 say 'Bob is a lifeform' if $bob->isa($lifeform);
105  
106 =head1 DESCRIPTION
107  
108 L<Class::Anonymous> implements anonymous classes and private data.
109 This private data is just the lexical variables available during the builder callback(s) that are used to build the class.
110 If L<Package::Anon> is available, then no stash entry is created at all.
111 If not, then the classes will actually be blessed into L<Class::Anonymous::Instance> though this is to be considered an implementation detail and subject to change.
112  
113 The instance itself is a code reference which can be thought of as a meta-object.
114 Called with a single string fetches the method of that name and returns it.
115 Called with a string and a code reference attaches a new method to the object.
116 Helper functions are provided in L<Class::Anonymous::Utils> which provides L<Class::Anonymous::Utils/method> and method modifiers for ease of use.
117  
118 my $class = class {
119 my ($self) = @_;
120 $self->(mymethod = sub { ... });
121 my $mymethod = $self->('mymethod');
122 }
123  
124 =head1 EXPORTED FUNCTIONS
125  
126 =head2 class
127  
128 my $class = class { my ($self) = @_; ... };
129  
130 Define a new class.
131 Takes a block (or code reference) which will be called to build and instance of that class.
132 The callback is called with the new empty instance and any arguments passed to C<new>.
133 Note that subclasses might need more initialization arguments, so you might want to plan for that.
134  
135 =head2 extend
136  
137 my $subclass = extend $class, sub { my ($self) = @_; ... };
138  
139 Define a new subclass of an existing anonymous class.
140 Takes an existing class and a code reference which will be called after the parent class builder callback(s).
141 Note that all callbacks receive the same arguments, so you might want to plan for that.
142  
143 =head2 via
144  
145 my $subclass = extend $class, via { my ($self) = @_; ... };
146  
147 Sugar for defining a code reference as a block, simply to make L</extend> look better.
148  
149 =head1 OTHER FUNCTIONS
150  
151 =head2 instance
152  
153 Builds a raw instance of a generic anonymous object.
154 All arguments are the classes to be returned by C<isa>.
155 By default it only fully implements the C<isa>, C<AUTOLOAD> and C<DESTROY> methods.
156 If the instance implements the C<BUILD> method then it is itself a class and you can call C<new> on it.
157  
158 =head3 Relationship to the class function
159  
160 The L</class> function simply creates an C<instance> when it creates a class.
161 It then attaches a C<BUILD> function; this function creates another instance, calls all the C<BUILD> methods from all the C<isa> classes on it, then returns it.
162  
163 =head1 SOURCE REPOSITORY
164  
165 L<http://github.com/jberger/Class-Anonymous>
166  
167 =head1 AUTHOR
168  
169 Joel Berger, E<lt>joel.a.berger@gmail.comE<gt>
170  
171 =head1 COPYRIGHT AND LICENSE
172  
173 Copyright (C) 2015 by Joel Berger
174  
175 This library is free software; you can redistribute it and/or modify
176 it under the same terms as Perl itself.
« Anonymous/Utils.pm ↑ Summary Anonymous/Instance.pm »