Front page | perl.perl6.internals |
Postings from August 2001
given when
From:
Bryan C . Warnock
Date:
August 5, 2001 15:35
Subject:
given when
Message ID:
01080518334804.29900@wakko.idiocity.nut
More questions regarding the new 'given when' construct.
1) Is a 'when' clause required? Is it:
[ LABEL: ] given ( expr_1 ) {
when expr_2 : block
[ when expr_3 : block ...]
expr_n [;]
}
or
[ LABEL: ] given ( expr_1 ) {
[ when expr_2 : block ...]
expr_n [;]
}
?
2) In expressions 2 through n-1 - the equivalent to 'case' - you may refer
to the return value of expression 1 with $^_. Does this imply that
expression 1 will always (and only) be evaluated in scalar context? If not,
can you mix contexts across 'when' clauses? Is the value of $^_ then a
reference to the return value?
3) Can you refer to $^_ within expression n - the equivalent to 'default:'?
given ( a() ) {
print $^_; # No when clause? See #1
}
4) Can you refer to $^_ within the code blocks of each 'when' clause?
given ( a() ) {
when $^_ == 1 : { foo($^_) }
when $^_ == 2 : { bar( 5 + $^_ ) }
when $^_ == 3 : { ++$^_ and print $^_ } # Mutable? See #5
}
5) Is $^_ an alias, reference (See #2), or a copy of the return value of
expression 1, and is it mutable? Does it have the appropriate magic and
overloading capabilities?
6) How many times is expression 1 evaluated?
$a = 0;
given ( ++$a ) {
when $^_ > 5 : { #foo }
when $^_ > 4 : { #bar }
when $^_ > 3 : { #baz }
#foobar
}
I would expect that $a would equal '1' after #foobar executed. But if
multiple contexts are allowed (see question 2), how would
sub a { # returns something different based on scalar or array context }
given ( a() ) {
when $^_ =~ /foo/ : { #foo in scalar context, with "" overloading? }
when $^_[5] eq "bar" : { #bar in list context (bad variable) }
when $^_ : { #baz in boolean context? }
when $^_ + 5 > 15 : { #foobar in scalar, with addition overloading? }
#default
}
be evaluated? In particular, would the fourth 'when' clause (the second use
of scalar context) use the previously obtained scalar value?
7) The 'when' blocks are not fall-through, a la C's switch statement. You
can obtain a similar result by invoking 'next' within the 'when' block.
given ( $a ) {
when expr1 : { print "*" && next }
when expr2 : { print "*" }
}
Is this regardless of whether expr2 would have also evaluted true? (IOW,
does it truly jump to the next block?) If not, does it jump to the
expression after that (if it exists) and try again, or does it default
expression (since the original next expression evaluated false)?
8) Is the order of evaluation guaranteed? (I assume so, but just thought
I'd check.)
9) It has a lexical construct similar to a conditional block, so that
lexical variables declared in the 'given' expression are within the inner
scope, correct?
$a = 5;
given (my $a = 1) {
when $^_ == 1 : { print $a } # 1
print $a; # 1
}
print $a; # 5
10) I've already asked Damian this question on the side, but I'll repeat
(and expand) it here, to give me an even ten. Should it be a single default
expression, multiple default expressions, or a default block?
[ LABEL: ] given ( expr_1 ) {
...
expr_n [;]
}
[ LABEL: ] given ( expr_1 ) {
...
expr_n
[; expr_n+1 ... ] [;]
}
[ LABEL: ] given ( expr_1 ) {
...
block
}
--
Bryan C. Warnock
bwarnock@capita.com
-
given when
by Bryan C . Warnock