Though singled out for submitting  a whopping 9 entries, I could
not help but notice that specialist obfuscator BooK submitted more.
Luckily for me, however, he did not find the strtol hack, for he
would surely have tried for a T-shirt by producing a "C is Perl"
entry, just as he did in the 4th Obfuscated Perl Contest:
http://www.perlmonks.org/index.pl?node_id=133971

As soon as I found the strtol solution, I immediately set about
producing a BooK-inspired "Perl-is-C" entry -- not wishing to waste
precious time with a mundane 'perldoc POSIX'. Before you question
my sanity, ask whether it is more "normal" to be grep-ing the
complete perl sources for "36" (like Ton) or sifting POSIX.pm
with a fine tooth comb (like Xavier), looking for something,
anything!, that might save a stroke. :)

Having already seen BooK's entry above, it proved to be a doddle.
I started by hacking BooK's entry, producing:

#include <stdio.h>
#include <stdlib.h>
#define strtol(a,b) strtol(a,0,b)
#define $ARGV argv
#define x : /*
               use POSIX;
               unshift @ARGV, 'Buffy'; "*/
main(int argc, char *argv[]) // ";
{
   printf("%lu\n", strtol($ARGV[1], 36));
}

I was then able to whittle a further 67 strokes from this,
finally producing:

#include <stdio.h>
#include <stdlib.h>
#define strtol(a,b) strtol(a,0,b)
#define $ /*
use POSIX;$argv[1]=pop;"*/
main(int argc,char *argv[]) //";
{
  printf("%ld\n",strtol($ argv[1],36));
}

The basic tricks used here are to hide the Perl code from C inside
a C /* */ comment, while hiding the C code from Perl inside a
void "..." string expression. Fortuitously, C pre-processor
directives start with the # Perl comment character, and so
don't require special treatment.

The line:
  printf("%ld\n",strtol($ argv[1],36));
is made valid in either language by the strtol macro and by
#define'ing $ to nothing. Some Perl novices may be surprised
to learn that white space is allowed between the $ and the
variable name.

I have tested this solution as both a C and C++ program on Windows
(MSVC6) and Linux (gcc) and it seems to work fine. So I can boast
that I have solved the problem in less than 200 strokes and you
have a choice of 3 different languages (Perl, C or C++). ^.^

If ever your boss asks you to write a program "in Perl or C", it
is worth considering the above program, so you might smugly answer
"both" when he asks you which language you used. :)

On seeing this entry, Jerome requested a Perl/Python/Cobol version,
which I leave as an exercise for the reader.

/-\ndrew

  • ... Andrew . Savige
    • ... Philip Newton
      • ... Peter Makholm
        • ... Philip Newton
      • ... Dmitry Kohmanyuk Дмитрий Кохманюк
        • ... Philip Newton
    • ... Andrew . Savige
      • ... Philip Newton

Reply via email to