How can I selectively access elements returned by a Perl subroutine?

后端 未结 3 669
余生分开走
余生分开走 2020-12-10 12:56

Say a Perl subroutine returns an array:

sub arrayoutput
{
    ...some code...
    return @somearray;
}

I want to access only a specific arr

3条回答
  •  萌比男神i
    2020-12-10 13:38

    Pull off the first argument only via list context:

    my ( $wanted ) = array_returning_sub( @args );
    

    TIMTOWTDI with a slice:

    my $wanted = ( array_returning_sub( @args ) )[0];
    

    Both styles could be extended to extract the n'th element of the returned array, although the list slice is a bit easier on the eye:

    my ( undef, undef, $wanted, undef, $needed ) = array_returning_sub( @args );
    
    my ( $wanted, $needed ) = ( array_returning_sub( @args ) )[2,4];
    

提交回复
热议问题