in reply to Re: Re: Re: A list returns its last, an array returns its weight ...
in thread A list returns its last, an array returns its weight
That's not to say that wantarray is never useful. For example, if building a result list is expensive, you can use wantarray to avoid that cost in scalar context.# let array handle context. # list of items in list context, count of items in scalar. sub find_things { my( $storage, $criteria ) = @_; my @things = $storage->lookup( $criteria ); @things } # let grep handle context. # list of items in list context, count of items in scalar. sub matching_things { my( $storage, $pat ) = @_; grep /$pat/, $storage->things() }
# list of items in list context, first item in scalar. sub matching_things { my( $input_iter, $pat ) = @_; my @things; while ( <$input_iter> ) { /$pat/ or next; wantarray or return $_; push @things, $_; } @things }
jdporter
...porque es dificil estar guapo y blanco.
|
|---|