如何扩展 Moose 的自动 pragma 导出? [英] How can I extend Moose's automatic pragma exports?

查看:49
本文介绍了如何扩展 Moose 的自动 pragma 导出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道 Moose 如何在导入过程中自动打开 strictwarnings 吗?我想通过在我的 Moose 类中打开 autodieuse feature ':5.10' 来扩展这种行为.

You know how Moose automatically turns on strict and warnings during import? I want to extend that behavior by turning on autodie and use feature ':5.10' in my Moose classes.

我在 Moose::Exporter 中找到了 Moose 在哪里执行此操作,它为 Moose 组装了一个自定义的 import 子函数,调用 strict->importwarnings->import 用于调用类.

I've tracked down where Moose does this, in Moose::Exporter, which assembles a custom import sub for Moose that calls strict->import and warnings->import for the calling class.

但是,我想不出一种方法来以类似 Moose 的方式扩展此导入方法.

However, I can't figure out a way to extend this import method in a Moose-ish way.

我该如何处理?

http://www.friedo.com/bullwinkle.gif

推荐答案

由于模块可以通过多种方式将其函数导出到 use-ing 命名空间中,因此您可能需要进行一些代码挖掘为了实现每个所需的库.您要求的不是 Moose 特有的任何内容,因此您可以编写您或您公司自己的最佳实践模块,该模块将为您设置一组标准供您使用,例如

Since there are many ways a module might export its functions into the use-ing namespace, you may need to do some code digging in order to implement each desired library. What you're asking for isn't anything specific to Moose, so you can write your or your company's own best practices module which will set up a group of standards for you to work with, e.g.

use OurCompany::BestPractices::V1;

package OurCompany::BestPractices::V1;

use strict;
use warnings;
use feature (':5.10');
require Fatal;
require Moose;

# Required for straight implementation of autodie code
our @ISA;
push @ISA, qw(
   Fatal
);

sub import {
   my $caller = caller;
   strict->import;
   warnings->import;
   feature->import( ':5.10' );
   Moose->import ({into => $caller});

   #autodie implementation copied from autodie source
   splice(@_,1,0,Fatal::LEXICAL_TAG);
   goto &Fatal::import;
}

1;

Autodie 使事情变得稍微复杂一些,因为它依赖于从 caller() 中查找用户的包并使用 goto,但是您可以通过更多的测试找到更好的方法.您实施的越多,这个库可能就越复杂,但它可能具有足够高的价值,让您拥有可以在您或您公司的所有代码中使用的一次性解决方案.

Autodie makes things a little more complicated since it relies on finding the use-er's package from caller() and uses the goto, but you may be able to find a better way with more testing. The more you implement, the more complicated this library might be, but it might be of high enough value for you to have the one-off solution that you can use within all you or your company's code.

这篇关于如何扩展 Moose 的自动 pragma 导出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆