Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Dual constant files

by stephen (Priest)
on Mar 13, 2001 at 06:44 UTC ( [id://64048]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings all...

I have a system where I have a constants class in Perl like so:

package FooConstants; ## ISA exporter and so on... use constant TYPE_BAR => 'bar'; use constant TYPE_BAZ => 'baz'; ## etc...
Which is fine... the constants are handy, self-documenting, and keep me from making typos. Problem: I have this Java interface class they have to stay in sync with, like so:
// Note-- Java code. Heresy! Heresy! public interface FooConstants { public static final String TYPE_BAR = "bar"; public static final String TYPE_BAZ = "baz"; //... etc... }
I'm expecting to have to add types to these classes on a relatively frequent basis, but the two have to stay in sync or chaos will ensue. How can I keep the data for both the files in the same place, so I don't run the risk of them going out of sync? I can think of a couple of ways to do it:
  • I can maintain a list of all my constants in some independent format-- XML maybe, or just a text file-- and generate both my Java class and my Perl module from a Makefile (or Ant equivalent.)
  • I could keep the list in independent format as above, but have the Perl module autogenerate the constants it needs from that file directly. I'd need to reengineer the Java stuff to do the same thing, meaning replacing my constants with method calls.
  • I could write a Java antfile function to generate the Perl module from the Java class.
  • I could write a Perl script to generate the Java class from the Perl module.
  • I could delegate the whole thing to somebody else.

What's the best way to handle this? I just know there's a trap in here someplace, and I'm doing my best to avoid it. Any advice would be greatly appreciated.

stephen

Replies are listed 'Best First'.
Re: Dual constant files
by Adam (Vicar) on Mar 13, 2001 at 06:54 UTC
    I agree that you only want one constants file. Since Java is compiled, you can add a compiler step to generate the java constants file off of your Perl one. You could even write the code to do that in Perl.

    Alternatively, you can write your constants in Java and write a constants module in Perl which uses AUTOLOAD to cull them from the java file as needed. This is a particularly cool way to do it (in my opinion) and it is a great use for AUTOLOAD. I actually did something similar for a company, where I had a hash table of constants and an AUTOLOADer to convert them to subroutines as needed. But watch out for non-scalar constants... arrays and hashes don't play as nice with eval.

    Search for more on AUTOLOAD

Re: Dual constant files
by Tyke (Pilgrim) on Mar 13, 2001 at 14:02 UTC
    The way I tend to handle this sort of problem is to keep the constants stored in a single independent store (xml for example), and use that information to generate the constants file in both the Perl and Java systems as part of the build process.

    You might have a look at the Text::Template module to help generate the appropriate files.

    Failing that, the delegation suggestion is good too ;-)

Re: Dual constant files
by Desdinova (Friar) on Mar 13, 2001 at 09:19 UTC
    This is kind of a tough one. Persoanlly i would say do it the way you are most comfortable. I know perl metter than java or Make so I would prolly write the tool in perl, but that is because Of what I know. Another consideration is what the other poeple who you are working with are comfortable with, if that it is an issue. It really is a maater of what will you be able to best maintain with the least effort. PS. I'm glad to know that i'm not the only one who thinks constants are a good thing

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://64048]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-19 18:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found