Help - Search - Member List - Calendar
Full Version: mixin object method
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Todd W
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
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


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.