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

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

Thread Previous | Thread Next
Brian Fraser
March 25, 2013 01:57
Re: Lexical subs allow overriding if/else & other syntax keywords?
Message ID:
On Sun, Mar 24, 2013 at 6:00 PM, Father Chrysostomos <> wrote:
> 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.

Ah.. For whatever reason I was under the impression that the outcome
of #108286 was disallowing the override of if/else/elsif/continue, but
looks like my head just made that up. That was my argument for calling
this a bug; Sorry for that noise!
That being said, I feel the ickyness has moved elsewhere: if
overriding *CORE::GLOBAL::if currently ignored, shouldn't the same
happen for lexical overrides? Otherwise, this introduces yet another
special case to the language: 'if' can be lexicalized but not
localized, which is inconsistent with both how other keywords work
(which allow overriding both ways) and with how special variables work
('$$' can be localized but can't be lexicalized).

>  Conceptually, ‘if’ is in the CORE:: namespace.  Since lexical names take precedence over those residing in symbol tables, the lexical ‘if’ takes precedence here.

Aye, this makes sense.

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

If I'm not mistaken, the keyword plugin happens before checking for
any other overrides, so I'm pretty sure this is already the case? Or
can you do a different sort of override with a lexical sub plus the
keyword plugin?

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