develooper Front page | perl.fwp | Postings from November 2001

Practical Perl Golf

Thread Next
Michael G Schwern
November 14, 2001 17:52
Practical Perl Golf
Message ID:
I have a practical golfing challenge for you all.  I need two portable

    1) Strips all comment-only lines from a Perl program (ie. /^\s*#/)
       except those which are C pre-processor commands.  (It's not
       necessary to worry about if you're inside a string or not).

    2) Same as 1, except it also strips everything before the first 
       #! line.

And I need them as small as possible.  80 characters would be nice.

Here's working versions of #1 and #2 respectively.  They're both too

perl -ne "print if (!/^\s*#/ || /^\s*#\s*(include|define|if|ifdef|ifndef|else|elif|undef|endif)/)" your_file

perl -ne "$saw_bang++ if /^#!.*(perl|PERL)/; print if $saw_bang && (!/^\s*#/ || /^\s*#\s*(include|define|if|ifdef|ifndef|else|elif|undef|endif)/)" your_file

The full story is I'm trying to get -Px working on VMS.  For those
that don't know, -P is "run my program through the preprocessor before
executing", -x is "strip off all the crap before the first #!.*perl
line".  Getting it working on VMS is a special challenge because the
command buffer is a mere 256 characters.

Here's how it currently works.  If you invoke perl -P your_file
Perl executes the following (on Linux).

/bin/sed    -e '/^[^#]/b'  
            -e '/^#[         ]*include[      ]/b'  
            -e '/^#[         ]*define[       ]/b'  
            -e '/^#[         ]*if[           ]/b'  
            -e '/^#[         ]*ifdef[        ]/b'  
            -e '/^#[         ]*ifndef[       ]/b'  
            -e '/^#[         ]*else/b'  
            -e '/^#[         ]*elif[         ]/b'  
            -e '/^#[         ]*undef[        ]/b'  
            -e '/^#[         ]*endif/b'  
            -e 's/^[         ]*#.*//'  your_file | 
cc -E -C -I/usr/local/perl/lib/5.6.1 -

Yep, it calls sed.  Ironic, no?

sed is just stripping out /^\s*#/ lines which are not C pre-processor
commands, otherwise the CPP will get confused.  It then pipes the
cleaned code off to your C pre-processor.

Two problems with this.  One is not everyone has sed.  Second is
that's way more than 256 characters and it overflows VMS's buffer.

So we replace the sed call with a call to perl!  Two birds with one
stone.  Unfortunately, what I've got is still too long.

There's another complication.  When -x is used with -P you have to
ignore everything until you see the first /^#!.*(perl|PERL)/ line.

The life-support necessary to run perl and the C pre-processor under
VMS eats up about 120 characters, plus the filename which could eat
anywhere from 1 to 100 characters.  This doesn't leave much room to


Michael G. Schwern   <>
Perl6 Quality Assurance     <>	     Kwalitee Is Job One
Is there an airport nearby or is that just my tae-kwon-do taking off?

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About