in reply to Re: Idea on a Base class API
in thread Idea on a Base class API

That's why you would have to do a Foo->define_attributes() first.

I should've given an example.

package Foo; use BaseClass; our @ISA = qw(BaseClass); Foo->define_attributes( name => 'STRING', age => 'NUM', weight => 'NUM', ); sub initialize { my $self = shift; $self->SUPER::initialize(@_); my ($name, $age) = $self->get('name', 'age'); print "My name is $name and I am $age years old!\n"; return 1; } 1; _____ package Bar; use Foo; my $person = Foo->new(name => 'Child1', age => 5); $person->set(weight => 40);

Replies are listed 'Best First'.
Re: Re{2}: Idea on a Base class API
by Masem (Monsignor) on Jun 13, 2001 at 01:39 UTC
    I think the question is begging for the ability to set more complex data typing that just the 7 that perl has to offer. For example, if I only want data items of a certain class, or that derive from a given class. Or if I only want to include positive numbers, instead of negatives.

    In addition, you need to consider read-only, write-only, and completely private variables. While the latter can be hidden elsewhere, you'd need to have a way to define this.

    Both are possible to include in the setup you have. The read-write-edness of the variable can be done by using arrays as the values for the attributes hash, using the second element of the array as "r", "rw", "w", or undef/0 for private variables.

    The former option, I would suggest that the 'type' can be several things. If a scalar, it can either be the 7 defined perl types, or if not one of those, then a possible class name (such that the argument can be checked by an ISA relationship. If an array, then the arguement can be any one of the types or ISAs in that array. Finally, if the passed argument is a coderef, then when setting, the code should be checked to see if what is passed matches the code, else you lead to failure.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      Continuing on the LISP-ish way of defining things, why not allow arbitrary constraints via a code block, not just an ISA type? If argument is a string, checks ISA. If code block, uses it as a checking predicate.

      —John