http://qs321.pair.com?node_id=648294

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__