Jim
Jim

Reputation: 19552

How can I get the path of a module dynamically?

If I have something like this:

In some script.pl:

#code here  
my $result = $class->foo();  
# extra code here  

In the module of the class:

package Test::A::Foo;  
use parent Test::A;  

sub foo {  
 # code here  
}  

How can I get the exact file path where Test::A::Foo resides when I am executing code inside the module? It is not the working directory as I am running the script.pl and that calls the $class->foo, so how can I get the actual path?

Upvotes: 3

Views: 81

Answers (1)

Borodin
Borodin

Reputation: 126722

The built-in hash %INC contains the resolved paths to module files that have been loaded using require or do

The location of the pm file for Test::A::Foo is in $INC{'Test/A/Foo.pm'}

Something like

$INC{ (__PACKAGE__ =~ s|::|/|gr) . '.pm' }

should do what you need

Upvotes: 6

Related Questions