检测Perl中如何调用子例程

前端之家收集整理的这篇文章主要介绍了检测Perl中如何调用子例程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想检测一个子程序是如何被调用的,所以我可以根据每种情况使它的行为不同:
# If it is equaled to a variable,do something:
$var = my_subroutine();

# But if it's not,do something else:
my_subroutine();

那可能吗?

解决方法

使用 wantarray
if(not defined wantarray) {
    # void context: foo()
}
elsif(not wantarray) {
    # scalar context: $x = foo()
}
else {
    # list context: @x = foo()
}

猜你在找的Perl相关文章