How can I extend Moose's automatic pragma exports?

后端 未结 4 1824
时光说笑
时光说笑 2021-02-08 13:36

You know how Moose automatically turns on strict and warnings during import? I want to extend that behavior by turning on autodie and

4条回答
  •  无人及你
    2021-02-08 14:06

    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;
    

    with

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

提交回复
热议问题