in reply to Re^2: The Case for Macros in Perl
in thread The Case for Macros in Perl

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

Sure, right, but

use MyFoo -macronic => <<'YO'; ... YO;
???

Replies are listed 'Best First'.
Re^4: The Case for Macros in Perl
by BrowserUk (Patriarch) on Sep 13, 2014 at 09:03 UTC
    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

      Way back, in perl4, there was (from perl.man)

      -Idirectory may be used in conjunction with -P to tell the C prep +rocessor where to look for include files. By default /usr/incl +ude and /usr/lib/perl are searched. -P causes your script to be run through the C preprocessor + before compilation by perl. (Since both comments and cpp di +rectives begin with the # character, you should avoid starting +comments with any words recognized by the C preprocessor such +as "if", "else" or "define".)
      which was dropped (in favour of source filters, I guess). But hey, you can still run your code through cpp:
      #define ADD(x,y) x + y my $r = ADD( getPfromSomewhere(), getQfromSomewhere() ); ... my $s = ADD( geta(), getb() );
      # 1 "macro.pl" # 1 "<command-line>" # 1 "/usr/include/stdc-predef.h" 1 3 4 # 30 "/usr/include/stdc-predef.h" 3 4 # 1 "/usr/include/x86_64-linux-gnu/bits/predefs.h" 1 3 4 # 31 "/usr/include/stdc-predef.h" 2 3 4 # 1 "<command-line>" 2 # 1 "macro.pl" my $r = getPfromSomewhere() + getQfromSomewhere(); ... my $s = geta() + getb();

      "can you tell me the way to Tipperary?" - "well, I wouldn't start from here..."
      ;-)

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
        But hey, you can still run your code through cpp:

        True! But its a little like using a combine-harvester to trim your lawn. With extreme care it could be made to work, but there are lots of logistical problems and the potential for things to go very wrong.


        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.

      What is that meant to prove or disprove?

      That eval doesn't have to be repeatedly called at runtime ... that you can have it at "compile time" ...

      not sure about "Macro" module but it looks like that macro pragma already provides this kind of thing, so what is missing? We have macros right?

        From the docs:
        SEE ALSO macro::JA. macro::filter - macro.pm source filter backend

        With all that implies.


        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.