2

我有三个文件:

~/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;

如您所见,base.pl 使用 pack2,而后者又使用 pack1。然而,这是一个如何不使用 FindBin 模块的演示:当执行 base.pl 时,pack2 将无法定位 pack1,因为它将保留从 base 获取的 "$FindBin::Bin" 的值.pl。

所以我的问题很简单:perl 中是否有一种方法可以“使用”一个“使用”另一个模块的模块,所有这些都基于相对于执行“使用”的文件的路径?

4

4 回答 4

2

如果你知道所有可能的库根,你可以在命令行中添加它们:

perl -I~/mod1 -I~/mod2 myscript.pl

你可以将它们添加到 PERL5LIB 环境变量中:

export PERL5LIB=~/mod1:~/mod2

任何一种方法都将目录放在 libaray 搜索路径上。


附加信息:

如果您希望各个包“声明”它们的依赖项所在的位置,Perl 提供了 'lib' pragma:

use lib '/path/to/lib/directory';
于 2012-12-04T19:12:54.397 回答
2

模块的位置必须在编译语句@INC的那一刻。最简单的方法是像这样use将它们全部添加到调用程序中Test.pl

use lib "$FindBin::Bin/../mod1", "$FindBin::Bin/../mod2";

那么所有模块的编译都会顺利进行。

于 2012-12-04T21:01:55.180 回答
2

查找模块文件位置的唯一方法是使用__FILE__. 两者FindBin$0始终引用主脚本文件。

对于模块,这是我能想到的最整洁的。您的主代码解决方案很好,但您也可以在那里使用这个替代方案。

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;
于 2012-12-04T22:33:44.277 回答
0

你没有那个,但你可以自己做。

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 @_
}

然后你像这样使用:

package main;

use A::Long::Package::Name;
use Smart::Comments;

use libr 'A::Long::Package::Name/../Foo', 'Figgy::Puddin';

试着在说完@INC话后转出,看看发生了什么。

于 2012-12-04T23:59:40.923 回答