package Foo;
use strict;
use warnings;
use Exporter;
our @ISA= qw( Exporter );
# these CAN be exported.
our @EXPORT_OK = qw( export_me export_me_too );
# these are exported by default.
our @EXPORT = qw( export_me );
sub export_me {
# stuff
}
sub export_me_too {
# stuff
}
1;
在你的主程序中:
use strict;
use warnings;
use Foo; # import default list of items.
export_me( 1 );
或者得到两个函数:
use strict;
use warnings;
use Foo qw( export_me export_me_too ); # import listed items
export_me( 1 );
export_me_too( 1 );
我知道这个问题特别提到了“函数”,但是当我在搜索“ perl include”时,我会把这篇文章放在很高的位置,而且通常(比如现在)我想要包含变量(用一种简单的方式,不需要考虑模块)。因此,我希望在这里发布我的示例没有问题(另见: Perl request 和变量; 简而言之: 使用 require,并确保“ include er”和“ include dee”文件都将变量声明为 our) :
$ perl --version
This is perl, v5.10.1 (*) built for i686-linux-gnu-thread-multi ...
$ cat inc.pl
use warnings;
use strict;
our $xxx = "Testing";
1;
$ cat testA.pl
use warnings;
use strict;
require "inc.pl";
our $xxx;
print "1-$xxx-\n";
print "Done\n";
$ perl testA.pl
1-Testing-
Done
$ cat testB.pl
use warnings;
use strict;
our $xxx;
print "1-$xxx-\n";
$xxx="Z";
print "2-$xxx-\n";
require "inc.pl";
print "3-$xxx-\n";
print "Done\n";
$ perl testB.pl
Use of uninitialized value $xxx in concatenation (.) or string at testB.pl line 5.
1--
2-Z-
3-Testing-
Done