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