Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: current path in a package?

by samarzone (Pilgrim)
on Dec 28, 2010 at 08:06 UTC ( #879396=note: print w/replies, xml ) Need Help??

in reply to current path in a package?

If you are using require with a physical path to the module file you already know the path. If you are concerned with absolute path rather than relative one you can use getcwd() prepended to the relative path mentioned in the require statement.

If you will use the module rather than requireing it you will find absolute path of module file into %INC hash variable with key as the module name.

use lib '/www/somedir/' use test; print $INC{test};
- Samar

Replies are listed 'Best First'.
Re^2: current path in a package?
by ELISHEVA (Prior) on Dec 28, 2010 at 08:34 UTC

    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: => '/usr/share/perl/5.8/' => '/usr/share/perl/5.8/' List/ => '/usr/lib/perl/5.8/List/'

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://879396]
[MidLifeXis]: Although the previous one could be a b0rken PATH, I would need to dig for that.
[thezip]: I've got to go to meetings now. If anyone has further comments regarding Spreadsheet::XLSX deployment to Strawberry Perl 5.24.1, please /msg me -- thanks!
LanX has to go ... plans to crash with a car into a group of pythonistas while screaming "LARRY IS THE GREATEST"
LanX ... darn ... where is my car?
[choroba]: Haven't you crashed into a boar recently?
[1nickt]: TCLion how is that going?
[LanX]: could someone lend me a car?

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2017-03-23 17:30 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (291 votes). Check out past polls.