develooper Front page | perl.perl6.internals | Postings from June 2002

Re: Irrational fear of macros

Thread Previous | Thread Next
From:
Michael G Schwern
Date:
June 19, 2002 01:07
Subject:
Re: Irrational fear of macros
Message ID:
20020619020325.GH3079@ool-18b93024.dyn.optonline.net
On Tue, Jun 18, 2002 at 01:25:49PM -0400, Melvin Smith wrote:
> 1) Macros and debuggers don't play as well together.

I second that.  One of my biggest barriers to useful debugging of perl5 is
having, for example, an HV and having to unroll something like the
SvRMAGICAL() macro to figure out if it's a magical HV.

Now, there is a little trick I use in Perl with macros to get around this
problem and it might work with C.

$ cat ~/tmp/foo.plx 
#!/usr/bin/perl

BEGIN { eval 'use Filter::cpp' }

sub FOO {
    my $bar = shift;
#define FOO($bar) \
    "FOO has ".$bar
}

print FOO(42), "\n";

$ perl ~/tmp/foo.plx 
FOO has 42

$ perl -dw ~/tmp/foo.plx 
Default die handler restored.

Loading DB routines from perl5db.pl version 1.07
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(/home/schwern/tmp/foo.plx:12):
12:     print "FOO has ". 42  , "\n";
  DB<1> x FOO(23)
0  23
  DB<2> 

FOO() is both a macro *and* a real subroutine with a minimum of code
duplication.  I originally used this technique to avoid depending on
Filter::cpp and a C preprocessor, but it also has the nice side effect of
creating debugger friendly macros.

I'm sure someone can figure out an equivalent techique for C.


-- 
This sig file temporarily out of order.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About