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

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

Thread Previous | Thread Next
From:
Aristotle Pagaltzis
Date:
September 27, 2012 08:15
Subject:
Re: given/when/~~ "final" thoughts (ha ha ha)
Message ID:
20120927151452.GA29921@fernweh.plasmasturm.org
* Smylers <Smylers@stripey.com> [2012-09-27 10:35]:
> 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.
>
> > 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.

So you get briefly surprised either way. :-)

That leaves the question of which behaviour is the gift that keeps on
giving. Is either behaviour harmful? As far as I can see, neither is.
Then which behaviour is inconvenient? Obviously – to me – the lack of
scope sharing.

> > 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.

That would be a silly reason to have to consider switching constructs,
would it not? Yet another point of friction, like having to choose
whether to copy from @_ to lexicals in an extremely short function –
“which kind of ugliness do I prefer?”. It seems entirely unnecessary
to inflict that on the language.

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

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