How to run an anonymous function in Perl?

前端 未结 7 2014
长发绾君心
长发绾君心 2021-01-31 08:57
(sub {
print 1;
})();



sub {
print 1;
}();

I tried various ways, all are wrong...

7条回答
  •  南方客
    南方客 (楼主)
    2021-01-31 09:46

    (sub { ... }) will give you the pointer to the function so you must call by reference.

    (sub { print "Hello world\n" })->();

    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" }}();

提交回复
热议问题