出口
手动调用 @EXPORT =() 东西变得有点憔悴。
package Bar;
use strict;
use warnings;
use Sub::Exporter -setup => {
exports => [qw[ foo ]],
groups => {
default => [qw[ foo ]],
}
};
sub foo(){
};
1;
利用:
use strict;
use warnings;
use Bar foo => { -as-> 'Foo' };
Sub::Exporter 可以做很多很棒的事情,比如组导出、组排除、构建器方法(即:它导出的 subs 的工作方式由传递的参数决定,subs 是在其他 subs 中生成的,等等)
重命名
对于重命名事物,最好有一个辅助函数,它只是作为一个遗留函数,当 Carp() 被调用时,它会推荐指向它的代码,以将其移动到新方法。这将提高代码范围内的一致性。
然后,当您的测试停止发出警告时,您可以删除旧功能。
sub old { # line 1
Carp::carp('Legacy function \'old\' called, please move to \'newmethod\' ');
goto &newmethod; # this passes @_ literally and hides itself from the stack trace.
} # line 4
sub newmethod { # line 6
Carp::cluck('In New Method');
return 5;
} # line 9
print old(), "\n"; # line 11
已调用旧函数“旧”,请移至 code.pl 第 2 行的“新方法”
main::old() 在 code.pl 第 11 行调用
在 code.pl 第 7 行的新方法中
main::newmethod() 在 code.pl 第 11 行调用
5
请注意 newmethod 中的警告看起来与直接调用它们的方式完全相同。