SPUG: first vs //
Colin Meyer
cmeyer at helvella.org
Wed Jul 18 13:19:05 PDT 2007
On Wed, Jul 18, 2007 at 11:37:42AM -0700, Michael R. Wolf wrote:
> > Interestingly, Larry's 'first' suggestion is now available in the core:
> >
> > use List::Util qw/first/;
> >
> > $var = first { defined } $foo, $bar, $baz;
>
> In the world of TMTOWTDI, I like.... *both*.
>
> I initially liked the use of 'first' as more intuitive (for my definition of
> intuitive). In reading the "old arguments", I realized that the 'first'
> subroutine does not (in fact, cannot) do short circuiting, in which case the
> // operator is better.
>
> $var = $foo // $bar // $baz;
>
> Short circuiting (lazy evaluation) makes much more sense when functions are
> called since they could have a non-trivial cost and side-effects that may
> need to be avoided. In some cases the later functions must be guarded
> against execution if earlier ones succeed.
>
> $var = foo() // bar() // baz();
Short circuiting with first:
$var = first { $_->() } \( &foo, &bar, &baz );
-Colin.
More information about the spug-list
mailing list