| 1 | package Rose::ObjectX::CAF; | ||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use base qw( Rose::Object ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use Rose::ObjectX::CAF::MethodMaker; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 3 | our $VERSION = '0.03'; | |||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | Rose::ObjectX::CAF - Class::Accessor::Fast compatability for Rose::Object | ||||||||||
| 13 | |||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||
| 15 | |||||||||||
| 16 | package MyClass; | ||||||||||
| 17 | use strict; | ||||||||||
| 18 | use base qw( Rose::ObjectX::CAF ); | ||||||||||
| 19 | __PACKAGE__->mk_accessors(qw( foo bar )); | ||||||||||
| 20 | __PACKAGE__->mk_ro_accessors(qw( color name )); | ||||||||||
| 21 | 1; | ||||||||||
| 22 | |||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||
| 24 | |||||||||||
| 25 | Rose::ObjectX::CAF is a compatability layer for Class::Accessor::Fast users who | ||||||||||
| 26 | want to migrate to Rose::Object. | ||||||||||
| 27 | |||||||||||
| 28 | As evidenced in L<App::Benchmark::Accessors>, | ||||||||||
| 29 | L<Rose::Object> + L<Class::XSAccessor> is | ||||||||||
| 30 | much faster than Class::Accessor::Fast (and more extensible). | ||||||||||
| 31 | I decided to switch over, but had a lot of code already using CAF. | ||||||||||
| 32 | So this class was born to make the migration easier. | ||||||||||
| 33 | |||||||||||
| 34 | Just replace this line in your classes: | ||||||||||
| 35 | |||||||||||
| 36 | use base qw( Class::Accessor::Fast ); | ||||||||||
| 37 | |||||||||||
| 38 | with this: | ||||||||||
| 39 | |||||||||||
| 40 | use base qw( Rose::ObjectX::CAF ); | ||||||||||
| 41 | |||||||||||
| 42 | and no other changes should be necessary. | ||||||||||
| 43 | |||||||||||
| 44 | =head1 METHODS | ||||||||||
| 45 | |||||||||||
| 46 | =head2 new | ||||||||||
| 47 | |||||||||||
| 48 | Works like CAF, but may take a hash (the Rose::Object style) | ||||||||||
| 49 | or hash ref (the CAF style). | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||
| 52 | |||||||||||
| 53 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 54 | 2 | my $class = shift; | |||||||||
| 55 | 2 | my $self = bless {}, $class; | |||||||||
| 56 | 2 | ▶ | $self->init( @_ > 1 ? @_ : %{ $_[0] } ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 2 | return $self; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | =head2 init | ||||||||||
| 61 | |||||||||||
| 62 | Like Rose::Object, called by new(). Do not override new() in your | ||||||||||
| 63 | subclasses; override init() instead. And be sure to call: | ||||||||||
| 64 | |||||||||||
| 65 | $self->SUPER::init(@_); # or with MRO::Compat, $self->next::method(@_); | ||||||||||
| 66 | |||||||||||
| 67 | in your subclass. | ||||||||||
| 68 | |||||||||||
| 69 | Rather than calling | ||||||||||
| 70 | the method name for each param passed in new(), the value | ||||||||||
| 71 | is simply set in the object as a hash ref. This assumes | ||||||||||
| 72 | every object is a blessed hash ref. | ||||||||||
| 73 | |||||||||||
| 74 | The reason the hash is preferred over the method call | ||||||||||
| 75 | is to support read-only accessors, which will croak | ||||||||||
| 76 | if init() tried to set values with them. | ||||||||||
| 77 | |||||||||||
| 78 | =cut | ||||||||||
| 79 | |||||||||||
| 80 | 2 | ▶ | sub init { | ||||||||
|
Subroutine
init: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 81 | 2 | my $self = shift; | |||||||||
| 82 | |||||||||||
| 83 | # assume object is hash and set key | ||||||||||
| 84 | # rather than call method, since we have read-only methods. | ||||||||||
| 85 | 2 | ▶ | while (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 2 | my $method = shift; | |||||||||
| 87 | 2 | ▶ | if (!$self->can($method)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | croak "No such method $method"; | |||||||||
| 89 | } | ||||||||||
| 90 | 2 | $self->{$method} = shift; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 2 | return $self; | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | =head2 mk_accessors( I<@list_of_method_names> ) | ||||||||||
| 97 | |||||||||||
| 98 | Just like CAF. | ||||||||||
| 99 | |||||||||||
| 100 | =cut | ||||||||||
| 101 | |||||||||||
| 102 | 1 | ▶ | sub mk_accessors { | ||||||||
|
Subroutine
mk_accessors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 103 | 1 | my $class = shift; | |||||||||
| 104 | 1 | Rose::ObjectX::CAF::MethodMaker->make_methods( | |||||||||
| 105 | { target_class => $class, }, | ||||||||||
| 106 | scalar => \@_ ); | ||||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | =head2 mk_ro_accessors( I<@list_of_method_names> ); | ||||||||||
| 110 | |||||||||||
| 111 | Just like CAF, for read-only (accessor/getter) methods. | ||||||||||
| 112 | |||||||||||
| 113 | =cut | ||||||||||
| 114 | |||||||||||
| 115 | 1 | ▶ | sub mk_ro_accessors { | ||||||||
|
Subroutine
mk_ro_accessors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 116 | 1 | my $class = shift; | |||||||||
| 117 | 1 | Rose::ObjectX::CAF::MethodMaker->make_methods( | |||||||||
| 118 | { target_class => $class, }, | ||||||||||
| 119 | 'scalar --ro' => \@_ ); | ||||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | =head1 AUTHOR | ||||||||||
| 123 | |||||||||||
| 124 | Peter Karman, C<< <karman at cpan.org> >> | ||||||||||
| 125 | |||||||||||
| 126 | =head1 BUGS | ||||||||||
| 127 | |||||||||||
| 128 | Please report any bugs or feature requests to C<bug-rose-objectx-caf at rt.cpan.org>, or through | ||||||||||
| 129 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Rose-ObjectX-CAF>. I will be notified, and then you'll | ||||||||||
| 130 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 131 | |||||||||||
| 132 | |||||||||||
| 133 | =head1 SUPPORT | ||||||||||
| 134 | |||||||||||
| 135 | You can find documentation for this module with the perldoc command. | ||||||||||
| 136 | |||||||||||
| 137 | perldoc Rose::ObjectX::CAF | ||||||||||
| 138 | |||||||||||
| 139 | |||||||||||
| 140 | You can also look for information at: | ||||||||||
| 141 | |||||||||||
| 142 | =over 4 | ||||||||||
| 143 | |||||||||||
| 144 | =item * RT: CPAN's request tracker | ||||||||||
| 145 | |||||||||||
| 146 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Rose-ObjectX-CAF> | ||||||||||
| 147 | |||||||||||
| 148 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 149 | |||||||||||
| 150 | L<http://annocpan.org/dist/Rose-ObjectX-CAF> | ||||||||||
| 151 | |||||||||||
| 152 | =item * CPAN Ratings | ||||||||||
| 153 | |||||||||||
| 154 | L<http://cpanratings.perl.org/d/Rose-ObjectX-CAF> | ||||||||||
| 155 | |||||||||||
| 156 | =item * Search CPAN | ||||||||||
| 157 | |||||||||||
| 158 | L<http://search.cpan.org/dist/Rose-ObjectX-CAF/> | ||||||||||
| 159 | |||||||||||
| 160 | =back | ||||||||||
| 161 | |||||||||||
| 162 | |||||||||||
| 163 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 164 | |||||||||||
| 165 | |||||||||||
| 166 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 167 | |||||||||||
| 168 | Copyright 2009 Peter Karman. | ||||||||||
| 169 | |||||||||||
| 170 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 171 | under the terms of either: the GNU General Public License as published | ||||||||||
| 172 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 173 | |||||||||||
| 174 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 175 | |||||||||||
| 176 | |||||||||||
| 177 | =cut | ||||||||||
| 178 | |||||||||||
| 179 | 3 | 1; # End of Rose::ObjectX::CAF | |||||||||