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
(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/"
>> 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
>> (my $dir = $INC{'MyModule.pm'}) =~ s#/MyModule.pm$##;
>>
>> chdir $dir or die "could not cd to '$dir' $!";
>
> this cannot work and does not work.
You are wrong. It works.
Put the following code in your module and you will see it:
for my $key (sort keys %INC) {
print "$key => $INC{$key}\n";
}
Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
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.
> 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
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
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
> (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,
> 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
You could use the __FILE__ macro
[mysys]: cat xyz/ABC.pm
package xyz::ABC;
print __PACKAGE__, " is in ", __FILE__, "\n";
1;
[mysys]: perl -I`pwd` -Mxyz::ABC -e ''
xyz::ABC is in /home/myhome/xyz/ABC.pm
--
Just because I've written it doesn't mean that
either you or I have to believe it.