How can I extend Moose's automatic pragma exports?

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

    You have to define a sub called import in your package, and import all the other stuff there.

    An example from Modern::Perl (another policy module you might look at):

    use 5.010_000;
    
    use strict;
    use warnings;
    
    use mro     ();
    use feature ();
    
    sub import {
         warnings->import();
         strict->import();
         feature->import( ':5.10' );
         mro::set_mro( scalar caller(), 'c3' );
    }
    

    Update: Sorry, didn't read the question carefully enough.

    A good way to extend an existing import method is to write your own in a new package, and call Moose's import method from there. You can do that by subclassing, maybe you can even use Moose yourself for that ;-)

提交回复
热议问题