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

Switch in the core: dynamic versus lexical

Thread Next
From:
Robin Houston
Date:
November 27, 2005 03:41
Subject:
Switch in the core: dynamic versus lexical
Message ID:
20051127113903.GA17494@rpc142.cs.man.ac.uk
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
parsing.

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 Switch.pm is loaded. (Of course it would be necessary
to insert a check into Switch.pm, 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) {
    foo();
  }

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

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

Robin

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