How to find directory of package residence from within package?

Discussion in 'Perl Misc' started by Krishna Chaitanya, Apr 2, 2009.

  1. 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...
    Krishna Chaitanya, Apr 2, 2009
    #1
    1. Advertising

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


    Er, read last line above as:

    use Cwd qw(getcwd abs_path);
    Krishna Chaitanya, Apr 2, 2009
    #2
    1. Advertising

  3. Krishna Chaitanya wrote:
    > 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//;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Apr 2, 2009
    #3

  4. > 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.
    Krishna Chaitanya, Apr 3, 2009
    #4
  5. Krishna Chaitanya

    J. Gleixner Guest

    Krishna Chaitanya wrote:

    > 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?
    J. Gleixner, Apr 3, 2009
    #5
  6. Krishna Chaitanya wrote:
    >> 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).


    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?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Apr 3, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KatB
    Replies:
    0
    Views:
    443
  2. Wybo Dekker
    Replies:
    1
    Views:
    352
    Yukihiro Matsumoto
    Nov 15, 2005
  3. Replies:
    4
    Views:
    183
    Jay Tilton
    May 12, 2005
  4. vdvorkin
    Replies:
    0
    Views:
    399
    vdvorkin
    Feb 10, 2011
  5. vdvorkin
    Replies:
    3
    Views:
    808
    vdvorkin
    Feb 14, 2011
Loading...

Share This Page