| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Class::Unique; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
52646
|
use strict; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
84
|
|
|
4
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
63
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
12
|
use Scalar::Util 'refaddr'; |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
171
|
|
|
7
|
2
|
|
|
2
|
|
12
|
use Carp 'croak'; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
244
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my $PKG = "Class::Unique pkg"; |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
|
14
|
2
|
|
|
2
|
1
|
23
|
my $class = shift; |
|
15
|
2
|
|
|
|
|
5
|
my $obj = { }; |
|
16
|
|
|
|
|
|
|
|
|
17
|
2
|
|
|
|
|
12
|
my $unique_class = $class . '::' . refaddr $obj; |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
{ |
|
20
|
2
|
|
|
2
|
|
11
|
no strict 'refs'; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
227
|
|
|
|
2
|
|
|
|
|
3
|
|
|
21
|
2
|
|
|
|
|
4
|
@{ $unique_class . '::ISA' } = ( $class ); |
|
|
2
|
|
|
|
|
54
|
|
|
22
|
|
|
|
|
|
|
} |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# so we don't have to rely on ref() |
|
25
|
2
|
|
|
|
|
7
|
$obj->{$PKG} = $unique_class; |
|
26
|
2
|
|
|
|
|
10
|
return bless $obj, $unique_class; |
|
27
|
|
|
|
|
|
|
} |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub install { |
|
30
|
1
|
|
|
1
|
1
|
3849
|
my $self = shift; |
|
31
|
|
|
|
|
|
|
|
|
32
|
1
|
|
|
|
|
4
|
my %args = @_; |
|
33
|
|
|
|
|
|
|
|
|
34
|
1
|
|
|
|
|
4
|
foreach my $s( keys %args ) { |
|
35
|
2
|
|
|
2
|
|
11
|
no strict 'refs'; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
203
|
|
|
36
|
1
|
|
|
|
|
3
|
*{ $self->{$PKG} . '::' . $s } = $args{$s}; |
|
|
1
|
|
|
|
|
15
|
|
|
37
|
|
|
|
|
|
|
} |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
1; |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 NAME |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Class::Unique - Create a unique subclass for every instance |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 VERSION |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Version 0.04 |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
package MyClass; |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
use base 'Class::Unique'; |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub foo { print "foo!\n"; } |
|
58
|
|
|
|
|
|
|
sub bar { print "bar!\n"; } |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
... |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
use MyClass; |
|
63
|
|
|
|
|
|
|
my $obj1 = MyClass->new; |
|
64
|
|
|
|
|
|
|
my $obj2 = MyClass->new; |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my $new_foo = sub { print "new foo!\n"; }; |
|
67
|
|
|
|
|
|
|
$obj2->install( foo => $new_foo ); |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
$obj1->foo; $obj1->bar; |
|
70
|
|
|
|
|
|
|
$obj2->foo; $obj2->bar; |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Class::Unique is a base class which provides a constructor and some utility routines |
|
75
|
|
|
|
|
|
|
for creating objects which instantiate into a unique subclass. |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
If MyClass is a subclass |
|
78
|
|
|
|
|
|
|
of Class::Unique, and inherrits Class::Unique's constructor, then every object returned |
|
79
|
|
|
|
|
|
|
by C<< MyClass->new >> will be blessed into a dynamically created subclass of MyClass. This |
|
80
|
|
|
|
|
|
|
allows you to modify package data on a per-instance basis. |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
L provides similar functionality; use this module if you want per-instance |
|
83
|
|
|
|
|
|
|
subclasses but you don't need a full prototype-based OO framework. |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 METHODS |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
The following methods are inherrited. |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=over |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=item C |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Constructor. Returns a hash ref blessed into a new dynamically created package. If you need |
|
94
|
|
|
|
|
|
|
to override the constructor, make sure you get your object by using C instead |
|
95
|
|
|
|
|
|
|
of blessing it yourself. |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
package MyClass; |
|
98
|
|
|
|
|
|
|
use base 'Class::Unique'; |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub new { |
|
101
|
|
|
|
|
|
|
my $class = shift; |
|
102
|
|
|
|
|
|
|
my $self = $class->SUPER::new( @_ ); |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# fiddle with $self here.... |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
return $self; |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=item C |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Install a new symbol into an object's namespace. This can be used to dynamically override |
|
112
|
|
|
|
|
|
|
an inherrited subroutine, e.g.: |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
my $code_ref = sub { print "wahoo!\n" }; |
|
115
|
|
|
|
|
|
|
$obj->install( exclaim => $code_ref ); |
|
116
|
|
|
|
|
|
|
$obj->exclaim; |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
This is really just a shortcut for doing: |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
my $pkg = ref $obj; |
|
121
|
|
|
|
|
|
|
no strict 'refs'; |
|
122
|
|
|
|
|
|
|
*{ $pkg . '::subname' } = $code_ref; |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
You can also use C to add other package symbols: |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
my @data = ( 1, 2, 3, 4 ); |
|
127
|
|
|
|
|
|
|
$obj->install( data => \@data ); |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=back |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 AUTHOR |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Mike Friedman, C<< >> |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 THANKS |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Thanks to Stevan Little for submitting some unit tests. |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 BUGS |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
|
143
|
|
|
|
|
|
|
C, or through the web interface at |
|
144
|
|
|
|
|
|
|
L. |
|
145
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress on |
|
146
|
|
|
|
|
|
|
your bug as I make changes. |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Copyright 2005 Mike Friedman, all rights reserved. |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
153
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=cut |
|
156
|
|
|
|
|
|
|
|