Well, you can iterate over @INC, or directly evaluate $INC{'App/Package.pm'}, or use __FILE__ as already suggested. But I'm aware that you are looking for a more secure method, and here it can get murky.

If you have relative paths in @INC you must assert that your code did not do a chdir between loading the library and reading the config file, or you might either not find your config file, or read some other file which per accident (or per malicious action) has the relative path you are using after changing the directory.

I would also consider whether putting a configuration file in the library path is a good idea. If the contents of the config file never change, then you have the option of putting them in a __DATA__ section of a config module, and have get_config() reading from the DATA file handle in that module. If, however, the contents may change over time, then you need to grant write permission to a file within the library path, which doesn't play well with the module installers and may create issues when upgrading the application.

I recommend to check out CPAN's File::HomeDir module, which has functions like my_dist_data and my_dist_config to obtain paths to directories for data and configuration files. They are outside the library path, though, so your installation / initialisation steps need to be adjusted accordingly.


In reply to Re: Path relative to package by haj
in thread Path relative to package by DreamT

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.