On 27 Oct 2002, Marco Baringer wrote:
: why not use -> to create a sub which you can return from?
:
: if $foo -> {
: ...
: return if $bar;
: ...
: }
Except that by the current rule you can only C<return> from something
that is declared with the word "sub". ->{...} is still just a fancy
block from that perspective.
: this of course means you can't directly return from the sub (or whatever) in
: which the if (or given or while or for) is nested...
Which is why the rule for "return" says there has to be a "sub",
because that's what people will usually expect "return" to do, at least
until they get sophisticated about every block being a subroutine.
And that's also why we need a different way of returning from the
innermost block (or any labelled block). "last" almost works, except
it's specific to loops, at least in Perl 5 semantics. I keep thinking
of "ret" as a little "return", but that's mostly a placeholder in
my mind. I've got a lot of those...
: slightly related:
:
: what happens to the return value of the subs passed to for, if, while
: and given statements? (what does '$foo = if $bar { ... } else { ... }'
: do?)
Same thing as in Perl 5. Try this:
print do { if (int rand 2) { "true" } else { "false" } }, "\n";
The only difference is that in Perl 6, there is no cat. :-)
Larry