http://www.perlmonks.org?node_id=157311


in reply to Re: Perl 6 will make amends (was:Perl's Bad Ideas)
in thread Perl's Bad Ideas

...is the statement part of a conditional in the same block as the body itself?

No, the conditional is outside the block, both physically and logically (err...physiologically? ;-).

Your example is equivalent to:

my $el; while ($el = pop @els) { # Body of loop }

BTW, in Perl 6, if and while (and the rest of the control structures) can be thought of as built-in functions. As if they were:

sub while (BOOL $condition, &closure) {...} sub if (BOOL $condition, &closure) {...}

That's why the conditional is not part of the block: it's an entirely separate argument to the control structure.

And, yes, you'll be able to get the signature (a.k.a. prototype) of a control structure, and overload it, and redefine it (lexically).

And you'll probably be able to write bad things like:

if condition(), &call_me;

and worse things like:

&debug := { if BEGIN{$debug}, &^action }; # and later... debug { print "here" }

Replies are listed 'Best First'.
Re: (3): Perl 6 will make amends (was:Perl's Bad Ideas)
by shotgunefx (Parson) on Apr 07, 2002 at 21:35 UTC
    Thanks for the enlightenment. Do you think there will be a mechanism to detect this? One thing I think would be nice would be able to tell if you are called in a control structure or not for lazy DWIM functions, like this findone { coderef } @array

    -Lee

    "To be civilized is to deny one's nature."
      Thanks for the enlightenment.
      Satori can arrive in the most unlikely ways. But I suspect I really only brought you information. ;-)
      ...nice would be able to tell if you are called in a control structure or not for lazy DWIM functions
      Sure. Depending on what kind of context information you're after, you'll use either caller or want (the successor to wantarray).
        Particularly, I'd like to be able to detect if the current sub was called from within a control block.

        For example, I wrote an operator called findone which operates like grep, returning the first match and optionally the index of the match lazily.

        I know I could do this with a iterative closure but I wanted it to be like a built-in. So I saved the args for the call in a hash with a key of caller() = ref. This almost works. The problem is that if it is not called in a control block (Example 2.) it will fail after the last match as there is no context to know that we shouldn't save state. Obviously there are other ways to accomplish this, but I would think there is probably other applications of this type of context info.

        # Example: findone { coderef } @array # Example 1. WORKS while (my ($el,$index) = findone { m/good/ } @potentially_huge_array) +{ # Found match in $el do something. } # Example 2. DOESN"T WORK my @tokens = (tokens here); while ($line =<>){ chomp($line); die "$line is not a valid token" unless findone { m/^$line/ } @tok +ens; # Fails after all elements have been processed and will always fai +l from now on. }


        -Lee

        "To be civilized is to deny one's nature."