From: Bart Lateur [mailto:[EMAIL PROTECTED]]
> 
> On Wed, 6 Sep 2000 16:24:41 -0500 , Garrett Goebel wrote:
> >> >       grep { $a > $_ and last } @b)
> >> 
> >> So "last" should return true, or what?
> >
> >The last operator doesn't return anything does it? It 
> >immediately exits the loop/block in question.
> 
> But then, what is the value that would be returned to grep()? 

Let's see if I've got this straight. To paraphrase Damian's
RFC 77 for reduce:

If a C<grep>'s block were terminated by a call to C<last>,
grep immediately returns the last block value (i.e. C<undef>
on the first block call, $_[0] otherwise). Or maybe that's @_ 
otherwise... 

@a = grep( { $_ > 2 or last } (@b));

I don't know how grep works internally. I don't know if grep pushes
elements into @a one at a time, or if it returns a finished list of 
elements which pass the conditional block. If it is the latter as I
assume, a short-circuited grep would return a list of all the
elements of @b that had passed through to that point. 

The end result would be that if C<last> occured the first time that
grep's block was evaluated, an empty list would be returned. If it
were called at some later point while processing @b, it would exit
the loop and return the list garnered up to that point.

I'm talking specifically about things with which I'm not specifically
familiar with ;) I'm just a Perl programmer looking in on Perl
development. Someone please straighten me out.

It almost feels like grep could have been written like this (in
another life):  @a = grep (@b) { $_ > 2 or last }

> If you use 0 as the last value evaluated inside the block,
> grep() would return an empty list, 0 in scalar conetxt, and
> all your tests woukld be in vain.
> 
> So: should 
> 
>       scalar grep { 1 and last } LIST
> 
> return 1, if LIST is not empty, and
> 
>       scalar grep { 0 or last } LIST
> 
> return 0?

undef in a scalar context
empty list in an array context


While I'm at it, I'm curious as to why:

$a = 2;
@b = (1, 2, 3, 2, 1));
@a = grep({ { $_ > $a and last} } @b);

results in an exception?

Reply via email to