开发者

Retrieve a particular return value in Perl

开发者 https://www.devze.com 2023-02-12 06:10 出处:网络
I\'m looking for a one-liner that lets me grab the second return value from a subroutine. Rather than this:

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

精彩评论

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

关注公众号