grinder has asked for the wisdom of the Perl Monks concerning the following question:

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

  • Comment on Identical symbolic constants in XS and Perl

Replies are listed 'Best First'.
Re: Identical symbolic constants in XS and Perl
by diotalevi (Canon) on Jul 17, 2006 at 20:37 UTC

    If you name your constants file something with a .PL on the end, EU::MM will automatically run it for you. You could even generate your .pm directly this way.

    # my-constants.PL open my $pm, ">", "my-constants.pm" or die "Can't open my-constants.pm +: $!"; open my $h, ">", "my-constants.h" or die "Can't open my-constants.h: $ +!"; print { $pm } "# Generated content follows. Edit $0 instead.\n" or die "Can't write to my-constants.pm: $!"; print { $h } "/* Generated content follows. Edit $0 instead. */\n" or die "Can't write to my-constants.h: $!"; while ( my $const = <DATA> ) { chomp $const; my ( $name, $val ) = split ' ', $const, 2; print { $pm } "use constant '$name' => $const;\n" or die "Can't write to my-constants.pm: $!"; print { $h } "#define $name $const\n" or die "Can't write to my-constants.h: $!"; } print { $pm } "1; # End of generated my-constants.pm\n" or die "Can't write to my-constants.pm: $!"; print { $h } "/* End of generated my-constants.h */\n" or die "Can't write to my-constants.h: $!"; close $pm or die "Can't flush my-constants.pm: $!"; close $h or die "Can't flush my-constants.h: $!"; __DATA__ FOO 1 BAR 2 BAZ 3

    Produces my-constants.pm

    # Generated content follows. Edit my-constants.PL instead. use constant 'FOO' => 1; use constant 'BAR' => 2; use constant 'BAZ' => 3'; 1; # End of generated my-constants.pm

    And my-constants.h

    /* Generated content follows. Edit my-constants.PL instead. */ #define FOO 1 #define BAR 2 #define BAZ 3 /* End of generated my-constants.h */

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Identical symbolic constants in XS and Perl
by bsdz (Friar) on Jul 17, 2006 at 13:10 UTC