develooper Front page | perl.perl5.porters | Postings from March 2013

Re: Lexical subs allow overriding if/else & other syntax keywords?

Thread Previous | Thread Next
Father Chrysostomos
March 24, 2013 21:00
Re: Lexical subs allow overriding if/else & other syntax keywords?
Message ID:

On Mar 24, 2013, at 12:52 PM, Brian Fraser wrote:

> This might be a bug:
> $ ./perl -M-warnings -Ilib -Mfeature=lexical_subs -E 'my sub else (&)
> { say "woah, what?"} if (1) {} else {}'
> woah, what?
> As there's no way of getting that override with normal subs. However,
> similar behavior is tested for in lexsub.t, so this looks intended?

Yes!  That is as intended.  There is no intrinsic reason why ‘if’ should not be a function if ‘system’ is.  After all, they are both special keywords with special syntax.  Conceptually, ‘if’ is in the CORE:: namespace.  Since lexical names take precedence over those residing in symbol tables, the lexical ‘if’ takes precedence here.  Using one of Zefram’s magic modules, you could even attach a parser to your lexical ‘if’ to make it parse just like the built-in ‘if’, but perhaps with more features.

> Since lexical subs are experimental I don't think is needs to be
> addressed right away, but it certainly looks icky.

Not icky at all. :-)

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