I've seen this question before. I'm going to give the same answer (3) None of the above...use a simple DB like a .INI file for this if there a bunch of them. As I said before, you can make a very thin layer for the MyConstants.pm module.

Having said that, alarm bells go off in my brain as to how many of these constants do you have? 10,50,100?

The reason that I'm asking is that in C, a lot of of these #define things have to do with a) memory limits (like array bounds..MAX_LINE_BYTES, etc) or b) stuff like thing "3" in this array means "name". Using Perl constants for either a or b is not the right Perl way.

So anyway, say you have a dozen of these things....the below code will do it. The code in your modules would look like use MyConstants(VAR1 VAR3); I LOVE C, but this is not C. You don't have to import any variable into your name space that you don't need. In C there will be hundreds if not thousands of vars imported into your compile name space via the .h files! In Perl, you can specify which vars you want to import, below this is specified by @EXPORT_OK. If you want all of them, then put them in the @EXPORT list (the default export/import list). Also, in Perl there is really no such thing as a truly "private variable". Even if VAR3 is not exported, it can still be accessed via $MyConstants::VAR3, assuming that $VAR3 has package scope which is what "our" does.

Anyway the below scheme should work fine for like a dozen vars.

#file MyConstants.pm use strict; use warnings; package MyConstants; use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); use Exporter; our $VERSION=0.1; our @ISA = qw(Exporter); our @EXPORT = qw(); #default export our @EXPORT_OK = qw(VAR1 VAR2 VAR3); #by request export our $VAR1 ="something"; our $VAR2 ="something_else"; our $VAR3 ="something_way_else"; 1;
Update:
I just read the ikegami post and it is completely correct as usual! Perl is not a "lightweight" language. I tried in my code to do something similar to what you would expect with a C .h file. I did not worry about whether some var is truly "constant" or not. I assume that you won't give these vars names or use them in a way that you wouldn't in a C program. Anyway "use MyConstants(VAR1); is most assuredly NOT the same as #include MyConstants.h

In reply to Re: Is "use" same as include? by Marshall
in thread Is "use" same as include? by Anonymous Monk

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.