in reply to Re: perl compiler optimizer curiosity
in thread perl compiler optimizer curiosity

unfortunately there is no macro mechanism in pure Perl.

Technically, there is. Although using source code filters is not a good idea, it's basically a smarter version of C makros.

perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'

Replies are listed 'Best First'.
Re^3: perl compiler optimizer curiosity
by LanX (Saint) on Mar 15, 2022 at 11:50 UTC
    Sorry, if I say "macro" I mean real ones like in Lisp.

    Preprocessor macros like in C are sh*t in comparison.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      if I say "macro" I mean real ones like in Lisp. Preprocessor macros like in C are sh*t in comparison.

      I never used Lisp, but I know C. Can you show some examples?

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        We had this discussion multiple times before, but I can't find those threads in a hurry.

        Preprocessing/source filter means you have an second parsing stage which has not only to reproduce the whole syntax (horror in Perl) but might also expand to unpredictable results when combined (like two source filters)

        Lisp'ish Syntactic macros:

        • look and parse like functions
        • the arguments are used as building blocks for an "expanded" template
        • the expansion happen while compiling in the same stage
        • don't require any syntax parsing by themselves
        • can be used to efficiently extend syntax

        There are still some traps, like the need for "hygiene", but this a problem with dumber source filters too.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        update
        see also https://en.wikipedia.org/wiki/Macro_(computer_science)#Syntactic_macros