http://qs1969.pair.com?node_id=1219731

dk has asked for the wisdom of the Perl Monks concerning the following question:

Dear all, what is the best way today to implement a macro I used to code with Devel::Declare:

x {} needs to be expanded as x $foo, sub {}. I tried Syntax::Keyword, it doesn’t allow to use x as function parameter. Function::Parameters looks rather complicated if I want to copy the macro mechanics. I wonder if there is a module that can help me?

Update: submitted a patch to Syntax::Keyword to allow expressions. Let's see if it is accurate enough ( I basically just hacked in parse_fullexpr() and that's all).

Replies are listed 'Best First'.
Re: Devel::Declare is deprecated, what’s instead? (Keyword::Simple)
by LanX (Saint) on Aug 02, 2018 at 20:45 UTC
    > x $foo, sub {}

    I don't really understand what you are trying to achieve, but if Function::Parameters is an option, it's based on Keyword::Simple from the same author, which facilitates the use of Pluggable keywords a lot.

    Be warned that keywords need >5.12 and only expand at the start of a statement (like after a semicolon). otherwise we could have a real syntactic macro (i.e. Lisp like) mechanism in Perl.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      In perlapi, the description of KEYWORD_PLUGIN_EXPR implies the pluggable keyword API can support keywords in the middle of an expression. Unfortunately, Keyword::Simple (and modules that use it) doesn't support that. I'm not sure whether Keyword::API supports that.

        I spoke to Lukas - aka "Mauke" - the author of Keyword::Simple in person at various conferences about this and macros in general.

        He said that it's pluggable keys which is not supporting it, and that he thinks that it's due to a bug in the implementation. I'm sure he would love to support it if possible. IIRC he even mentioned a discussion on P5P.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: Devel::Declare is deprecated, what’s instead?
by tobyink (Canon) on Aug 03, 2018 at 06:40 UTC

    As well as the modules mentioned, there's this thing Parse::Keyword which is pretty good.

    It does have a weird issue involving how it closes over variables, though it can be worked around using PadWalker. Sadly after I reported that issue, the author decided that was a good reason to deprecate Parse::Keyword. But deprecated or not, once you work around that issue, it's a good module.