Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

Comment on Dual language
Select or Download Code
Re: Dual language
by davido (Archbishop) on Sep 22, 2012 at 08:45 UTC

    This is creative and fun.

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

    Why?

    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 :)

    Dave

Re: Dual language
by eyepopslikeamosquito (Canon) 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 http://www.perlmonks.org/?node_id=133308. I don't understand why use  #ifdef _FSTDIO where a simple #if 1 would also do the same.
Re: Dual language
by choroba (Abbot) 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2014-08-29 19:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (287 votes), past polls