in reply to Re^5: Perl can do it, take 1 (sentence generation)
in thread Perl can do it, take 1 (sentence generation)

I'm not really recommending it as a good idea, just pointing out that it can be done and is pretty much the direct translation of cond. That said, the problem with doing other stuff in the blocks only arises when you leave the realms functional programming and start using side-effects (and in fact mixing side effect with returning values).

I just tried something interesting:

my ($x, $y) = (0,0); for my $i (1..2) { do { if ($i == 1) { $x; } else { $y; } } = 7; print "i = $i, x = $x, y = $y\n"; }

This doesn't work but it would have worked with an lvalue sub so why not allow lvalue do blocks.

Replies are listed 'Best First'.
Re^7: Perl can do it, take 1 (sentence generation)
by BrowserUk (Pope) on Jun 21, 2005 at 17:21 UTC

    Actually, one of the major disappointments I have with Perl6 is the continued distinction between subs and blocks. Personally, I think that blocks should be first class entities in their own right.

    If you assigned a block to a variable, it would become a named subroutine.

    my $sub = { ## do stuff }; my $result = $sub();

    if, [while, for etc. would be methods called on the block:

    { #do stuff }.if( cond ); .if( cond ) { ## indirect object form ## do stuff } .while( cond ) { ## do stuff } .for $a -> ( 0 .. 10 ) { ##do stuff }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      Is that Rubyish? Funny, in Smalltalk I think it's the other way around. If is just a method you can call on a value and it takes blocks of code as paramter.