Front page | perl.perl6.language |
Postings from April 2005
Statement modifier scope
Thread Next
From:
Paul Seamons
Date:
April 15, 2005 10:51
Subject:
Statement modifier scope
Message ID:
200504151150.52407.parrot@seamons.com
The following chunks behave the same in Perl 5.6 as in Perl 5.8. Notice the
output of "branching" statement modifiers vs. "looping" statement modifiers.
perl -e '$f=1; {local $f=2; print "$f"} print " - $f\n"'
# prints 2 - 1
perl -e '$f=1; {local $f=2 if 1; print "$f"} print " - $f\n"
# prints 2 - 1
perl -e '$f=1; {local $f=2 unless 0; print "$f"} print " - $f\n"''
# prints 2 - 1
perl -e '$f=1; {local $f=2 for 1; print "$f"} print " - $f\n"'
# prints 1 - 1
perl -e '$f=1; {local $f=2 until 1; print "$f"} print " - $f\n"'
# prints 1 - 1
perl -e '$f=1; {local $f=2 while !$n++; print "$f"} print " - $f\n"'
# prints 1 - 1
It appears that there is an implicit block around statements with looping
statement modifiers. perlsyn does state that the control variables of the
"for" statement modifier are locally scoped, but doesn't really mention that
the entire statement is as well. I'm not sure if this was in the original
design spec or if it flowed out of the implementation details, but either way
it seems to represent an inconsistency in the treatment of locality with
regards to braces (ok I guess there are several in Perl5).
So the question is, what will it be like for Perl6. It would seem that all of
the following should hold true because of scoping being tied to the blocks.
pugs -e 'our $f=1; {temp $f=2; print $f}; say " - $f"'
# should print 2 - 1 (currently prints 2 - 2 - but that is a compiler
issue)
pugs -e 'our $f=1; {temp $f=2 if 1; print $f}; say " - $f"'
# should print 2 - 1 (currently dies with parse error)
pugs -e 'our $f=1; {temp $f=2 for 1; print $f}; say " - $f"'
# hopefully prints 2 - 1 (currently dies with parse error)
As a side note - pugs does work with:
pugs -e 'our $f=1; {$f=2 for 1; print $f}; say " - $f"'
# prints 2 - 2 (as it should. It seems that statement modifiers don't
currently work with declarations - but that is a compiler issue - not a
language issue.)
I have wanted to do this in Perl5 but couldn't but would love to be able to do
in Perl6:
my %h = <a 1 b 2 c 3>;
{
temp %h{$_} ++ for %h.keys;
%h.say; # values are incremented still
}
%h.say; # values are back to original values
Paul
Thread Next
-
Statement modifier scope
by Paul Seamons