Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (12)
    As of 2014-11-28 09:58 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (195 votes), past polls