简体   繁体   English

你如何在lib Path中使用变量?

[英]How do you use a variable in lib Path?

I would like to use the $var variable in lib path. 我想在lib路径中使用$ var变量。

my $var = "/home/usr/bibfile;"

use lib "$var/lib/";

However when I do this it throws an error. 但是,当我这样做时,它会抛出一个错误。

I'd like to use use lib "$var/lib/"; 我想use lib "$var/lib/"; instead of use lib "/home/usr/bibfile/lib/"; 而不是use lib "/home/usr/bibfile/lib/"; .

How can I assign a variable, so that it can be used in the setting of lib modules? 如何分配变量,以便可以在lib模块的设置中使用?

Variables work fine in use lib , well, just like they do in any string. 变量在use lib时运行良好,就像在任何字符串中一样。 However, since all use directives are executed in BEGIN block, your variable will be not yet initialized at the moment you run use , so you need to put initialization in BEGIN block too. 但是,由于所有use指令都是在BEGIN块中执行的,因此在运行use ,您的变量尚未初始化,因此您还需要在BEGIN块中初始化。

my $var;
BEGIN { $var = "/home/usr/bibfile"; }
use lib "$var/lib/";

use Data::Dumper;
print Dumper \@INC;

Gives: 得到:

$VAR1 = [
      '/home/usr/bibfile/lib/',
      # ... more ...
    ];

Not sure about what you're trying to accomplish, but seems like a task for FindBin::libs : 不确定你想要完成什么,但似乎是FindBin :: libs的任务:

my $var;
BEGIN { $var = "/home/usr/bibfile" };
use FindBin::libs "Bin=$var", "base=lib";

You can't, because the use directive is evaluated at compile time, while other variables are evaluated at runtime. 您不能,因为use指令是在编译时计算的,而其他变量是在运行时计算的。

If your lib is located somewhere relative to your original script, you can use the standard module FindBin : 如果您的lib位于相对于原始脚本的某个位置,则可以使用标准模块FindBin

# $Bin from FindBin is the directory of the original script
use FindBin;
use lib "$FindBin::Bin/path/to/bib";
use MyModule;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM