I'm writing some XS to expose O/S datastructures to Perl. I have the need to abstract away certain things (numbers and strings) behind symbolic names. So I have a file that looks like

FOO  1
BAR  2
QUUX 3

It is a trivial matter to produce #define FOO 1 and use constant FOO => 1; and create the appropriate C header file or Perl file to be required, and I do so in Makefile.PL. But I have a couple of issues.

If I add another mapping to the above file, I have to rerun Makefile.PL to rebuild the header files. It would be nice for the resulting Makefile to know about these dependencies.

Using a C header file for the XS code is natural enough, since it won't be needed once the XS file has been compiled. On the other hand, having to require the corresponding .pl file is sub-optimal. What I really want to do is to insert it directly into the .pm file. (Bear in mind that these constants are not exposed to the client code. They are simply there to make sure that the C and Perl and talking about the same things).

So I was toying with a sort of template approach, that gets filled in with the generated use constant lines to produce the .pm file. Surely somebody has already encountered (and solved) this problem before. So, how did you do it?

Thanks

• another intruder with the mooring in the heart of the Perl


In reply to Identical symbolic constants in XS and Perl by grinder

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.