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