in reply to Re: empty out C function bodies
in thread empty out C function bodies

perl -e 'undef $/; $ENV{f}=$ARGV[0]; $_=`cat -- "\$f"`;

Backquotes in scalar context already do what you want regardless of the contents of $/ and you can use $ARGV[0] directly instead of copying it to the environment, so that then becomes:

perl -e ' $_=`cat -- "$ARGV[0]"`;

You could also simplify that whole thing with the use of the -0 switch and the -p switch:

perl -0777pe's/^(\w+[^\n;]*?\([^\n;]*\n\{\n)[\s\S]+?\n(\}\n)/$1$2/mg' +FILE.c > FILE_MODIFIED.c

Replies are listed 'Best First'.
Re^3: empty out C function bodies
by jakobi (Pilgrim) on Oct 24, 2009 at 14:09 UTC
    1. Wrong. There's a very important reason for this idiom: you've forgotten the shell's interpolation (might be Unix specific, but it's nonetheless a DEADLY & EASILY EXPLOITABLE TRAP).
      !!!!Please do not do insecure shell invocations like $_=`cat "$ARGV[0]"` ever!!!!
      (unless you control each and every tenth of each bit of each filename character and shell word individually; in case you missed it, it's indeed a major pet peeve of mine. Why you ask: consider rm -rf /* ./* and reinstalls & restores all over the place in huge lans. I don't intend to try "overnight" bare-metal recovery on that order of magnitude, and neither should you)
    2. switches: indeed. But even if we start playing golf, I'm still rather partial to my personal one and only space between -e and the Perl scrap: I greatly fear that you'll win by default :).