develooper Front page | perl.perl5.porters | Postings from September 2012

Re: given/when/~~ "final" thoughts (ha ha ha)

Thread Previous | Thread Next
From:
Smylers
Date:
September 27, 2012 01:33
Subject:
Re: given/when/~~ "final" thoughts (ha ha ha)
Message ID:
20120927083249.GE15060@stripey.com
Aristotle Pagaltzis writes:

> we have three options here:
> 
> 1. Just let it work the minimal way and let it surprise programmers
>    who expect `when {/(.)/} { say $1 }` to work, on the grounds that
>    it has the semantics you should really be expecting from everywhere
>    else.

That may've briefly surprised me when first brought up, but as soon as
it was mentioned it made sense, is consistent with how lexical scoping
normally works elsewhere with braces, and is what I'd expect.

> 2. Add warnings to warn programmers before they get bitten.

Reasonable. But not essential to have initially; they can always be
added later if needed.

> 3. Make it Just Work.

That would then be surprising in code like this:

  my $grault = 'initial';
  # ...
  when { my $grault = fred($_); garply($grault) || corge($grault) }
  {
    say $grault;
  }
  
I'd expect that to say 'initial', and not for $grault to be hidden by
the temporary value used to avoid repetition inside the condition, and
which just happened to use the same variable name.

> Note also that both options #1 and #2 require the code to be written
> something like
> 
>     my $x;
>     when { $x = foo $bar } { do_something_with $x }

It does, so that's a situation C<when> wouldn't be ideal for. Perhaps
using if () ... elsif () ... would be better for that situation.

Many uses of switch/select-type constructs don't require assigning
variables as part of the tests (indeed, some other languages' equivalent
constructs don't allow doing so, yet are still useful). For C<when> to
be useful, it doesn't need to be able to handle every possible situation
it could be twisted into duty for.

> or worse, in the case of captures,
> 
>     my $c1;
>     when { /(.)/ ? ( $c1 = $1, 1 ) : 0 }  { ... }

Can't that just be written like this:

     when (qr/(.)/) { ... }

> note that we also have cross-block scope sharing in at least one key
> part of the language:
> 
>     if ( my $x = foo $bar ) { do_something_with $x }
>     else                    { do_something_else_with $x }
> 
> (Likewise for `elsif` blocks.)
> 
> There is also this rather more obscure case:
> 
>     while ( my $line = <> ) { ... }
>     continue { log $line }

However, they are slightly different in that the variable is declared in
parens, outside of the blocks it is used in; it doesn't leak out of the
brace-delimited block it is declared in.

Smylers
-- 
New series of TV puzzle show 'Only Connect' (some questions by me)
Mondays at 20:30 on BBC4, or iPlayer: http://www.bbc.co.uk/onlyconnect

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About