Matthijs van Duin wrote:

On Mon, Mar 31, 2003 at 02:58:12PM -0800, Michael Lazzaro wrote:

    my $x = &baz(...args...);
    return $x if $x;

I'm looking for a Perl6 way to say that oft-repeated, oft-chained two-line snippet up there without declaring the temporary variable. Using C<given> or C<when>, maybe?

$_ and return $_ given baz(...args...);

Yep. Or:


given baz(@args) { return $_ when true }

Which generalizes nicely to other definitions of success:

  given baz(@args) { return $_ when defined }
  given baz(@args) { return $_ when $_ > 0 }
  # etc.


note that putting & in front of a sub call won't work in perl 6 (that syntax is used to actually refer to the right sub var itself, iirc)

&baz does refer to the Code object itself, as you say.


However, the &bar(...) syntax *will* DWYM too. That's because:

&baz(@args);

is just a shorthand for:

&baz.(@args);

and &baz in the scalar context of the . dereferencer returns a Code reference.

Damian




Reply via email to