module needs to know its own path

M

Marten Lehmann

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
 
T

Tad J McClellan

Marten Lehmann said:
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' $!";
 
M

Marten Lehmann

Hello,
(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
 
X

xhoster

Marten Lehmann said:
Hello,


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

Marten Lehmann

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
 
G

Gunnar Hjalmarsson

Marten said:
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.

#!/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.
 
C

comp.llang.perl.moderated

Hello,


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
 
M

Michele Dondi

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

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

(Just to be *very* fussy!)


Michele
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top