2015-06-25 110 views
2

关于下面的代码,$ one_sub和$ two_sub如何变成指定子“sup”中的匿名子例程的coderef?指定的子不是'返回'这两个匿名子;还是它? (至少我没有提出这样的声明)。perl命名子程序中的匿名子程序

sub sup { 

      my $neh = sub { 
        say "this is 'neh' subroutine" 
      }; 

      my $hen = sub { 
        say "this is 'hen' subroutine" 
     }; 
      ($neh, $hen); 
    } 

    my ($one_sub, $two_sub) = ⊃ 

使用数据::自卸车::流光显示的:

$CODE1 = sub { 
      use warnings; 
      use strict; 
      no feature; 
      use feature ':5.10'; 
      say q[this is 'neh' subroutine]; 
     }; 
$CODE1 = sub { 
      use warnings; 
      use strict; 
      no feature; 
      use feature ':5.10'; 
      say q[this is 'hen' subroutine]; 
     }; 
+4

['-f的perldoc return'(http://perldoc.perl.org/functions/return.html):“在没有一个显式的'return',一个子例程,'eval'或'do FILE'会自动返回最后一个表达式的值。 – ThisSuitIsBlackNot

+1

@ThisSuitIsBlackNot应该可能是一个答案。 –

+0

这是明确回报是好事的原因之一:) – Sobrique

回答

8

引述perlsub

如果没有发现return,如果最后一条语句是一个表达式,它的值返回。

($neh, $hen);是一个表达式。在列表上下文中,其值是2个匿名的子集。