Perl 中函式引用


在使用 Perl 指令碼時,如果您需要建立一個訊號處理程式,那麼這樣可能發生,您可以透過將 \& 字首到該函式名稱來建立對一個函式的引用,並且為了解除該引用的引用,您只需要使用 & 字首引用變數。以下是示例 −

示例

 線上演示

#!/usr/bin/perl
# Function definition
sub PrintHash {
   my (%hash) = @_;
   foreach $item (%hash) {
      print "Item : $item\n";
   }
}
%hash = ('name' => 'Tom', 'age' => 19);
# Create a reference to above function.
$cref = \&PrintHash;
# Function call using reference.
&$cref(%hash);

輸出

當執行上述程式時,它生成以下結果 −

Item : name
Item : Tom
Item : age
Item : 19

更新於: 29-Nov-2019

148 瀏覽

開啟你的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.