开发者

Shorthand for referring to Perl/Moose package names?

开发者 https://www.devze.com 2023-01-15 10:32 出处:网络
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 ni

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) { ... }, with MooseX::Method::Signatures
  • $var->isa('Item');
  • try { .开发者_JAVA百科.. } catch (DatabaseError $e) { ... }, with TryCatch
  • $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.

0

精彩评论

暂无评论...
验证码 换一张
取 消