While writing some ELF header modules, I had a bad fit of Laziness, and decided that I didn't want to manually transfer the plethora of #define's over to my perl script. This quick hack strips comments and changes #define's to equivilant 'use constant' format.

cat /usr/include/linux/elf.h | perl def2const.pl >>Constants.pm

Much joy. Update: oops, forgot an extra + in there.

#!/usr/bin/perl -w use strict; map { print if s!#\s*define\s+(\S+)\s+([^(?:\s*/\*.*\*/)]+)$!use constant $ +1 => $2;!g; } <>;

Replies are listed 'Best First'.
RE: Converting C #define to 'use constant' statements
by btrott (Parson) on Nov 03, 2000 at 04:42 UTC
    Useless use of cat, as merlyn would (will?:) say.

    Try this:

    perl -ne 'print if s!#\s*define\s+(\S+)\s+([^(?:\s*/\*.*\*/)])$!use co +nstant $1 => $2;!g' /usr/include/linux/elf.h >> Constants.pm

      No need to rewrite the script to kill one cat:

      perl def2const.pl /usr/include/linux/elf.h >>Constants.pm

              - tye (but my friends call me "Tye")
        Yes, but part of my point--no, I didn't mention it :)--was that the original script was reading the entire input into memory. The slight rewrite I did doesn't do that. And I realize that, w/ an include file, it's probably not that much of an issue; but I don't like the idea of reading an entire file into memory when it could be done more easily as a filter.