Tilly suggested using __FILE__ because using cwd is not necessarily reliable. A script might have changed the current directory after it has been loaded. Additionally, a module can be loaded from any of the paths in @INC and may never have been reachable via the current directory in the first place.

Note: __FILE__ only works for packages defined in the current file. If you want to find the location of an arbitrary package, look up the package in %INC. %INC contains a set of entries like this:

Carp.pm => '/usr/share/perl/5.8/Carp.pm' vars.pm => '/usr/share/perl/5.8/vars.pm' List/Util.pm => '/usr/lib/perl/5.8/List/Util.pm'

In %INC the module name gets ".pm" tacked onto the end and all of the '::' are replaced by '/'. I believe this transformation is consistent across platforms, so to look up the location of an arbitrary package:

my $k = $module_name; $k =~ s/::/\//g; $k .= '.pm'; my $path = $INC{$k};
.

In reply to Re^2: current path in a package? by ELISHEVA
in thread current path in a package? 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.