in reply to JA(C|P)H

This uses a few tricks to convert C into valid Perl. You can also do some tricks in the opposite direction using #define. Unfortunately you're limited by the fact that $ and (I think) @ are not valid characters in C code. However, gcc allows them so if you don't mind restricting the C code to gcc compilation you have more freedom.
#include <stdio.h> main() #ifdef __PERL__ ;sub main {} my $i; sub i:lvalue {$i} #endif { int i; for(i = 0; (i) < 10; (i)++){ printf("%d\n",i); } }

Replies are listed 'Best First'.
Re^2: JA(C|P)H
by ktross (Deacon) on Mar 23, 2005 at 13:35 UTC


    Thanks, this is exactly the sort of thing I was looking for. I really like the fact that you defined main as a sub. That hadn't occured to me. Using #define had crossed my mind, but I decided not to use it this time.

    What really intreges me about this example is the line: 'sub i:lvalue {$i}'. To be honest, this is the first time I've come across an lvalue sub. I know what it is doing, but not exactly how it is doing it. Documentation here I come!

    Thanks again, you've raised some interesting possibilities.