in reply to Re: current path in a package?
in thread current path in a package?

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};
.