我有一個包,為了簡單起見,我們稱它為 Foo.pm。在那里,我得到了一些我想匯出的匿名函式。
Foo.pm 如下所示:
package Foo;
use Exporter qw(import);
our @EXPORT_OK = ();
our @EXPORT = qw(
subroutine1
subroutine2
$anon_function
);
subroutine1 {
# Do something here
}
subroutine2 {
# Do something else here
}
my $anon_function = sub {
my $parameter = shift;
# Do something with parameter
return 1 if $parameter == 1 or $parameter == 2;
return 0;
}
在我的主腳本中,將其命名為 bar.pl,我正在匯入模塊并(顯然)使用它的功能。
bar.pl:
use lib "/usr/share";
use Foo;
subroutine1("foobar");
subroutine2("foobar");
&$anon_function("foobar");
在 Foo.pm 之外使用正常的子例程似乎沒有問題,當我到達&$anon_function()它時會產生以下錯誤:
Use of uninitialized value in subroutine entry at ./bar.pl line 7
嘗試使用 列印匿名函式時print Dumper \$anon_function,它還回傳$VAR1 = \undef
TL;DR:如何從包中匯出匿名函式?
uj5u.com熱心網友回復:
您不能匯出詞法變數。您只能匯出符號,即封裝全域變數和命名子程式。
更改為我們的作品:
use Exporter qw{ import };
our @EXPORT = qw( $anon );
our $anon = sub { ... };
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/521579.html
標籤:perl匿名函数perl 模块
上一篇:為什么我不能像我期望的那樣參考(特定的)Perl陣列?
下一篇:如果滿足條件,則排除單詞
