本文介绍了如何在Perl的闭包工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! $ p 这里是一个代码的例子。我不明白: sub make_saying { my $ salute = shift; my $ newfunc = sub { my $ target = shift; print$ salute,$ target!\\\; }; return $ newfunc; #return a closure } $ f = make_saying(Howdy); #创建一个闭包 $ g = make_saying(Greetings); #创建另一个闭包#时间通过... $ f->(world); $ g->(earthlings); 所以我的问题是: 如果一个变量被分配给一个函数,它是否自动引用该函数? 在上面的代码中,我可以写 $ f = \make_saying(Howdy)?我什么时候可以使用& ,因为我尝试在传递参数(& $ f(world)),但它不工作。 最后,在上面的代码中,他在**上面做了 world 和添加到 howdy 和 greices 。 注意:我理解$ f有点绑定到函数的参数 howdy 这是我的理解如何世界附加。我不明白的是里面的第二个函数。如何操作它的魔法。抱歉,我真的不知道如何问这个。解决方案在Perl中,标量变量不能直接保存子例程,只保留引用。这非常像标量不能持有数组或散列,只能包含arrayref或hashref。 sub {...} 求值为coderef,所以你可以直接赋值给一个标量变量。如果你想分配一个命名的函数(例如 foo ),你必须获得 \& foo 。 您可以调用coderefs loke $ code->(@ args)或& $ code(@args)。 代码 $ f = \make_saying(Howdy) code> make_saying(Howdy),并引用返回的值。因此,你得到一个指向coderef的引用,而不是一个coderef本身。 因此,它不能像& $ f (world),您需要取消引用一个额外的级别:& $$ f(world)。 闭包是绑定到某个环境的函数。 环境包含所有当前可见的变量,因此闭包始终记住此范围。在代码 my $ x; sub foo { my $ y; return sub {$ x,$ y}; } foo 关闭 $ x ,因为外部环境由 $ x 组成。内部子是 $ x 和 $ y 的关闭。 每次执行 foo 时,我们得到一个新的 $ y ,因此一个新的闭包。 当我们执行 make_saying(Howdy), $ salute 变量设置为 Howdy 。 当我们用 myke_saying(Greetings)再次执行它时,的 make_saying 。 $ salute 现在设置为 Greetings ,内部子关闭此变量。这个变量与上一个 $ salute 分开,它仍然存在,但除非通过第一次闭包才能访问。 这两个问候语已经关闭了单独的 $ salute 变量。当它们被执行时,它们各自的 $ salute 仍在范围内,并且他们可以访问和修改该值。 Newbie in Perl again here, trying to understand closure in Perl.So here's an example of code which I don't understand:sub make_saying { my $salute = shift; my $newfunc = sub { my $target = shift; print "$salute, $target!\n"; }; return $newfunc; # Return a closure}$f = make_saying("Howdy"); # Create a closure$g = make_saying("Greetings"); # Create another closure# Time passes...$f->("world");$g->("earthlings");So my questions are:If a variable is assigned to a function, is it automatically a reference to that function?In that above code, can I write $f = \make_saying("Howdy") instead? And when can I use the & because I tried using that in passing the parameters (&$f("world")) but it doesn't work.and lastly, In that code above how in he** did the words world and earthlings get appended to the words howdy and greetings.Note: I understand that $f is somewhat bound to the function with the parameter howdy so that's my understanding how the world got appended. What I don't understand is the 2nd function inside. How that one operates its magic. Sorry I really don't know how to ask this one. 解决方案 In Perl, scalar variables cannot hold subroutines directly, they can only hold references. This is very much like scalars cannot hold arrays or hashes, only arrayrefs or hashrefs.The sub { ... } evaluates to a coderef, so you can directly assign it to a scalar variable. If you want to assign a named function (e.g. foo), you have to obtain the reference like \&foo.You can call coderefs loke $code->(@args) or &$code(@args).The code$f = \make_saying("Howdy")evaluates make_saying("Howdy"), and takes a reference to the returned value. So you get a reference that points to a coderef, not a coderef itself.Therefore, it can't be called like &$f("world"), you need to dereference one extra level: &$$f("world").A closure is a function that is bound to a certain environment.The environment consists of all currently visible variables, so a closure always remembers this scope. In the codemy $x;sub foo { my $y; return sub { "$x, $y" };}foo is a closure over $x, as the outer environment consists of $x. The inner sub is a closure over $x and $y.Each time foo is executed, we get a new $y and therefore a new closure. Each time it is called, a different closure is returned.When we execute make_saying("Howdy"), the $salute variable is set to Howdy. The returned closure remembers this scope.When we execute it again with myke_saying("Greetings"), the body of make_saying is evaluated again. The $salute is now set to Greetings, and the inner sub closes over this variable. This variable is seperate from the previous $salute, which still exists, but isn't accessible except through the first closure.The two greeters have closed over seperate $salute variables. When they are executed, their respective $salute is still in scope, and they can access and modify the value. 这篇关于如何在Perl的闭包工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 10:38
查看更多