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__


In reply to Perl to generate code in other languages by sg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.