Class-AccessorMaker
view release on metacpan or search on metacpan
lib/Class/AccessorMaker.pm view on Meta::CPAN
The AccesorMaker takes in, at use-time, a hash-reference and an extra
keyword. It uses the keys of the hash-reference to create
accessor-methods in the name-space of the caller. The values that are
given to the keys are the default value of the accessor.
Class::AccessorMaker will create a constructor (called C<new()>) by
default. This constructor will be able to take that nice and shiny
hash-like structure as you can see in the first example.
If you want your constructor to run your objects C<init()> routine you
can specify the keyword "new_init". If you want to write your own
C<new()> routine you can use "no_new". Note that Class::AccessorMaker
expects your object to be a hash-reference.
=head1 BUT I WANT TO ...
test the value of my accessor or perhaps even slightly alter it...
Too bad, Class:AccessorMaker does not do this for you. You will have to
write your own accessor method. It is up to you how you write it, but
it would be wise to keep the objects data-structure, which is just a
plain hash if you let Class::AccessorMaker make the constructor.
But if you wish to use global-data for it, be my guest, not that I
advice it, cause it is generally a bad idea to use global data, and if
you use it, you do not even need accessor methods, right?
=head2 an example:
sub seperator {
my ($self, $value) = @_;
# set the default value.
$self->{seperator} = '$$' if !exists $self->{seperator}
if ( $#_ > 0 ) {
$self->{seperator} = quotemeta($value);
return $self;
}
return $self->{seperator};
}
I think I can skip a code explanation, right?
=head1 NOW HERE THIS
This module is still under some sort of development, and I am
expecting to release things like ReadOnly / WriteOnce accessors methods
in the near future. I already have Class::AccessorMaker::Private out
there for you, which could prove to be very useful for you.
=head1 PITFALLS
Please do not put those perl-reserved names in there like DESTROY,
import, AUTOLOAD, and so on. It will hurt you badly.
Q: "But why do you not filter those?"
A: "This is perl baby, you can do whatever you like..."
And besides, there is going to be someone out there who is actualy
going to put it to good use...
=head1 AUTHOR
Hartog 'Sinister' de Mik <hartog@2organize.com>
=head1 COPYRIGHT
Copyright (c) 2002 Hartog C. de Mik. All rights reserved. This
program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included
with this module.
=cut
( run in 2.550 seconds using v1.01-cache-2.11-cpan-5837b0d9d2c )