在Python和Java中,我们都有import
来消除代码中完全限定的包/模块名称的重复。在Perl / Moose中有没有相应的东西?我认为如果我们不必重复MyApp::Model::Item
,那么真的会让Moose更好用。相反,我想[somehow declare] MyApp::Model::Item;
以及之后,只需参考Item
。我可以想到所有使用类名的用例......
extends 'Item';
with 'ItemRole';
Item->new(name => 'thing');
method foo(Item $xyz) { ... }
,MooseX::Method::Signatures
$var->isa('Item');
try { ... } catch (DatabaseError $e) { ... }
,TryCatch
$Item::SOME_PACKAGE_GLOBAL_VARIABLE
如果还没有这样的事情,有什么想法我可能会开始干净地实现这个吗?我可以看到处理classname用作字符串的情况会很棘手。
答案 0 :(得分:18)
这一切都适用于aliased
use aliased 'MyApp::Model::Item';
use aliased 'MyApp::ItemRole';
use aliased 'MyApp::Exception::DatabaseError';
extends Item;
with ItemRole;
Item->new(name => 'thing');
method foo (Item $xyz) { ... }
$var->isa(Item);
try { ... } catch(DatabaseError $e) { ... }
这不是:
$Item::SOME_PACKAGE_GLOBAL_VAR
需要这样的东西似乎很少见,但我想可以使用namespace::alias
模块。