检测在Perl中如何调用子例程
我想检测如何调用子例程,以便根据不同情况使其行为不同:检测在Perl中如何调用子例程,perl,subroutine,Perl,Subroutine,我想检测如何调用子例程,以便根据不同情况使其行为不同: # If it is equaled to a variable, do something: $var = my_subroutine(); # But if it's not, do something else: my_subroutine(); 这可能吗?使用 是的,你要找的是: 此代码生成以下输出: Called and assigned to an array! Called and assigned to a scalar
# If it is equaled to a variable, do something:
$var = my_subroutine();
# But if it's not, do something else:
my_subroutine();
这可能吗?使用
是的,你要找的是: 此代码生成以下输出:
Called and assigned to an array!
Called and assigned to a scalar!
Called in void context!
Robin Houston开发了一个功能强大且非常有用的XS模块。它扩展了核心perl函数wantarray()提供的功能,并使您能够发现您的方法(例如)是否在对象上下文中被调用
$object->do_something->some_more;
在这里的dou_something
中,您可以在返回之前编写:
if( want('OBJECT') )
{
return( $object );
}
else
{
return; # return undef();
}
还有一个模块,但在这种情况下,它可能是杀伤力过大。
$object->do_something->some_more;
if( want('OBJECT') )
{
return( $object );
}
else
{
return; # return undef();
}