views:

106

answers:

1

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 { ... } 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.

+17  A: 

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.

rafl
Fantastic! Thanks for the quick answer!
David
and yeah, I rarely need to access a package global, so I can live with spelling out the name in those cases.
David