Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Dual language

by smeezekitty (Novice)
on Sep 22, 2012 at 05:26 UTC ( #995043=obfuscated: print w/replies, xml ) Need Help??

I hope this is the right place for this. I have been messing with perl a bunch today a managed to take advantage of how Perl and C use different comment characters. Here are two methods to write a program that runs as either C or Perl:
#include <stdio.h> // prints 99 Bottles of beer on the wall. Works as +either a C or a Perl program. #define PERL C /* "*/ int main(){//"; { int $bottles; $bottles=99; while($bottles){ printf("%d bottle%s of beer on the wall, %d bottle%s of beer.\ +n", $bottles, $bottles==1?"":"s", $bottles, $bottles==1?"":"s"); printf("Take one down and pass it around, "); if($bottles==1){ printf("no more bottles of beer on the wall.\n\n"); } else{ printf("%d bottle%s of beer on the wall.\n\n", $bottles-1, + $bottles==2?"":"s"); } $bottles--; } printf("No more bottles of beer on the wall, no more bottles of be +er.\n"); printf("Go to the store and buy some more, 99 bottles of beer of t +he wall."); }
and using a bit different method:
#include <stdio.h> #define sub void #define $x char *$t #define chomp gets sub main(){ $x; printf("Please enter your name: "); #if 0 $t = <>; #endif chomp($t); printf("Hello, %s!", $t); } #if 0 main(); #endif

Replies are listed 'Best First'.
Re: Dual language
by davido (Cardinal) on Sep 22, 2012 at 08:45 UTC

    This is creative and fun.

    I think you need to meet Inline::CPR. From its POD:


    Several reasons, (none great).

    1. It's a cute Perl trick. After explaining CPR to an uninitiated friend, he said "My head feels like it's been wrapped around a brick". That should be reason enough :)


Re: Dual language
by eyepopslikeamosquito (Chancellor) on Sep 22, 2012 at 21:05 UTC

    From Perl Monks circa 2001: BooK's C is Perl response to Ovid's Perl is C.

    Jerome Quelin show-cased an octo-lingual! (perl, c, c++, befunge, brainf**k, python, ook, html/javascript) program that computed the Fibonacci series in a YAPC::Europe 2003 talk. (If anyone has a copy of this program, please let us know).

    The above info was got from the "Multi-Lingual" section of The Lighter Side of Perl Culture (Part III): Obfu.

      Wow that is impressive. I discovered the first example also compiles as C++ and the second example also does if I change void to int. I guess that would make it tri-lingual? Would love to see an 8 language code though. BTW I actually created these without seeing the other C/Perl codes. I didn't even know about them. Also in I don't understand why use  #ifdef _FSTDIO where a simple #if 1 would also do the same.
Re: Dual language
by choroba (Bishop) on Sep 22, 2012 at 21:41 UTC
    See also The Best Ever for a Perl/Postscript programme.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Dual language
by ambrus (Abbot) on Oct 09, 2012 at 20:52 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: obfuscated [id://995043]
Approved by Athanasius
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2019-08-23 04:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found