in reply to Variable scope in ternary ?: operator not propagating?

In Perl 6 it will work as you expect, because the rule has been changed to introduce lexicals as soon as the name is seen, rather than waiting for the end of the current statement.
  • Comment on Re: Variable scope in ternary ?: operator not propagating?

Replies are listed 'Best First'.
Re: Re: Variable scope in ternary ?: operator not propagating?
by theorbtwo (Prior) on Mar 15, 2003 at 04:28 UTC

    Then how do you achieve the current effect of

    my $x=42; print "\$x before block: $x\n"; { my $x=$x; $x++; print "\$x inside block: $x\n"; } print "\$x after block: $x\n";
    ? (Should give 42, 43, 42.)


    (Update: added missing $x++;, thanks, BrowserUk.) Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

      I suspect it will be by way of one of the new pseudopackages - as an educated guess, since all blocks are really closures in Perl 6, I'd say CALLER::. So my $x = $CALLER::x; will probably do that.

      Or I may be entirely wrong. :-)

      Makeshifts last the longest.