develooper Front page | perl.perl5.porters | Postings from November 2005

Switch in the core: dynamic versus lexical

Thread Next
Robin Houston
November 27, 2005 03:41
Switch in the core: dynamic versus lexical
Message ID:
Dear Damian,

[ CCed to P5P, in case anyone else has comments to make. ]

Since Switch is so very useful, it's a real shame that it can't
reliably be used, because of the limitations of source-filter

I'm working on implementing the construct in core perl, which
would overcome these sad limitations. The idea is to make it
as compatible as possible with your module, and to arrange
things so that the 'switch' and 'case' keywords are recognised
only when is loaded. (Of course it would be necessary
to insert a check into, so that if it's running on
a version of perl that can recognise switch natively, it doesn't
invoke the source filtering.)

(It should be possible to do the right thing with the options
qw(Perl5 Perl6 __ fallthrough) as well, though I'm concentrating
on the core behaviour for today.)

My original plan was to treat a switch block as a lexical
construct, so that each case statement binds to the lexically
enclosing switch. I thought that was the least surprising approach,
in line with the trend towards my() and away from local(),
for example.

But playing with your module, I see that the current implementation
is dynamic rather than lexical.  For example, you can do:

  use Switch;
  switch (23) {

  sub foo {
    case 17 {print "It's 17\n"}
    case 23 {print "It's 23\n"}
    else    {print "None of the above\n"}

and it will print "It's 23".

I'm curious as to whether you regard this as an important and/or
useful feature, or whether it's just an artefact of your chosen

Do you think I should retain the dynamic behaviour, or go with
the lexical binding that I originally had in mind?


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About