如何在 Perl 中运行匿名函数? [英] How to run an anonymous function in Perl?
本文介绍了如何在 Perl 中运行匿名函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
(sub {
print 1;
})();
sub {
print 1;
}();
我尝试了各种方法,都错了...
I tried various ways, all are wrong...
推荐答案
(sub { ... })
会给你指向函数的指针,所以你必须通过引用调用.
(sub { ... })
will give you the pointer to the function so you must call by reference.
(sub { print "Hello world\n" })->();
Blagovest Buyukliev 指出的另一种简单方法是取消引用函数指针并调用它使用 { }
运算符
The other easy method, as pointed out by Blagovest Buyukliev would be to dereference the function pointer and call that using the { }
operators
&{ sub { print "Hello World" }}();
这篇关于如何在 Perl 中运行匿名函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文