How can I extend Moose's automatic pragma exports?

后端 未结 4 1839
时光说笑
时光说笑 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:18

    Moose::Exporter will allow you to define a custom import method for a sugar class you're using. MooseX::POE used a version of this for years, but does so in what I consider a "hackish" fashion. Looking at the documentation for Moose::Exporter the following should be roughly what you're asking for

    package Modern::Moose;
    use Moose ();
    use Moose::Exporter;
    
    my ($import) = Moose::Exporter->build_import_methods(
        also => 'Moose',
        install => [qw(unimport init_meta)],
    );
    
    sub import { # borrowing from mortiz's answer for feature/mro
        feature->import( ':5.10' );
        mro::set_mro( scalar caller(), 'c3' );        
        goto &$import;
    }
    

    This can then be used like so

    package MyApp;
    use Modern::Moose;
    
    has greeting => (is => 'ro', default => 'Hello');
    sub run { say $_[0]->greeting } # 5.10 is enabled
    

提交回复
热议问题