|
Class::ObjectTemplate:DB - Perl extension for an optimized template builder base class with lookup capability. |
Class::ObjectTemplate:DB - Perl extension for an optimized template builder base class with lookup capability.
package Foo; use Class::ObjectTemplate::DB; require Exporter; @ISA = qw(Class::ObjectTemplate:DB Exporter);
attributes(lookup => ['one', 'two'], no_lookup => ['three']);
$foo = Foo->new();
# these two invocations can trigger lookup $val = $foo->one(); $val = $foo->two();
# this invocation will not trigger lookup $val = $foo->three();
# undefined() handles lookup
sub Foo::undefined {
my ($self,$attr) = @_;
# we retrieve $attr from DB
return DB_Lookup($self,$attr);
}
Class::ObjectTemplate::DB extends Class::ObjectTemplate in one simple
way: the undefined() method.
When a class that inherits from Class::ObjectTemplate::DB defines a
method called undefined(), that method will be triggered when an
attribute\'s getter method is invoked and the attribute\'s current
value is undef.
The author finds this useful when representing classes based on objects stored in databases (hence the name of the module). That way an object can be created, without triggering a DB lookup. Later if data is accessed and it is not currently present in the object, it can be retrieved on an as-need basis.
attributes() still supports the standard Class::ObjectTemplate
syntax of a list of attribute names.
To use the new functionality, the new key-value syntax must be
used. Any method names specified in the lookup array, will trigger
undefined. Those specified in the no_lookup will not trigger
undefined().
undefined() in order to utilize the lookup behavior.
Whenever an attribute\'s getter method is invoked, and that
attribute\'s value is currently undef, then undefined() will be
invoked if that attribute was defined as in the lookup array when
attributes() was called.
A class\'s undefined() method can include any specialized code
needed to lookup the value for that objects\'s attribute, such as
using DBI to connect to a local DB, and retrieve the value from a
table.
Class::ObjectTemplate::DB defines a default undefined() which does
nothing.
attributes()
Jason E. Stewart (jason@openinformatics.com)
perl(1).
|
Class::ObjectTemplate:DB - Perl extension for an optimized template builder base class with lookup capability. |