File Coverage

blib/lib/Class/AutoAccess.pm
Criterion Covered Total %
statement 25 26 96.1
branch 4 6 66.6
condition n/a
subroutine 6 6 100.0
pod n/a
total 35 38 92.1


line stmt bran cond sub pod time code
1             package Class::AutoAccess;
2              
3 2     2   41878 use warnings;
  2         5  
  2         59  
4 2     2   9 use strict ;
  2         3  
  2         105  
5              
6             =head1 NAME
7              
8             Class::AutoAccess - Zero code dynamic accessors implementation.
9              
10             =head1 VERSION
11              
12             Version 0.03
13              
14             =cut
15              
16             our $VERSION = '0.03';
17              
18             =head1 DESCRIPTION
19              
20             Base class for automated accessors implementation.
21              
22             If you implement a class as a blessed hash reference, this class helps you not
23             to write the fields accessors yourself. It uses the AUTOLOAD method to implement accessors
24             on demand. Since the accessor is *REALLY* implemented the first time it is attempted to be used,
25             using this class does NOT affect the performance of your program.
26              
27             Inheriting from this class does not impose accessors. If you want to implement your own accessors for any reason
28             (checking, implementation change ... ), just write them and they will be used in place of automated ones.
29              
30              
31             This class uses the AUTOLOAD method, so be careful when you
32             implement your own AUTOLOAD method in subclasses.
33              
34             If you want to keep this feature functionnal in this particular case,
35             evaluate SUPER::AUTOLOAD in your own AUTOLOAD method before doing anything else.
36              
37              
38             =head1 SYNOPSIS
39              
40             package Foo ;
41              
42             use base qw/Class::AutoAccess/ ; # Just write this
43              
44             sub new{
45             my ($class) = @_ ;
46             my $self = {
47             'bar' => undef ,
48             'baz' => undef ,
49             'toCheck' => undef
50             };
51             return bless $self, $class ;
52             }
53              
54             sub toCheck{
55             my ($self , $value ) = @_ ;
56             # Behave the way you want. This accessor will be used in place of automated ones.
57             }
58              
59             1;
60              
61             package main ;
62              
63             my $o = Foo->new();
64            
65             # Since there is a bar attribute, the accessor will be implemented at the first use:
66             $o->bar();
67             # This time, the bar accessor is really implemented so there is no performance lost.
68             $o->bar("new value");
69              
70             # Idem.
71             $o->baz() ;
72            
73             # If you wrote your own accessor, it will be used (this is a Perl feature)
74             $o->toCheck("value");
75              
76             =head1 AUTHOR
77              
78             Jerome Eteve, C<< >>
79              
80             =head1 BUGS
81              
82             None known.
83              
84             Please report any bugs or feature requests to
85             C, or through the web interface at
86             L.
87             I will be notified, and then you'll automatically be notified of progress on
88             your bug as I make changes.
89              
90             =head1 ACKNOWLEDGEMENTS
91              
92             =head1 COPYRIGHT & LICENSE
93              
94             Copyright 2005-2010 Jerome Eteve, all rights reserved.
95              
96             This program is free software; you can redistribute it and/or modify it
97             under the same terms as Perl itself.
98              
99             =cut
100              
101 2     2   8 use Carp ;
  2         14  
  2         456  
102              
103             our $AUTOLOAD ;
104              
105              
106             sub AUTOLOAD{
107 2     2   38 my ($self,$value)= @_ ;
108            
109             # $AUTOLOAD contains the full name of the missing method.
110              
111             # Avoid implicit ovverriding of destroy method.
112 2 50       7 return if $AUTOLOAD =~ /::DESTROY$/ ;
113              
114 2         4 my $attname = $AUTOLOAD;
115             # Removing packagename from the attname.
116 2         11 $attname =~ s/.*::// ;
117              
118 2 50       9 if(! exists $self->{$attname}){
119 0         0 confess("Attribute $attname does not exists in $self");
120             }
121              
122             # If attribute exists, got to set up the method
123             # in order to avoid calling this everytime
124              
125 2         4 my $pkg = ref($self ) ;
126             my $methCode = sub{
127 1004     1004   2479 my $obj = shift ;
128 1004 100       2198 @_ ? $obj->{$attname} = shift : $obj->{$attname} ;
129 2         11 };
130            
131             ## Install method as $pkg::$attname
132             {
133 2     2   10 no strict 'refs' ;
  2         4  
  2         119  
  2         4  
134 2         1 *{$pkg.'::'.$attname} = $methCode ;
  2         8  
135             }
136            
137             # Let's use our shiny new method
138 2         9 goto &$AUTOLOAD ;
139            
140             }
141              
142             1; # End of Class::AutoAccess