我当前的Perl软件包导出感觉很长(下面的代码段)。是的,最好一次将所有内容键入一次,这样我的许多Perl脚本都可以通过以下方式访问所有内容:
use Funx;
仍然我只是希望有一种简单的方法来以更少的输入导出所有内容。
package Funx;
#use strict;
use warnings;
use DBI;
use Exporter;
our @ISA = 'Exporter';
our @EXPORT = qw(pdone dbstart dbstop dbc dbcdata numnums $SUCCESS $NOFILE
$COPYFAIL $SOXFAIL $CURLFAIL $OPENFAIL $APPRUNNING $RAWDBEXIISTS $DBCREATEERR $DBCONNECTERR $TMPFILEERR $DBWRITEERR $INVALIDUSER $DBLOCKERR $DBUNLOCKERR WERR);
our $SUCCESS = 0;
our $NOFILE = 1;
our $COPYFAIL = 2;
our $SOXFAIL = 3;
our $CURLFAIL = 4;
our $OPENFAIL = 5;
our $APPRUNNING = 6;
our $RAWDBEXIISTS = 7;
our $DBCREATEERR = 8;
our $DBCONNECTERR = 9;
our $TMPFILEERR = 10;
our $DBWRITEERR = 11;
our $INVALIDUSER = 12;
our $DBLOCKERR = 13;
our $DBUNLOCKERR = 14;
use constant WERR => 100;
答案 0 :(得分:5)
如果您使用常量而不是变量,
package Funx;
use strict;
use warnings;
use constant qw( );
use Exporter qw( import );
BEGIN {
my %error_codes = (
FUNX_SUCCESS => 0,
FUNX_NOFILE => 1,
FUNX_COPYFAIL => 2,
FUNX_SOXFAIL => 3,
FUNX_CURLFAIL => 4,
FUNX_OPENFAIL => 5,
FUNX_APPRUNNING => 6,
FUNX_RAWDBEXIISTS => 7,
FUNX_DBCREATEERR => 8,
FUNX_DBCONNECTERR => 9,
FUNX_TMPFILEERR => 10,
FUNX_DBWRITEERR => 11,
FUNX_INVALIDUSER => 12,
FUNX_DBLOCKERR => 13,
FUNX_DBUNLOCKERR => 14,
FUNX_WERR => 100,
);
constant->import(\%error_codes);
my @syms = keys(%error_codes);
our @EXPORT_OK = \@syms;
our %EXPORT_TAGS = ( ALL => \@syms, ERROR_CODES => \@syms );
}
除了解决您提出的问题之外,上述
SUCCESS
代码的模块吗?@ISA
的污染。 Funx不是Exporter的子类。用法:
use Funx; # Imports nothing.
use Funx qw( ); # Imports nothing.
use Funx qw( :ERROR_CODES ); # Imports error codes.
use Funx qw( :ALL ); # Imports error codes.
use Funx qw( FUNX_SUCCESS FUNX_NOFILE ); # Imports specific error codes.
答案 1 :(得分:1)
您可以使用source filter添加一些自定义预处理。
示例:
package MyExport;
use Filter::Util::Call;
sub import {
my ($type) = @_;
my ($ref) = [];
filter_add(bless $ref);
}
sub filter {
my ($self) = @_;
my ($status);
if (($status = filter_read()) > 0) {
s/^(.*)\s+export\s+(\S+)(.*)$/push \@EXPORT, '$2'; $1 $2 $3/;
}
$status;
}
1;
用法:
...
use MyExport;
use Exporter;
our @ISA = 'Exporter';
our @EXPORT;
...
our export $SUCCESS = 0;
...
请注意,此实现可能有问题。基本上s/^(.*)\s+export\s+(\S+)(.*)$/push \@EXPORT, '$2'; $1 $2 $3/;
的正则表达式会变成类似
our export $SUCCESS = 0;
进入
push @EXPORT, '$SUCCESS'; our $SUCCESS = 0;