perl - 引用 Perl/Moose 包名称的简写?

标签 perl coding-style packages moose verbosity

在 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

  • 如果还没有这样的事情,关于我如何开始干净地实现它的任何想法?我可以看到处理类名用作字符串的情况会很棘手。

    最佳答案

    这一切都适用于 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 一起使用。模块。

    关于perl - 引用 Perl/Moose 包名称的简写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3647826/

    相关文章:

    HTML 调用 Perl Perl 调用脚本 脚本 SSH 不起作用

    c# - OnDataBinding 与 Inline : pros, 缺点和开销

    .net - Visual Studio DTE2 : how to get text content of the current document

    r - 在包开发中处理数据集依赖的正确方法?

    perl - 在 Perl 中从括号中获取键值对

    perl - 如何从 hashref 中的文件句柄中读取一行

    perl - 如何使用 Perl 获取 DOS 工具的命令行输出?

    Python:如何提高脚本的性能以获得一组点的边界框

    c++ - Eclipse - C++ 代码风格和标准定制和自动化

    R 使用 mingw_32 为 64 位架构编译包