Class-Lite
view release on metacpan or search on metacpan
lib/Class/Lite.pm view on Meta::CPAN
qq* push \@ISA, '$bridge'; *,
;
# This second eval fails in case recursive inheritance is attempted.
die "Failed to generate $caller: $@" if $@;
# In case caller must get the last word.
$class->rear_import(@_);
return 1;
}; ## import
# Dummy methods do nothing.
sub fore_import { shift; return @_ };
sub rear_import { shift; return @_ };
## END MODULE
1;
#============================================================================#
__END__
=head1 NAME
Class::Lite - Simple base class with get/put accessors
=head1 VERSION
This document describes Class::Lite version v0.1.0
=head1 SYNOPSIS
package Toy::Class;
use Class::Lite qw| foo bar baz |; # make get/put accessors
package Any::Class;
use Toy::Class;
my $toy = Toy::Class->new;
$toy->init(@_); # does nothing; override
$toy->put_foo(42);
my $answer = $toy->get_foo;
use Class::Lite; # no accessors
=head1 DESCRIPTION
=over
I<< Nature's great masterpiece, an elephant,
The only harmless great thing. >>
-- John Donne
=back
The hashref-based base class that does no more than it must. Your
constructor and accessors are defined in a bridge package so you can
override them easily.
=head1 Why?
Computer programmers are clever people who delight in evading restrictions.
Create an L<< inside-out|Class::Std >> (flyweight) class to enforce
encapsulation and another fellow will L<< hack in|PadWalker >>. The only
way to win the ancient game of locksmith and lockpick is never to begin.
If someone misuses your class then it's not your responsibility.
Hashref-based objects are traditional, well-understood, even expected in
the Perl world; tools exist with which to work with them.
Similarly, C<< Class::Lite >> provides no read-only accessors. If your client
developer wants to alter an attribute he will; you may as well provide a
method for the purpose. You might warn against the practice by overriding
the default method:
sub put_foo {
warn q{Please don't write to the 'foo' attribute.};
my $self = shift;
return $self->SUPER::put_foo(@_);
};
B<< set >> is too similar to B<< get >> in one way, not enough in another.
Also B<< set >> is one of those heavily overloaded words, like "love" or
"data", that I prefer to avoid using at all. I say B<< put >> is equally
short, clearer in intent, not easily misread for B<< get >>; and the first
character's descender points in the opposite direction.
I eschew single-method C<< foo() >> accessors.
I have long defined C<< init() >> as a shortcut method to fill up a new
object; but this is a blatant violation of encapsulation, no matter who
does it. No more.
If accessors are defined in your calling package then you will raise a
warning if you attempt to redefine them; if they are defined in
C<< Class::Lite >> itself then they will be available to all that inherit
from it. So your accessors are defined in an intermediate "bridge" package
generated at compile-time.
=head1 USE-LINE
package Toy::Class;
use Class::Lite qw| foo bar baz |; # make get/put accessors
use Class::Lite; # no accessors
Makes C<< Class::Lite >> a base class for Toy::Class. If arguments are
given then simple get and put accessors will be created in caller's
namespace for each argument. The accessors do no validation.
B<< This is probably all you need to know. >> Read on if you intend to do tricky stuff in a superclass.
=head1 INHERITED METHODS
=head2 import()
Class::Lite->import(@_);
A::Subclass->import(@_);
Called by use() as usual and does all the work. Inherited by caller so
your further subclasses can also take advantage of C<< Class::Lite >>
features.
Since this is merely inherited you may define your own C<< import() >> with
impunity. If you want to have your cake and eat it, too, beware:
( run in 0.787 second using v1.01-cache-2.11-cpan-39bf76dae61 )