Interesting idea. I have certainly written Perl code that generates .h files that a C/C++ program uses. I see no reason why it couldn't go the other direction?

So one way would be to modify the "make file" for your C program so that if the .h file has changed, it creates a new Perl .pm module file (that your main Perl program "uses"). You do this by writing a Perl program that reads the .h file and writes Perl code!

I personally would be thinking in the direction of a module like this rather than just another .pl file that is executed at the start of your Perl's main .pl file. You can make it very clear what is "derived from the .h file" and that would be the purpose of this Perl module - defining constants that the C program knows about.

Hope this makes sense to you.

Write a Perl program that is one of the "make targets" in the make file. That Perl program writes a Cinterface.pm module file. That file is "used" by the PerlA.pl file when it runs. If you redefine #define MAX_FILES = 20 in the .h file, then building the C program causes the Perl program to also understand that: use constant MAX_FILES => 20; and that change goes into a new version of the .pm file.

Of course instead of just running your PerlA.pl program, you need to run the "make" file of the C program first. The Perl program depends upon the .h constants of the C program. The syntax of this enum stuff can all be taken care of. Perhaps the "OH!" moment for you is that you need to write a Perl program that writes Perl code! And that Perl program is a target in the "make" file. If the .h file changes, then the Perl Cinterface.pm module (program generated) changes and therefore the .pl program that runs has been modified.


In reply to Re: Using C-style .h as constants by Marshall
in thread Using C-style .h as constants by gri6507

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.