Its often the case that when i write a script, i'll break the code into multiple files that are required by the 'main' file. I'm talking about situations where some of the broken out files arent modules, but they're separated for helping keep the code organized. I think this is a pretty common practice.
Anyway, sometimes i'll want to use the same constants across many of these required libraries. Is the best way to do this to create a package with all the constants in it, export them, and use the constants package in all the support libraries that need them?
I guess this method seems a bit cluttered. Heres a simple example:
# file MyConstants.pm
package MyConstants;
require Exporter;
use base qw(Exporter);
@EXPORT = qw(C1 C2);
use constant C1 => 'blah';
use constant C2 => 'foo';
1;
# file my_program.pl
#!/usr/bin/perl
use MyConstants.pm
require 'my_support_lib.pl';
...
# do something with the constants from MyConstants.pm
...
# file my_support_lib.pl
...
use MyConstants.pm;
...
# do something with the constants from MyConstants.pm
...
Is this a good way to accomplish this sort of thing?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.