I
Irving Kimura
It would be useful to have a generic, base class with a little bit
more functionality than UNIVERSAL. The purpose of this class would
be to eliminate tedious repetition when defining the constructor
and accessor methods of a generic hash-ref object. It would let
users specify what are the required and optional parameters for
the constructor (along with the default values for the optional
parameters), and would set up accessor methods (through AUTOLOAD)
for these parameters, and maybe others. These methods could be
overridden when the default behavior is inappropriate. So, defining
a new class could be as simple as something like
package Book;
use base Vanilla;
use constant required_params => 'title';
use constant optional_params => { author => [ anonymous ],
edition => 1,
isbn => '',
year => '',
publisher => '' };
use constant extra_accessors => 'id';
and one could immediately begin using this object like this:
use Book;
my $book_1 = Book->new('JR', { author => ['Gaddis, William'] });
my $book_2 = Book->new({ year => 2000 }); # triggers exception
print $book_1->author; # prints 'Gaddis, William'
print $book_1->edition(9); # prints '1' (previous value)
$book_1->city('New York'); # triggers exception
....etc. I haven't thought through all the facilities this vanilla
class would have, but something along these lines would be a nice
start.
Before I go off and reinvent the wheel, I thought that I'd ask if
anything like this already exists.
Thanks,
Irv
more functionality than UNIVERSAL. The purpose of this class would
be to eliminate tedious repetition when defining the constructor
and accessor methods of a generic hash-ref object. It would let
users specify what are the required and optional parameters for
the constructor (along with the default values for the optional
parameters), and would set up accessor methods (through AUTOLOAD)
for these parameters, and maybe others. These methods could be
overridden when the default behavior is inappropriate. So, defining
a new class could be as simple as something like
package Book;
use base Vanilla;
use constant required_params => 'title';
use constant optional_params => { author => [ anonymous ],
edition => 1,
isbn => '',
year => '',
publisher => '' };
use constant extra_accessors => 'id';
and one could immediately begin using this object like this:
use Book;
my $book_1 = Book->new('JR', { author => ['Gaddis, William'] });
my $book_2 = Book->new({ year => 2000 }); # triggers exception
print $book_1->author; # prints 'Gaddis, William'
print $book_1->edition(9); # prints '1' (previous value)
$book_1->city('New York'); # triggers exception
....etc. I haven't thought through all the facilities this vanilla
class would have, but something along these lines would be a nice
start.
Before I go off and reinvent the wheel, I thought that I'd ask if
anything like this already exists.
Thanks,
Irv