Todd W
Jul 13 2005, 11:23 PM
I've been trying to undersand what mixins are and at the same time figuring
out how to make them easy to use. I just belched out this piece of code, but
I dont know if its doing anything special:
use warnings;
use strict;
package MyMixins;
sub SomeMethod {
my $obj = shift;
print( 'a ', ref( $obj ), ' goes: ', $obj->{slot}, "n" );
}
package MyClient;
sub new {
my $class = shift;
my $self = bless( { }, $class );
$self->{slot} = shift || 'moo';
return $self;
}
package main;
my $client = MyClient->new;
$client->MyMixins::SomeMethod;
print("done!n");
C:waverighthometrwwwmisc>perl mixintest.pl
a MyClient goes: moo
done!
Jeff 'japhy' Pinyan
Jul 14 2005, 08:20 AM
On Jul 13, Todd W said:
QUOTE |
I've been trying to undersand what mixins are and at the same time figuring out how to make them easy to use. I just belched out this piece of code, but I dont know if its doing anything special:
|
Perl allows you to call an object from any class to call a method from any
class. Typically, $object->method searches through $object's class and
inheritance tree. If you give a fully-qualified method name, such as
OtherClass::method, then Perl calls that very specific method, which is
what you've done below.
QUOTE |
package MyMixins;
sub SomeMethod {...}
package MyClient;
sub new {...}
package main;
my $client = MyClient->new; $client->MyMixins::SomeMethod;
|
Yeah, that looks about right. Thrilling, wasn't it?
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart