Reputation: 9235
I have three files:
~/multiFindBinTest.pl:
use FindBin;
use lib "$FindBin::Bin/mod2";
use pack2;
~/mod1/pack1.pm
package pack1;
1;
~/mod2/pack2.pm
use FindBin;
use lib "$FindBin::Bin/../mod1";
use pack1;
package pack2;
1;
As you can see, base.pl uses pack2, which in turn uses pack1. However, this is a demonstration of how NOT to use the FindBin module: when base.pl is executed, pack2 will not be able to locate pack1, because it will retain the value of "$FindBin::Bin" that was gotten from base.pl.
So my question is simple: Is there a method in perl to "use" a module which "uses" another module, all based on paths relative to the file which does the "using"?
Upvotes: 2
Views: 391
Reputation: 3484
If you know all of the possible library roots, you can just add them on the command line:
perl -I~/mod1 -I~/mod2 myscript.pl
of you can add them to the PERL5LIB environment variable:
export PERL5LIB=~/mod1:~/mod2
Either method puts the directories on to the libaray search path.
Additonal info:
If you want the individual packages to "declare" where their dependencies live, Perl provides the 'lib' pragma:
use lib '/path/to/lib/directory';
Upvotes: 2
Reputation: 29854
You don't have that, but you could make your own.
package libr;
use strict;
use warnings;
use File::Spec;
sub import {
shift; # invoker
my ( @cands, @missed );
ARGS:
while ( @_ ) {
# Get the next argument from the queued candidates or from the
# arguments
my $raw_path
= my $path
= @cands ? shift @cands : shift
;
# We don't need to worry about this argument unless it has relative
# notation in it.
if ( index( $path, '::' ) > -1 ) {
# split it into parts
my ( $mod, $rest ) = split qr{(?:/(?:\.(?=/))?)+}, $path, 2;
$mod =~ s/^:://; # Allow for one-word relative nodes: 'Word::/';
# Move it from mod notation to file...
my ( $mod_path ) = map { s|::|/|g; $_ } $mod;
my %set;
while ( my $len = length $mod_path ) {
# Remember the more specific path first
$set{ $_ } ||= $mod_path
foreach
# for each key that qualifies, subtract the same
# number of characters from the end of the value
map { substr( $INC{ $_ }, 0, $len - length ) . $rest }
# test each key that it starts with our string
grep { substr( $_, 0, $len ) eq $mod_path }
keys %INC
;
}
continue {
# Check if our separator is in the mod path.
my $mark = rindex( $mod_path, '/' );
last if $mark == -1;
# move the unmatched part of the substring to the
# ending
substr( $rest, 0, 0, substr( $mod_path, $mark ));
# delete it from the path
substr( $mod_path, $mark ) = '';
}
my @sort_order
# We only want the first value...
= map { shift @$_ }
# sort by length of matching path first, then alphabetically
sort { $b->[2] <=> $a->[2] or $a->[1] cmp $b->[1] }
# store a collection of values for sorting:
# lowercase string and length of matching string
map { [ $_ => lc $_ => length $set{ $_ } ] }
keys %set
;
### Assemble list of candidates
@cands = ( @sort_order, map { "$_/$mod_path$rest" } @INC );
next ARGS;
}
# If the path exists
if ( -e $path ) {
# Store the canonical path
push @INC, File::Spec->canonpath( $path );
# And reset tracking arrays
@cands = () if @cands;
@missed = () if @missed;
}
elsif ( @cands ) {
# If we're trying out values, just remember the missed ones.
push @missed, $path;
}
else {
# Else, we're going to tell you we couldn't match the directory
# either to one or to all candidates we tried.
Carp::carp(
"A valid path cannot be determined from '$raw_path': "
. ( @missed > 1 ? do {
local $LIST_SEPARATOR = "\n - ";
push @missed, '', $path;
"\n No paths:@missed\n do not exist!";
}
: "$path does not exist!"
));
@missed = () if @missed;
} # end else
} # end while @_
}
Then you use like this:
package main;
use A::Long::Package::Name;
use Smart::Comments;
use libr 'A::Long::Package::Name/../Foo', 'Figgy::Puddin';
Try dumping out @INC
after words and see what happened.
Upvotes: 0
Reputation: 126722
The only way to find the location of a module file is to use __FILE__
. Both FindBin
and $0
always refer to the main script file.
For the module, this is the neatest I could come up with. Your solution for the main code is fine, but you could use this alternative there as well.
use strict;
use warnings;
use File::Basename 'fileparse';
use File::Spec;
my $dir;
BEGIN {
$dir = (fileparse(File::Spec->rel2abs(__FILE__)))[1];
}
use lib $dir.'../mod1';
use pack1;
package pack2;
1;
Upvotes: 2
Reputation: 126722
The locations of the modules must be in the @INC
at the moment the use
statement is compiled. The easiest way would be to add them all in the calling program Test.pl
like this
use lib "$FindBin::Bin/../mod1", "$FindBin::Bin/../mod2";
then the compilation of all the modules will go ahead fine.
Upvotes: 2