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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP