In both Python and Java we have import
to eliminate the repetition of fully-qualified package/module names throughout code. Is there any equivalent in Perl/Moose? I think it would really make Moose nicer to use if we didn't have to repeat MyApp::Model::Item
. Instead, I'd like to [somehow declare] MyApp::Model::Item;
and later on, simply refer to Item
. I can think of all of these use-cases where class names are used...
extends 'Item';
with 'ItemRole';
Item->new(name => 'thing');
method foo(Item $xyz) { ... }
, withMooseX::Method::Signatures
$var->isa('Item');
try { .开发者_JAVA百科.. } catch (DatabaseError $e) { ... }
, withTryCatch
$Item::SOME_PACKAGE_GLOBAL_VARIABLE
If there is no such thing yet, any idea on how I might start to cleanly implement this? I can see that it would be tricky to deal with cases where the classname is used as a string.
This all works with 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) { ... }
This doesn't:
$Item::SOME_PACKAGE_GLOBAL_VAR
Needing something like that seems to be quite rare, but I suppose it could be made to work with the namespace::alias
module.
精彩评论