How to find directory of package residence from within package?

  • Thread starter Krishna Chaitanya
  • Start date
K

Krishna Chaitanya

Hi, in case of a package, I need to populate the 'pwd' key of $self
with the directory of the same package's residence. I used Cwd but it
doesn't work in all cases. For example:

/home/kris/1.pl (WIP is an immediate subdirectory of this script's
directory)
==========================================================
use warnings;
use strict;
use WIP::Test;
my $obj = WIP::Test->new();
print $obj->pwd,"\n";

Package WIP/Test.pm
========

package WIP::Test;
use warnings;
use Cwd qw(getcwd);

sub new
{
my $pkg = shift;
my $classname = ref $pkg || $pkg;
my $self = {};
$self->{pwd} = getcwd();
bless $self, $classname;
return $self;
}

sub pwd
{
my $self = shift;
return $self->{pwd};
}

1;

In this case, if I run the 1.pl script from /home/kris, it gives /home/
kris rather than /home/kris/WIP (which is what I want). I want the
module to "remember" where it is located (without hard-coding it since
the module could be moved around in a flexible install)

Do you know how it can done easily using any special variable or
technique? I've thought about one way (which doesn't sound very
efficient to myself):

package WIP::Test;
use warnings;
use Cwd qw(getcwd);

my $pwd = undef;

use File::Find;
find(
{
wanted => sub {
my $pkg = __PACKAGE__;
$pkg =~ s|::|/|g;
$pkg = $pkg . ".pm";
if (/$pkg$/) {
$pwd = abs_path($File::Find::dir);
}
},
no_chdir => 1,
},
@INC
);

sub new
{
....
....
....

Regards...
 
G

Gunnar Hjalmarsson

Krishna said:
Hi, in case of a package,

s/package/module/

(a module may include multiple packages)
I need to populate the 'pwd' key of $self
with the directory of the same package's residence. I used Cwd but it
doesn't work in all cases.

One way may be to make use of the %INC hash.

( my $pwd = $INC{'WIP/Test.pm'} ) =~ s/Test\.pm//;
 
K

Krishna Chaitanya

One way may be to make use of the %INC hash.

     ( my $pwd = $INC{'WIP/Test.pm'} ) =~ s/Test\.pm//;

That gives only relative path (i.e. WIP). I've slightly modified it
as:

package WIP::test::abc;

use warnings;
use strict;

use Cwd qw(abs_path);
my $pwd = undef;

BEGIN {
my $pkg = __PACKAGE__;
$pkg =~ s|::|/|g;
$pkg = $pkg . ".pm";
( $pwd = abs_path($INC{$pkg}) ) =~ m|^(.+)/|;
$pwd = $1;
print "Module is located at $pwd\n";
}

Thanks, Gunnar.
 
J

J. Gleixner

Krishna said:
That gives only relative path (i.e. WIP). I've slightly modified it
as:

Just curious. Why do you need to know the full path to the
module?
 
G

Gunnar Hjalmarsson

Krishna said:
That gives only relative path (i.e. WIP).

Not on my Perl installations, and I would be very surprised if that was
the case for you.

$ perl -MCwd -le 'print "$_ = $INC{$_}" for keys %INC'
XSLoader.pm = /usr/lib/perl5/5.10.0/i686-linux/XSLoader.pm
warnings.pm = /usr/lib/perl5/5.10.0/warnings.pm
warnings/register.pm = /usr/lib/perl5/5.10.0/warnings/register.pm
Cwd.pm = /usr/lib/perl5/5.10.0/i686-linux/Cwd.pm
vars.pm = /usr/lib/perl5/5.10.0/vars.pm
Exporter.pm = /usr/lib/perl5/5.10.0/Exporter.pm
strict.pm = /usr/lib/perl5/5.10.0/strict.pm
$

What does that code output for you?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top