module needs to know its own path

Discussion in 'Perl Misc' started by Marten Lehmann, Mar 6, 2008.

  1. Hello,

    within a perl module, I need to access content included with this
    module, but stored in separate files (WSDL definitions in my case).

    If my module lies in /usr/lib/perl5/xxx/MyModule.pm, the WDSL files
    could be stored in /usr/lib/perl5/xxx/MyModule/WSDLs/*.wsdl or similar.

    But how can the perl module find out where it has been loaded from? The
    files I have to access would always be relative to the perl module. But
    that doesn't help much, as the working directory within the perl module
    for a perl script in /test/script.pl would always be /test and not the
    path to the script. So reading from ./WSDLs/*.wsdl would fail.

    Any ideas?

    Regards
    Marten
     
    Marten Lehmann, Mar 6, 2008
    #1
    1. Advertising

  2. Marten Lehmann <> wrote:
    > Hello,
    >
    > within a perl module, I need to access content included with this
    > module, but stored in separate files (WSDL definitions in my case).
    >
    > If my module lies in /usr/lib/perl5/xxx/MyModule.pm, the WDSL files
    > could be stored in /usr/lib/perl5/xxx/MyModule/WSDLs/*.wsdl or similar.
    >
    > But how can the perl module find out where it has been loaded from? The
    > files I have to access would always be relative to the perl module. But
    > that doesn't help much, as the working directory within the perl module
    > for a perl script in /test/script.pl would always be /test and not the
    > path to the script. So reading from ./WSDLs/*.wsdl would fail.
    >
    > Any ideas?



    (my $dir = $INC{'MyModule.pm'}) =~ s#/MyModule.pm$##;

    chdir $dir or die "could not cd to '$dir' $!";


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Mar 6, 2008
    #2
    1. Advertising

  3. Hello,

    >> within a perl module, I need to access content included with this
    >> module, but stored in separate files (WSDL definitions in my case).
    >>
    >> If my module lies in /usr/lib/perl5/xxx/MyModule.pm, the WDSL files
    >> could be stored in /usr/lib/perl5/xxx/MyModule/WSDLs/*.wsdl or similar.
    >>
    >> But how can the perl module find out where it has been loaded from? The
    >> files I have to access would always be relative to the perl module. But
    >> that doesn't help much, as the working directory within the perl module
    >> for a perl script in /test/script.pl would always be /test and not the
    >> path to the script. So reading from ./WSDLs/*.wsdl would fail.
    >>
    >> Any ideas?

    >
    > (my $dir = $INC{'MyModule.pm'}) =~ s#/MyModule.pm$##;
    >
    > chdir $dir or die "could not cd to '$dir' $!";


    this cannot work and does not work. I asked how can the perl module find
    out where it has been loaded from, but your code does only work within
    the script that has loaded the module. But the perl module on its own
    needs to know the path it is stored.
    And the perl module doesn't have to be installed within the standard
    perl directories necessarily. Example:

    use lib "/my/own/dir";
    use MyModule;

    The MyModule module shall be able to get the path /my/own/dir somehow.

    Regards
    Marten
     
    Marten Lehmann, Mar 6, 2008
    #3
  4. Marten Lehmann

    Guest

    Marten Lehmann <> wrote:
    > Hello,
    >
    > >> within a perl module, I need to access content included with this
    > >> module, but stored in separate files (WSDL definitions in my case).
    > >>
    > >> If my module lies in /usr/lib/perl5/xxx/MyModule.pm, the WDSL files
    > >> could be stored in /usr/lib/perl5/xxx/MyModule/WSDLs/*.wsdl or
    > >> similar.
    > >>
    > >> But how can the perl module find out where it has been loaded from?
    > >> The files I have to access would always be relative to the perl
    > >> module. But that doesn't help much, as the working directory within
    > >> the perl module for a perl script in /test/script.pl would always be
    > >> /test and not the path to the script. So reading from ./WSDLs/*.wsdl
    > >> would fail.
    > >>
    > >> Any ideas?

    > >
    > > (my $dir = $INC{'MyModule.pm'}) =~ s#/MyModule.pm$##;
    > >
    > > chdir $dir or die "could not cd to '$dir' $!";

    >
    > this cannot work and does not work.


    It works for me. Did you actually try it?

    > I asked how can the perl module find
    > out where it has been loaded from, but your code does only work within
    > the script that has loaded the module.


    %INC is a global variable. It is available from any scope, including
    the scope of modules. (And apparently it is updated before
    the module is compiled, which I did have some initial concerns about.)


    > But the perl module on its own
    > needs to know the path it is stored.


    A perl module *on its own* is just a text file. It can't "know" anything.


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
     
    , Mar 6, 2008
    #4
  5. Hello,

    > You are wrong. It works.


    ok, it works as long as perl module and script are separate files. For
    testing, I was putting both in one file and then it didn't work. But
    thats not a problem as I was definetely plannung to separate both.

    #!/usr/bin/perl
    package Test;
    use strict;
    use Data::Dumper;

    sub new {
    my ($class) = @_;
    my $self = {};
    bless ($self, $class);

    print Dumper(%INC);

    return $self;
    }

    my $t = Test->new();

    Regards
    Marten
     
    Marten Lehmann, Mar 6, 2008
    #5
  6. Marten Lehmann wrote:
    > Frank Seitz wrote:
    >> You are wrong. It works.

    >
    > ok, it works as long as perl module and script are separate files.


    Aren't they always?

    > For testing, I was putting both in one file and then it didn't work.


    <snip>

    > #!/usr/bin/perl
    > package Test;
    > use strict;
    > use Data::Dumper;
    >
    > sub new {
    > my ($class) = @_;
    > my $self = {};
    > bless ($self, $class);
    >
    > print Dumper(%INC);
    >
    > return $self;
    > }
    >
    > my $t = Test->new();


    Declaring a package in a script does not make that package a module.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Mar 6, 2008
    #6
  7. On Mar 6, 10:11 am, Marten Lehmann <> wrote:
    > Hello,
    >
    > > You are wrong. It works.

    >
    > ok, it works as long as perl module and script are separate files. For
    > testing, I was putting both in one file and then it didn't work. But
    > thats not a problem as I was definetely plannung to separate both.
    >
    > #!/usr/bin/perl
    > package Test;
    > use strict;
    > use Data::Dumper;
    >
    > sub new {
    > my ($class) = @_;
    > my $self = {};
    > bless ($self, $class);
    >
    > print Dumper(%INC);
    >
    > return $self;
    >
    > }
    >
    > my $t = Test->new();
    >


    If you're trying to create a runnable module,
    you can use caller() to get the invoking filename:

    my( $pkg, $file, $line ) = caller;

    See B.Foy's "modulino" for an example:

    http://www252.pair.com/comdog/mastering_perl/Chapters/18.modulinos.html

    --
    Charles DeRykus
     
    comp.llang.perl.moderated, Mar 6, 2008
    #7
  8. On Wed, 5 Mar 2008 19:29:01 -0600, Tad J McClellan
    <> wrote:

    > (my $dir = $INC{'MyModule.pm'}) =~ s#/MyModule.pm$##;


    (my $dir = $INC{'MyModule.pm'}) =~ s#/MyModule\.pm$##;
    ^
    ^

    (Just to be *very* fussy!)


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Mar 6, 2008
    #8
  9. On Thu, 06 Mar 2008 19:21:53 +0100, Gunnar Hjalmarsson wrote:

    > Marten Lehmann wrote:
    >> Frank Seitz wrote:
    >>> You are wrong. It works.

    >>
    >> ok, it works as long as perl module and script are separate files.

    >
    > Aren't they always?


    Oh, heavens no. Although problems like this indicate why it *is* a good
    idea to separate them.

    M4
     
    Martijn Lievaart, Mar 6, 2008
    #9
    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. Saverio M.
    Replies:
    0
    Views:
    557
    Saverio M.
    Jul 3, 2006
  2. kj
    Replies:
    11
    Views:
    528
    Gabriel Genellina
    Oct 31, 2009
  3. thunk
    Replies:
    1
    Views:
    359
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    536
    thunk
    Apr 1, 2010
  5. Bart Van der Donck

    Own module needs loaded module from main script

    Bart Van der Donck, Aug 25, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    142
    Gunnar Hjalmarsson
    Aug 27, 2004
Loading...

Share This Page