Here's a concise snippet showing how nice it would be to have macros. I'm writing a better, static version that runs a BEGIN-time version but I could have been done already. I'm grumpy.

my @attr = eval join ',', map sprintf( 'map( $self->stringify_%1$s( $op, $_ ), @%1$s_attribu +tes )', $_ ), qw( string number unhandled treeorder execorder );

Replies are listed 'Best First'.
Re: Macro-envy, a snippet.
by Aristotle (Chancellor) on Feb 21, 2006 at 02:33 UTC
    • my @attr = map { my $type = $_; no strict 'refs'; map $self->${ \"stringify_$type" }( $op, $_ ), @{ "${type}_attributes" }; } qw( string number unhandled treeorder execorder );
    • use PadWalker qw( peek_my ); my @attr = map { my $type = $_; map $self->${ \"stringify_$type" }( $op, $_ ), @{ peek_my( 0 )->{ "\@${type}_attributes" } || [] }; } qw( string number unhandled treeorder execorder );

    Hi, my name is Aristotle, and I’m a hack value addict.

    Makeshifts last the longest.

Re: Macro-envy, a snippet.
by dragonchild (Archbishop) on Feb 20, 2006 at 16:02 UTC
    A lot of days, I wish I had C-macros or inlinable functions, let alone Lispy macros. Just anything to avoid those extra subroutine calls for the 20-char utility subroutines that every single function seems to end up needing. Like uniq().

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      Agreed. I'm pretty certain that if B::Generate was maintained, it would be possible to use it to produce a fairly simple module that would allow the creation of real macros.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I started work on Sub::Compose in part to start down this road. Right now, it uses Data::Dump::Streamer to deconstruct the subrefs into text, then stitches them together and evals them. I got some info from a few people that I haven't had the tuits to really work through to build a composer on the bytecode level. Maybe, I should.

        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

        Well see... I've been fixing up B::Utils first. That's more basic than B::Generate. I just haven't gotten to Generate yet. Maybe next year. I'm perfectly happy if someone else wants CPAN perms to send updates.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Macro-envy, a snippet.
by ysth (Canon) on Feb 20, 2006 at 07:46 UTC
    So why all the @foo_attributes arrays instead of an %attributes HoA? And don't forget ->$method syntax.
      So why all the @foo_attributes arrays instead of an %attributes HoA?

      I got strict to check my names for me - that wouldn't happen with hash keys. It's also cheaper to use. I use ->$method syntax when forced to but it's verbose. It requires two statements where I previously had one expresssion.

      my $m = "stringify_%1$s"; $self->$m( $op, $_ )

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        You can roll these into a single expression, as I posted in direct reply: $self->${ \"stringify_$foo" }( $op, $_ )

        Makeshifts last the longest.

        Previously, you had one expression and a macro (that was probably defined a long way away). If you're optimizing for the count of the number of expressions, I think you're doing it wrong.

        --
        brian d foy <brian@stonehenge.com>
        Subscribe to The Perl Review
        Hash::Util (in the core in Perl 5.8) has routines to lock hashes. Sure, it happens at run time rather than compile time. But still I've found it to be very useful when developing software.
Re: Macro-envy, a snippet.
by tilly (Archbishop) on Feb 24, 2006 at 04:31 UTC
    If you want to define your own macro system is, you know where Filter::Simple is.