in reply to (Sort of) poll: what Perl6 features do you consider {likely,desirable} to leak into P5?

Macros.

B::Generate already has most (all?) of the nouce required to make macros available in Perl 5 right now. I wish I could pursuade it to build on win32.

As a very loose spec, something like this ought to work.

use Macro 'min( $x, $y )' => q[ ( $x < $y ? $x : $y ) ], 'max( $x, $y )' => q[ ( $x > $y ? $x : $y ) ], ; ... my( $p, $q, $r ) = ( 1, 2, 3); my $i = min( $p, $q ); my $j = max( $q, $r );

This can be almost done now by evaling the strings and storing a coderef in a hash, and declaring an empty sub with the macro name. Then at INIT{} time, walk the codetree looking for calls to the subs and substitute the code generated earlier for the subcalls.


Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.
  • Comment on Re: (Sort of) poll: what Perl6 features do you consider {likely,desirable} to leak into P5?
  • Download Code

Replies are listed 'Best First'.
Re^2: (Sort of) poll: what Perl6 features do you consider {likely,desirable} to leak into P5?
by blazar (Canon) on Mar 10, 2005 at 08:07 UTC
    Macros.
    Now that you mention them it occurs to me that another desirable feature could be the possibility of creating user defined (infix, postfix, postcircumfix, etc.) operators. But I doubt that even hypothetically this could be doable without anything close to Perl6's powerful prototyping.

      I'd like that ability too, but I seriously doubt it is possible.

      Having (just) managed to work my way through the process of adding a new keyword, I have a new (but still basic) understanding of the process involved in toke.c.

      I cannot imagine that it would be an easy task to allow the addition on new operators on the fly.

      If the macro facility was added, then you might be able to define a few catchall keywords that would act as placeholders in the syntax. Say uniop() and binop() and then wrap those in macros to define an new operator. Not sure about that.


      Examine what is said, not who speaks.
      Silence betokens consent.
      Love the truth but pardon error.
        Having (just) managed to work my way through the process of adding a new keyword, I have a new (but still basic) understanding of the process involved in toke.c.
        Hey, you can't give that hint without telling the whole thing: now I'm too curious. What is it? What is it about?
        If the macro facility was added, then you might be able to define a few catchall keywords that would act as placeholders in the syntax. Say uniop() and binop() and then wrap those in macros to define an new operator. Not sure about that.
        And even in that case it wouldn't have quite the same power Perl6's equivalents are supposed to have, e.g. in terms of user-definable priority. Unless a whole array of new uniop()s and binop()s are also defined, which seems rather awkward after all...