Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Class::Classless vs. Class::Prototyped

by bsb (Priest)
on Sep 12, 2003 at 08:32 UTC ( [id://290958]=note: print w/replies, xml ) Need Help??


in reply to Class::Classless vs. Class::Prototyped

I'd be interested to know more about your project and why you're considering going down that route. (See: When to use prototype inheritence?). I'm also interested in your experiences after the project.

There's also Class::SelfMethods by the same guy who wrote Class::Prototyped (Toby Everret). It's worth checking out as it had input from Damian Conway and Sean Burke. (Gleaned from the docs).

Unfortunately, I haven't got much practical experience with the modules so can't give you much advice. I'm considering emailing these authors to find out how the modules are being used and what the experience have been. I'll report back if I do.

I have my own, minimal version which is still being designed and debugged and may be ditched for a module implementation
The important bits follow, just 'new' and an AUTOLOAD accessor.

sub AUTOLOAD { # attrs my ($self) = @_; (my $attr = $AUTOLOAD) =~ s/^.*:://; return if $attr eq 'DESTROY'; return $self->{$attr} if(exists $self->{$attr}); return $self->{prototype}->$attr if(exists $self->{prototype}); } =item new Create a new Slot with prototyping. # class method my $proto = SD::Slot->new({ min_val => 1, max_val => 10 }); # $slot is blessed as an SD::Slot and has prototype = $proto my $slot = $proto->new({ min_val => 0 }); # $slot2 isa SD::Slot::Multi but still has prototype = $proto my $slot2 = SD::Slot::Multi->new({ prototype => $proto }); The parameter hashref is blessed into the appropriate class and returned. =cut sub new { my ($class, $proto, $self); # @_ processed conditionally if($class = ref $_[0]) { $proto = shift; # we have a prototype object } else { $class = shift; } $self = shift || {}; if(!exists $self->{prototype} && $proto) { $self->{prototype} = $proto; } bless $self => $class; } # example use (config) my %prototypes; $prototypes{int} = SD::Slot->new({ widget => 'textfield', constraints => [ \&SD::Constraints::integer_rx, \&SD::Constraints::min_val, \&SD::Constraints::max_val, ], }); $prototypes{int1_10} = $prototypes{int}->new({ prototype => $prototypes{int}, skip_warn => 1, min_val => 1, max_val => 10, }); my %slots; $slots{intelligence} = $prototypes{int1_10}->new({ name => 'intelligence', mandatory => 1, constraints => [ # add extra constraint @{ $prototypes{int}->constraints }, sub { $_[1] % 2 or die E->new( "Not an odd number '$_[1]'") }, ], });

Brad

Replies are listed 'Best First'.
Re: Re: Class::Classless vs. Class::Prototyped
by John M. Dlugosz (Monsignor) on Sep 12, 2003 at 16:33 UTC
    Well, I have a number of different record types and modifier flags (http://www.dlugosz.com/ZIP2/structure.html) and I want to model them as Perl objects. They will encode/decode, as well as provide functionality specific to that record type. Notice on that page I have real binary hex-dump examples, as I wrote a simple thing initially. But last I left it, I was working on a more elaborate system of populating both values and behavior based on the initial "type" when new-ing it. On a particular species, for example, a "set" method could check for contraints based on the rules of that specific situation.

    The implementation concept was to have "slots" that could be values or code. That's starting to sound like what these modules do. So why re-invent it?

    —John

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://290958]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 09:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found