perl – 如何“使用”多个模块与一个“使用”?

我想在我的所有程序中使用一些包和一些编译指示,如:
use 5.014;
use warnings;
use autodie;
use My::ModuleA::Something;
use ModuleB qw(Func1 Func2);

我不想在每个模块中重复自己,所以寻找一个如何制作一个包,例如我的::常见的将包含上述包和在我的程序只做:

use My::Common;
say Func1("hello"); #say enabled and Func1 imported in the My::Common

怎么实现呢?

这是使用preldoc -f和perldoc perlmodlib,所以我想我必须“有点”这样做,BEGIN加上require& import,但绝对不知道如何.

更新:我已经尝试了基本的东西.

与require – 我的prg.pl程序.

require 'mymods.pl';
$var = "hello";
croak "$var\n";

mymods.pl包含

use strict;
use feature 'say';
use Carp qw(carp croak cluck);
1;

不工作有错误

$perl prg.pl 
String found where operator expected at prg.pl line 3,near "croak "$var\n""
    (Do you need to predeclare croak?)
Syntax error at prg.pl line 3,near "croak "$var\n""
Execution of prg.pl aborted due to compilation errors.

与“使用我的”:

use My;
$var = "hello";
croak "$var\n";

我的My.pm

package My;
use strict;
use feature 'say';
use Carp qw(carp croak cluck);
1;

也不工作.有同样的错误.

任何工作的想法?

解决方法

我会去这样做的:
package My::Common;
use 5.14.0;
use strict;
use warnings;
use autodie;
use Carp qw(carp croak cluck);

sub import {
    my $caller = caller;
    feature->import(':5.14');
    # feature->import('say');
    strict->import;
    warnings->import;
    ## autodie->import; # <-- Won't affect the caller side - see my edit.
    {
        no strict 'refs';
        for my $method (qw/carp croak cluck/) {
            *{"$caller\::$method"} = __PACKAGE__->can($method);
        }
    }
}

1;

如果我错了,请更正我,否则有更好的方法.

编辑:

抱歉,我使用autodie->导入错误

这应该工作,但它假定您始终从主包中调用My :: Common:

package My::Common;
# ...
sub import {
    # ...
    strict->import;
    warnings->import;
    {
        package main;
        autodie->import;
    }
    # ...
}

所以,当然,添加一个使用autodie更安全和更简单;到每个脚本:

use My::Common;
use autodie;
# ...

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...