in reply to The Case for Macros in Perl

I don't understand ... what are macros?

my $you_ve_been_bacrod = eval niceMacro(...);

macro? Filter::Macro?

Replies are listed 'Best First'.
Re^2: The Case for Macros in Perl
by eyepopslikeamosquito (Archbishop) on Sep 13, 2014 at 12:46 UTC
        Perhaps with an agreed-upon Perl 6 spec we can eventually get the same backported into Perl 5 ;-)

      Actually thinking of something like a Lisp macro, in particular being self-processed source-code not merely on the textual level but on a level higher than that (below the perl 6 macro level though)

      In this specific case, my thinking is to create a text string and eval it and see if that is cleaner. But it would be nicer to be able to process statements of perl as statements of perl, not as strings which will hopefully compile.

Re^2: The Case for Macros in Perl
by BrowserUk (Patriarch) on Sep 13, 2014 at 07:31 UTC
    I don't understand ... what are macros?

    Textual substitutions made, once, inline, at compile time.

    • evals are made every time they are encountered, at runtime.
    • Source-code filters partially fit the bill, but are buggy, unreliable, and a maintenance nightmare.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Actually I think evals and strings would be a good match for this use case because the primary use here is only done once, on module load (this could be done at near-compile time by putting the method generators in a BEGIN block).

      However this specific case would be more generally solvable as a compile time macro, i.e. expanding the macro call at compile time into exactly what you want. The only reason I haven't seriously considered going with eval and strings is I haven't had the time to put together a good proof of concept for comparison's sake. When I do, I may ask about clarity here or in the seekers forum.

      evals are made every time they are encountered, at runtime.

      Sure, right, but

      use MyFoo -macronic => <<'YO'; ... YO;
      ???
        Sure, right, but
        use MyFoo -macronic => <<'YO'; ... YO;
        ???

        What is that meant to prove or disprove?

        All you've succeeded in doing is pass a multi-line string as an argument to a modules import list.

        A (trivial) example macro might look something like this:

        use Macro ADD( x, y ) { x + y; } ... my $p = getPfromSomewhere(); my $q = getQfromSomewhere(); my $r = ADD( $p, $q );

        Which would substitute out as:

        my $p = getPfromSomewhere(); my $q = getQfromSomewhere(); $r = $p + $q;

        And better yet:

        use Macro ADD( x, y ) { x + y; } ... my $r = ADD( getPfromSomewhere(), getQfromSomewhere() ); ... my $s = ADD( geta(), getb() );

        Resulting in just:

        my $r = getPfromSomewhere() + getQfromSomewhere(); my $s = geta() + getb();

        Allowing the efficient use of small "functions and methods" at the source level without the runtime overhead of the calls.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
        functions and methods