How to run an anonymous function in Perl?

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



sub {
print 1;
}();

I tried various ways, all are wrong...

7条回答
  •  盖世英雄少女心
    2021-01-31 09:27

    You might not even need an anonymous function if you want to run a block of code and there is zero or one input. You can use map instead.

    Just for the side effect:

    map { print 1 } 1;
    

    Transform data, take care to assign to a list:

    my ($data) = map { $_ * $_ } 2;
    

提交回复
热议问题