Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to include modules in different hierarchies?

Status
Not open for further replies.

whn

Programmer
Oct 14, 2007
265
US
Hi, Experts,

I wrote a few simple sample codes in a hierarchy shown below:

Code:
[b]% ls *[/b]
callingModuleFromTopLevel.pl*

subdir1:
callingModuleFromSubLevel.pl*  subLevelModule_1.pm*

subdir2:
subLevelModule_2.pm*

[b]% cat callingModuleFromTopLevel.pl[/b]
use lib "./subdir1";
use subLevelModule_1;
print "$0. In top level. Calling subLevelModule_1::func1().\n";
&subLevelModule_1::func1;

[b]% cat subdir1/callingModuleFromSubLevel.pl[/b]
use subLevelModule_1;
print "$0, In subdir1. Calling subLevelModule_1::func1().\n";
&subLevelModule_1::func1;

[b]% cat subdir1/subLevelModule_1.pm[/b]
package subLevelModule_1;
use lib "../subdir2";
use subLevelModule_2;
sub func1 {
    my $str = "subLevelModule_1.pm..func1() is called.";
    print "$str\n";
    print "subLevelModule_1.pm..func1() is calling ../subdir2/subLevelModule_2::func2()\n";
    &subLevelModule_2::func2;
}
1;

[b]% cat subdir2/subLevelModule_2.pm[/b]
package subLevelModule_2;
sub func2 {
    my $str = "subLevelModule_2.pm..func2() is called.";
    print "$str\n";
}
1;

I can only run callingModuleFromSubLevel.pl under subdir1:

Code:
[b]% perl callingModuleFromSubLevel.pl[/b]
callingModuleFromSubLevel.pl, In subdir1. Calling subLevelModule_1::func1().
subLevelModule_1.pm..func1() is called.
subLevelModule_1.pm..func1() is calling ../subdir2/subLevelModule_2::func2()
subLevelModule_2.pm..func2() is called.

However, neither callingModuleFromTopLevel.pl nor callingModuleFromSubLevel.pl can be invoked at the top level:

Code:
[b]% perl callingModuleFromTopLevel.pl[/b]
Can't locate [b][COLOR=red]subLevelModule_2.pm[/color][/b] in @INC (@INC contains: ../subdir2 ./subdir1 /usr/perl5/5.8.4/lib/sun4-solaris-64int /usr/perl5/5.8.4/lib /usr/perl5/site_perl/5.8.4/sun4-solaris-64int /usr/perl5/site_perl/5.8.4 /usr/perl5/site_perl /usr/perl5/vendor_perl/5.8.4/sun4-solaris-64int /usr/perl5/vendor_perl/5.8.4 /usr/perl5/vendor_perl .) at subdir1/subLevelModule_1.pm line 3.
BEGIN failed--compilation aborted at subdir1/subLevelModule_1.pm line 3.
Compilation failed in require at callingModuleFromTopLevel.pl line 2.
BEGIN failed--compilation aborted at callingModuleFromTopLevel.pl line 2.

[b]% perl subdir1/callingModuleFromSubLevel.pl[/b]
Can't locate [b][COLOR=red]subLevelModule_1.pm[/color][/b] in @INC (@INC contains: /usr/perl5/5.8.4/lib/sun4-solaris-64int /usr/perl5/5.8.4/lib /usr/perl5/site_perl/5.8.4/sun4-solaris-64int /usr/perl5/site_perl/5.8.4 /usr/perl5/site_perl /usr/perl5/vendor_perl/5.8.4/sun4-solaris-64int /usr/perl5/vendor_perl/5.8.4 /usr/perl5/vendor_perl .) at subdir1/callingModuleFromSubLevel.pl line 1.
BEGIN failed--compilation aborted at subdir1/callingModuleFromSubLevel.pl line 1.

How to fix these two problems?

Thanks.
 
You're running the script from the top level?

@INC on the relative paths (../subdir2 and ./subdir1) is going to be relative to your current working directory.

Try `pwd` to verify what path you're working in before you start the script.

Also:

Code:
(@INC contains: ../subdir2 ./subdir1 ...)

Make sure that "../subdir2/subLevelModule_2.pm" OR "./subdir1/subLevelModule_2.pm" exists. If they don't exist (relative to pwd of course), then Perl can't find them either.

If you're running this in the top directory where subdir1 and subdir2 are folders, then why does @INC contain "../subdir2", with two dots? This means "up one directory", surely you wanted 1 dot then?

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Ah, I see. I looked at the code again,

Your module in subdir1 wants to use lib "../subdir2" -- this is correct, if your working directory was in subdir1 -- but it isn't, your working directory is at "../" where both subdirs are in the current directory.

It doesn't matter where your Perl modules and scripts are located, the working directory is always the working directory you were in when you launched the first script. So even though subdir1's module was in a different folder, the working directory is still that of your top-level script.

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top