Steven Lu
Steven Lu

Reputation: 43427

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 $b

It should be possible to do something like this

print ??? function

Upvotes: 0

Views: 154

Answers (2)

Ted Hopp
Ted Hopp

Reputation: 234795

This works:

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

This also works:

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

Upvotes: 6

BadFileMagic
BadFileMagic

Reputation: 701

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.

Upvotes: 0

Related Questions