Tuesday, April 7, 2009

Class::Implant - No &import() !

Class::implant is a experimental little helper implemented when I developed Railsish with gugod. Manipulating mixin and inheritance outside package is its primary function, also with abilities to select methods which would import.

Original idea is inspire by the purpose: we want to import whole Helper methods into Controller which the Helper is named after. And it is should not be left to framework users to do it by themself. In Ruby, we just need to write
XxxController.send(:include, XxxHelper)
In Perl, it can also be written as
eval qq{ package XxxController; use XxxHelper; }
But a tedious &import() should be write in XxxHelper as follow.
sub import {
for (qw(hello world foo bar method...)) {
*{blah::$_} = \&$_
}
}
and edit the export methods list by hand.

Even use Exporter and edit @EXPORT and export by symbol :all, users still need to do follow by hand
use base Exporter;
our @EXPORT = qw(..);
Class::Implant provide simplified equivalent.

package main;
use Class::Implant;
implant "XxxHelper", { into => "XxxController" };

Everything done! All methods in XxxHelper is imported into XxxController!

If call use Class::Implant in other package, default import target would become it. That means, above code is the same as follow:

package XxxController;
use Class::Implant;
implant "XxxHelper";

In the release 0.02_01, two other options work. { inherit => 1 } make imported packages appear in @ISA of import target. { match => pattern } filter, and import methods conform to pattern.

For example,

package main;
use Class::Implant;
implant qw(Foo Bar), { into => "Cat", match => qr{h\w+} };

means import methods whose name start with h in Foo and Bar into Cat.

I also write UNIVERSAL::Implant. As its name, require once, and write as follow everywhere.

Cat->implant qw(Foo Bar), { match => qr{h\w+} }

Do the same thing as previous example. That means, caller package Cat assign { into => "Cat" }.



Chinese Version is @ Chupei.pm.org

 

No comments: