开发者

Retrieve a particular return value in Perl

I'm looking for a one-liner that lets me grab the second return value from a subroutine.

Rather than this:

($a,$b)=function;
print 开发者_StackOverflow中文版$b

It should be possible to do something like this

print ??? function


This works:

sub test { return (1,2) }
print ((test)[1]);  # Returns 2

This also works:

print +(func())[1], "\n";


assuming that function() returns a list, then using a slice like the poster above suggested works just fine. If it returns an array reference, then you need to access it appropriately, for instance (@{function()})[1] to dereference the aref and then slice it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜