File Coverage

blib/lib/Class/Accessor/Fast/Contained.pm
Criterion Covered Total %
statement 39 39 100.0
branch 15 22 68.1
condition n/a
subroutine 11 11 100.0
pod 4 4 100.0
total 69 76 90.7


line stmt bran cond sub pod time code
1             package Class::Accessor::Fast::Contained;
2              
3 6     6   13045 use strict;
  6         13  
  6         259  
4 6     6   31 use warnings FATAL => qw(all);
  6         11  
  6         270  
5              
6 6     6   51 use base qw(Class::Accessor::Fast);
  6         11  
  6         5408  
7              
8             our $VERSION = '1.01';
9             $VERSION = eval $VERSION; # numify for warning-free dev releases
10              
11 6     6   27590 use Symbol;
  6         5769  
  6         3449  
12              
13             # this module does two things differently to the venerable
14             # Class::Accessor::Fast,
15             # 1) fields are stored at arms-length in a single key of $self
16             # 2) new() allows mixin into an existing object
17              
18             sub new {
19 7     7 1 5452 my ($class, $fields) = @_;
20              
21 7 100       36 $fields = {} unless defined $fields;
22              
23 7 50       38 my $self = (ref $class ? $class : bless {}, $class);
24              
25 7 50       78 my $copy = ("$self" =~ m/=GLOB/ ? *$self : $self);
26 7         53 $copy->{ref $self} = {%$fields};
27              
28 7         24 return $self;
29             }
30              
31             *{Symbol::qualify_to_ref('setup')} = \&new;
32              
33             sub make_accessor {
34 11     11 1 2583 my($class, $field) = @_;
35              
36             return sub {
37 14     14   1432 my $self = shift;
38 14 50       58 my $copy = ("$self" =~ m/=GLOB/ ? *$self : $self);
39 14 100       195 return $copy->{ref $self}->{$field} if scalar @_ == 0;
40 5 50       24 $copy->{ref $self}->{$field} = (@_ == 1 ? $_[0] : [@_]);
41 11         56 };
42             }
43              
44              
45             sub make_ro_accessor {
46 9     9 1 1292 my($class, $field) = @_;
47              
48             return sub {
49 7     7   22 my $self = shift;
50 7 50       29 my $copy = ("$self" =~ m/=GLOB/ ? *$self : $self);
51 7 100       46 return $copy->{ref $self}->{$field} if scalar @_ == 0;
52 2         5 my $caller = caller;
53 2         25 $self->_croak("'$caller' cannot alter the value of '$field' on objects of class '$class'");
54 9         43 };
55             }
56              
57             sub make_wo_accessor {
58 9     9 1 919 my($class, $field) = @_;
59              
60             return sub {
61 5     5   2180 my $self = shift;
62 5 50       26 my $copy = ("$self" =~ m/=GLOB/ ? *$self : $self);
63              
64 5 100       18 unless (@_) {
65 2         3 my $caller = caller;
66 2         16 $self->_croak("'$caller' cannot access the value of '$field' on objects of class '$class'");
67             }
68             else {
69 3 50       19 return $copy->{ref $self}->{$field} = (@_ == 1 ? $_[0] : [@_]);
70             }
71 9         45 };
72             }
73              
74             =head1 NAME
75              
76             Class::Accessor::Fast::Contained - Fast accessors with data containment
77              
78             =head1 VERSION
79              
80             This document refers to version 1.01 of Class::Accessor::Fast::Contained
81              
82             =head1 SYNOPSIS
83              
84             package Foo;
85             use base qw(Class::Accessor::Fast::Contained);
86              
87             # The rest is the same as Class::Accessor::Fast
88              
89             =head1 DESCRIPTION
90              
91             This module does two things differently to the venerable Class::Accessor::Fast :
92              
93             =over 4
94              
95             =item *
96              
97             Fields are stored at arms-length within a single hash value of $self, rather
98             than directly in the $self blessed referent.
99              
100             =item *
101              
102             C allows mixin into an existing object, rather than creating and
103             returning a new blessed hashref. To do this, just call something like:
104              
105             my $self = Some::Other::Class->new;
106             $self = $self->Class::Accessor::Fast::Contained::new;
107              
108             Note that the mixin code only supports objects which use a blessed hash
109             reference or a blessed typeglob reference.
110              
111             An alias C is available which does the same as C but might
112             make more sense if being used in this way.
113              
114             =back
115              
116             =head1 DEPENDENCIES
117              
118             Other than the standard Perl distribution, you will need the following:
119              
120             =over 4
121              
122             =item *
123              
124             Class::Accessor
125              
126             =back
127              
128             =head1 BUGS
129              
130             If you spot a bug or are experiencing difficulties that are not explained
131             within the documentation, please send an email to oliver@cpan.org or submit a
132             bug to the RT system (http://rt.cpan.org/). It would help greatly if you are
133             able to pinpoint problems or even supply a patch.
134              
135             =head1 SEE ALSO
136              
137             L
138              
139             =head1 AUTHOR
140              
141             Oliver Gorwits C<< >>
142              
143             =head1 ACKNOWLEDGEMENTS
144              
145             Thanks to Marty Pauly and Michael G Schwern for L and its
146             tests, which I've shamelessly borrowed for this distribution.
147              
148             =head1 COPYRIGHT & LICENSE
149              
150             Copyright (c) The University of Oxford 2008.
151              
152             This library is free software; you can redistribute it and/or modify it under
153             the same terms as Perl itself.
154              
155             =cut
156              
157             1;
158