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 aseval qq{ package XxxController; use XxxHelper; }
But a tedious &import() should be write in XxxHelper as follow.and edit the export methods list by hand.sub import {
for (qw(hello world foo bar method...)) {
*{blah::$_} = \&$_
}
}
Even use Exporter and edit
@EXPORT
and export by symbol :all
, users still need to do follow by handClass::Implant provide simplified equivalent.use base Exporter;
our @EXPORT = qw(..);
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:
Post a Comment