blib/lib/Class/Data/Reloadable.pm

filter: Statementstmt 91.9% 57 / 62 Branchbran 54.5% 12 / 22 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 92.3% 12 / 13 Podpod 100% 1 / 1 totaltotal 72.8% 86 / 118 CC 18 SCAR 14.1
CC
18
Cov
76%
AUTOLOAD
6.1
__classdata
5.7
__has
5.6
CRAP 4.1
↑ Summary
1 package Class::Data::Reloadable;
2 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # use Devel::StackTrace;
6  
7 5 ▶use Class::ISA;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use NEXT;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our ( $VERSION, $AUTOLOAD, $DEBUG );
11  
12 =head1 NAME
13  
14 Class::Data::Reloadable - inheritable, overridable class data that survive reloads
15  
16 =cut
17  
18 5 $VERSION = 0.04;
19  
20 =head1 SYNOPSIS
21  
22 package Stuff;
23 use base qw(Class::Data::Reloadable);
24  
25 # Set up DataFile as inheritable class data.
26 Stuff->mk_classdata('DataFile');
27  
28 # Declare the location of the data file for this class.
29 Stuff->DataFile('/etc/stuff/data');
30  
31 # ... reload Stuff within same interpreter
32  
33 print Stuff->DataFile; # /etc/stuff/data
34  
35 =head1 DESCRIPTION
36  
37 A drop-in replacement for L<Class::Data::Inheritable|Class::Data::Inheritable>,
38 but subclasses can be reloaded without losing their class data. This is useful
39 in mod_perl development, and may be useful elsewhere.
40  
41 In mod_perl, L<Apache::Reload|Apache::Reload>
42 conveniently reloads modules that have been modified, rather than having to
43 restart Apache. This works well unless the module stores class data that are
44 not re-created during the reload. In this situation, you still need to restart the
45 server, in order to rebuild the class data.
46  
47 Saves many (if your code starts out buggy like mine) Apache restarts.
48  
49 But only if you're strict about storing B<all> class data using this mechanism.
50  
51 See L<Class::Data::Inheritable|Class::Data::Inheritable> for more examples.
52  
53 =head2 Drop-in
54  
55 If you want to switch over to this module in a large app, instead of changing
56 all references to L<Class::Data::Inheritable|Class::Data::Inheritable>, you can
57 instead create an empty subclass C<Class::Data::Inheritable> and put it somewhere
58 in your Perl search path that gets searched before the path with the real
59 L<Class::Data::Inheritable|Class::Data::Inheritable>, e.g.
60  
61 use lib '/my/lib';
62  
63 and /my/lib/Class/Data/Inheritable.pm is:
64  
65 package Class::Data::Inheritable;
66 use base 'Class::Data::Reloadable';
67 1;
68  
69 =head1 METHODS
70  
71 =over
72  
73 =item mk_classdata
74  
75 Creates a classdata slot, optionally setting a value into it.
76  
77 $client->mk_classdata( 'foo' );
78 $client->classdata->foo( 'bar' );
79 # same thing:
80 $client->mk_classdata( foo => 'bar' );
81  
82 Note that during a reload, this method may be called again for an existing
83 attribute. If so, any value passed with the method is silently ignored, in
84 favour of whatever value was in the slot before the reload.
85  
86 This also provides a C<_foo_accessor> alias.
87  
88 =cut
89  
90 =item AUTOLOAD
91  
92 If the class has been reloaded, and if before the reload, other classes have
93 called C<mk_classdata> on this class, then some accessors will be missing after
94 the reload. AUTOLOAD replaces these methods the first time they are called.
95  
96 Redispatches (via L<NEXT|NEXT>) to any C<AUTOLOAD> method further up the
97 chain if no attribute is found.
98  
99 =back
100  
101 =cut
102  
103 3 ▶sub mk_classdata {
Subroutine mk_classdata: called CC=3 SCAR=12.5
Pod documented
104 3 my ( $proto, $attribute ) = ( shift, shift );
105  
106 # During a reload, this method will often be called again. In that case,
107 # do _not_ set any value being passed in this call - discard it and return
108 # whatever was last stored there before the reload.
109 3 ▶ return $proto->$attribute if $proto->__has( $attribute ) && $proto->can( $attribute );
Branch: 1 branch
BranchTrueFalseTotal
if $proto->__has($attribute) and $proto->can($attribute) 0 3 3
Condition33%1/3
$proto->__has($attribute) and $proto->can($attribute)
!ll&&!rl&&r
300
MC/DC0%0/2
$proto->__has($attribute) and $proto->can($attribute)
$proto->__has($attribute) $proto->can($attribute)
Truth table33%1/3
A and B
A= $proto->__has($attribute)B= $proto->can($attribute)
ABresult
0X0
100
111
110  
111 3 $proto->__mk_accessor( $attribute, @_ );
112 }
113  
114 2 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=5 SCAR=18.0
115 2 my $proto = shift;
116  
117 2 my ( $attribute ) = $AUTOLOAD =~ /([^:]+)$/;
118  
119 2 ▶ warn "AUTOLOADING $attribute ($AUTOLOAD) in $proto\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Class::Data::Reloadable::DEBUG 0 2 2
120  
121 2 my $owner = eval { $proto->__has( $attribute ) };
122  
123 2 ▶ if ( my $er = $@ )
Branch: 1 branch
BranchTrueFalseTotal
if (my $er = $@) 0 2 2
124 {
125 0 die "Error AUTOLOADing $AUTOLOAD for $proto - $er";
126 }
127  
128 2 ▶ if ( $owner )
Branch: 1 branch
BranchTrueFalseTotal
if ($owner) { } 0 2 2
129 {
130 # put it back where it came from
131 0 $owner->__mk_accessor( $attribute );
132 0 return $proto->$attribute( @_ );
133 }
134 else
135 {
136 2 ▶ warn "'$attribute' not owned by C::D::Reloadable client - delegating AUTOLOAD in $proto\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Class::Data::Reloadable::DEBUG 0 2 2
137 # maybe it was intended for somewhere else
138 2 return $proto->NEXT::ACTUAL::DISTINCT::AUTOLOAD( @_ );
139 }
140 }
141  
142 0 ▶sub DESTROY { $_[0]->NEXT::DISTINCT::DESTROY() }
Subroutine DESTROY: not called CC=1 SCAR=6.9
143  
144 3 ▶sub __mk_accessor {
Subroutine __mk_accessor: called CC=4 SCAR=14.1
145 3 my ( $proto, $attribute ) = ( shift, shift );
146  
147 3 ▶ my $client = ref( $proto ) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
148  
149 3 ▶ warn "making '$attribute' accessor in $client\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Class::Data::Reloadable::DEBUG 0 3 3
150  
151 3 ▶ my $accessor = sub { shift->__classdata( $attribute, @_ ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
152  
153 3 my $alias = "_${attribute}_accessor";
154  
155 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
156 3 *{"$client\::$attribute"} = $accessor;
157 3 *{"$client\::$alias"} = $accessor;
158  
159 3 ▶ $proto->$attribute( $_[0] ) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 3 3
160 }
161  
162 # in case you want to mess with it - but don't do that
163 5 our $ClassData;
164  
165 7 ▶sub __classdata {
Subroutine __classdata: called CC=5 SCAR=17.5
166 7 my ( $proto, $attribute ) = ( shift, shift );
167  
168 7 ▶ my $client = ref( $proto ) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
070
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
169  
170 # if there's data to set, put it in the client slot
171 7 ▶ return( $ClassData->{ $client }{ $attribute } = $_[0] ) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 4 7
172  
173 # if there's no data to set, search for a previous value
174 4 foreach my $ima ( Class::ISA::self_and_super_path( $client ) )
175 {
176 return $ClassData->{ $ima }{ $attribute } if
177 4 ▶ exists $ClassData->{ $ima }{ $attribute };
Branch: 1 branch
BranchTrueFalseTotal
if exists $Class::Data::Reloadable::ClassData->{$ima}{$attribute} 4 0 4
178 }
179  
180 0 return undef; # should always at least return undef (i.e. not an empty list)
181 }
182  
183 5 ▶sub __has {
Subroutine __has: called CC=5 SCAR=17.2
184 5 my ( $proto, $attribute ) = @_;
185  
186 5 ▶ my $client = ref( $proto ) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
187  
188 5 my $owner;
189  
190 5 foreach my $ima ( Class::ISA::self_and_super_path( $client ) )
191 {
192 10 ▶ $owner = $ima if exists $ClassData->{ $ima }{ $attribute };
Branch: 1 branch
BranchTrueFalseTotal
if exists $Class::Data::Reloadable::ClassData->{$ima}{$attribute} 0 10 10
193 10 ▶ last if $owner;
Branch: 1 branch
BranchTrueFalseTotal
if $owner 0 10 10
194 }
195  
196 5 return $owner;
197 }
198  
199 =head1 AUTHOR
200  
201 David Baird, C<< <cpan@riverside-cms.co.uk> >>
202  
203 =head1 BUGS
204  
205 Please report any bugs or feature requests to
206 C<bug-class-data-separated@rt.cpan.org>, or through the web interface at
207 L<http://rt.cpan.org>. I will be notified, and then you'll automatically
208 be notified of progress on your bug as I make changes.
209  
210 =head1 DEBUGGING
211  
212 Set C<$Class::Data::Reloadable::DEBUG = 1> to get debugging output (via C<warn>) that
213 may be useful for debugging either this module, or classes that inherit from it.
214  
215 You may also want to dig around in C<$Class::Data::Reloadable::ClassData>, but
216 don't tell anyone I told you.
217  
218 =head1 COPYRIGHT & LICENSE
219  
220 Copyright 2004 David Baird, All Rights Reserved.
221  
222 This program is free software; you can redistribute it and/or modify it
223 under the same terms as Perl itself.
224  
225 =cut
226  
227 5 1; # End of Class::Data::Separated
↑ Summary