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__
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|