2012-10-23 158 views
1

我想弄清楚究竟是什麼在這個簡單的語句正在處理一個匿名函數:返回返回標值

sub testReturn { 
    return sub { 
     my $val = shift; 
     return $val; 
    } 
} 

my $num = testReturn(1); 
print $num; 

被調用時,它輸出:

CODE(0x9c63b34) 

當我試圖讓它只返回1的值。謝謝。

回答

7

您的函數返回另一個函數作爲匿名代碼塊。 你可以調用代碼來爲你做一些事情,如:

my $anonsub = testReturn(); # $anonsub is CODE object 
my $num = &$anonsub(1);  # here this code is called 
print $num; 
+6

或'我的$ NUM = testReturn() - >(1);' – ysth

+2

'我的$ NUM = $ anonsub - >(1 );' –