Subscribed unsubscribe Subscribe Subscribe

Kentaro Kuribayashi's blog

Software Engineering, Management, Books, and Daily Journal.

Class::Extendable As One of Prerequisites for DCI

I want perl to have easy-to-use "singleton method"-like feature to help us making some code to prove DCI concept in Perl. Of course I know there have been some solutions for it, but I just want to write like $obj->extend("Foo", "Bar") same as Ruby. Then, I hacked up a module, whose codes were mainly borrowed from dankogai-san's blog entry, though ;)

It's simple enough to start quickly. Just use the module:

package My::Foo;
use Class::Extendable;
sub new { bless {}, shift }

package My::Bar;
sub bar {}

package main;
my $obj1 = My::Foo->new;
my $obj2 = My::Foo->new;

ok !$obj1->can('bar');
ok !$obj2->can('bar');

$obj1->extend('My::Bar');

# Now that `$obj1` extended, it can receive all the methods in `My::Bar`
ok $obj1->can('bar');
ok !$obj2->can('bar');

I'm not planning upload the module to CPAN, though.