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

Perl to generate code in other languages

by sg (Pilgrim)
on Oct 31, 2007 at 16:28 UTC ( #648294=CUFP: print w/replies, xml ) Need Help??

I use perl to write code in other languages such as C and assembly. The core of the technique is the embedding of perl code in a perl string via

"perl string @{[eval{ <code> }]} perl string"
The technique is illustrated in the code below. I have also used this technique to do spread-sheet type calculations (replacement for Excel).

Languages such as Verilog and VHDL require the user to type the same information in different forms in multiple places -- a good use for perl would be to type the essential information once and have perl reproduce it in different forms and in different places as required by the Verilog/VHDL being generated. But I haven't coded in these languages in a while and so don't have an example to provide here.

A recent post indicated that others too make of perl for such ends. So here's a thread for sharing techniques used for similar ends.

#!/usr/bin/perl use warnings; use strict; $| = 1; # template to illustrate using perl to generate files # in other languages such as C # in the editor, good to set filetype to c (rather than perl) my $a_file_c = ''; my $copyright = ''; $copyright =<<here_doc; #{{{3 /* **** blah blah ********************************************************* */ here_doc #}}} # --- $a_file_c =<<here_doc; #{{{3 $copyright #include <stdio.h> int main( ) /* {{{3 */ { int rc; printf("Hello World!\n"); here_doc # }}} # --- # pure perl code my $key_var = 'a_key_c_var'; # next block of c-code has perl executable code inside it # the perl code is captured in $stuff and then inserted # into $a_file_c $a_file_c .=<<here_doc; #{{{3 for(int foo=0; foo<1000; foo++) { @{[eval{ my $stuff=''; foreach(0, 2) { $stuff .= "#ifdef DO_WORK_"."$_\n"; $stuff .= " $key_var = $_;\n"; $stuff .= " $key_var = $key_var ** $_;\n"; $stuff .= "#endif\n"; } $stuff }]} // more C-code while (int j > 0) { must_do = some_more + thing; } } // end main here_doc #}}} # --- my $foo = ''; $foo = 'a_file.c'; open (OUT, ">$foo") or die "Unable to open $foo for writing:$!\n"; binmode OUT; print OUT $a_file_c; close OUT; __END__

Replies are listed 'Best First'.
Re: Perl to generate code in other languages
by spectre9 (Beadle) on Nov 08, 2007 at 06:28 UTC
    "Beginnerís Mind is the key behind the phenomenon of Beginnerís Luck: a person doing something for the first time often does it much better than he does after heís practiced for a while. Because he tries more approaches, and tries them rapidly, a person in Beginnerís Mind is more likely to succeed at a task than one who thinks he understands how it works." -- Arlo Banshee

    "In the beginner's mind there are many possibilities, but in the expert's there are few." -- Shunryu Suzuki

    Code generation goes hand-in-hand with Patterns. The basic premise is using automation to bypass busy work.

    My current job is lead ETL development on a multi-district "Public Education" (K-12)data warehouse. I'm using Perl to extract, generate DDL transformations, Oracle load processes and generate Cognos objects.

    Perl is my life-preserver. Some of my sources have, literally, 9-20k tables. I've abstracted, aggregated, profiled, translated, filtered, verified, converted, surrogated and documented, all with Perl code.

    ETL is more about operations than is is development. If the code still works, why bother refactoring it? Answer: You have a novice around! See: Principle of Lease Qualified Implementer.

    To those truly wanting to follow The Path, allow me to provide another forks in the road. K. Czarnecki, s. Helsen; Feature-based survey of model transformation approaches IBM Systems Jounal Vol 45 No 3, 2006. My mind exploded with ideas from this one!

    Code generation and patterns provide something useful and interesting to do at the office. I helps me to keep my "Beginner's Mind"

    I've recently signed up for PAUSE so that I can contribute some module code -- probably to SQL::Translater -- specifically for Oracle 9+. Its amazing how dated most Oracle support is within CPAN (Metadata API has been around for years...)

    Anyone else out there notice a similarity between Perl Monks and Scientology? Who's hiding all the secrets to make ETL easy? It this job security or religion?

    So, while I have not provided any concrete examples that I've grown, hopefully I can help "make the soil richer." Nice post -- looking forward to see what this captures.

    spectre#9 -- more dangerous than acme
      Anyone else out there notice a similarity between Perl Monks and Scientology? Who's hiding all the secrets to make ETL easy? It this job security or religion?

      Methinks some of those secrets are revealed in Data Munging with Perl and Pro Perl Parsing. ;-)


Re: Perl to generate code in other languages
by spectre9 (Beadle) on Dec 06, 2007 at 05:14 UTC
    I'm rather surprised (and its unfortunate) that your post did not... generate... any more replies.

    "Strictly speaking, there are no enlightened people, there is only enlightened activity." -- Shunryu Suzuki

Re: Perl to generate code in other languages
by missingthepoint (Friar) on Sep 05, 2008 at 11:50 UTC

    That was cool. Thanks. I feel inspired (or corrupted, I'm now thinking about combined C/Perl obfu :P)

    email: perl -e 'print reverse map { chr( ord($_)-1 ) } split //, "\x0bufo/hojsfufqAofc";'

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://648294]
Approved by Corion
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2023-02-04 06:27 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (31 votes). Check out past polls.