Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I have a Perl module that I'd like to link a config file to. This was slightly trickier than I initially anticipated since I'd like to keep it as portable as possible such that no matter where the module is located it always looks for its config file in the same directory. Piece of cake if I'm calling the module directly or from scripts in the same path, but when I use it in a script located in a different directory it looks for the module's config file in the running script's directory rather than the module's directory.

I'd rather not fiddle with @INC just for a config file. So I came up with something like this:

#! perl -w package mylib; my ($MODULE_PATH, $CONF_PATH); ($CONF_PATH = __FILE__) =~ s/pm$/conf/i; use constant { MODULE_PATH => __FILE__, CONF_PATH => $CONF_PATH }; print CONF_PATH;

This code warns me that the constant is uninitialized in the print statement. Hm. So things must be happening in a different order than I thought at compile-time, right?

So my next stab at it:

#! perl -w package mylib; my ($MODULE_PATH, $CONF_PATH); # assign value at compile-time before constants BEGIN { ($CONF_PATH = __FILE__) =~ s/pm$/conf/i; } use constant { MODULE_PATH => __FILE__, CONF_PATH => $CONF_PATH, }; print CONF_PATH;

And now I'm getting what I want: a constant that contains the absolute path of my config file for later use with Config::General and also drops it in @EXPORT_OK in case some calling script ever needs to see it.

So I'm wondering what's really going on under the hood here. Looking at the docs, constants get set at compile-time and I guess since I declare the BEGIN block first it beats the constant declaration to the punch.

Anyway, just curious if this is a classic case of "I'm doing it wrong" or maybe get some tips/suggestions for an intelligent way to implement this.


In reply to BEGIN block and failure to initialize constants by temporal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found