Perl 使用/需要绝对路径? [英] Perl use/require abolute path?

查看:68
本文介绍了Perl 使用/需要绝对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个 .pm 文件,有没有一种方法可以使用它,而无需将它放在我的 @INC 路径上?我认为这在我的特定用例中会更清晰 - 比使用相对路径或将此目录添加到 @INC 更清晰.

If I have a .pm file, is there a way I can use it, without placing it on my @INC path? I think that would be clearer in my particular use case - clearer than using relative paths or adding this directory to @INC.

澄清:

我希望避免遍历 @INC 中的每一项的必要性,而是直接指定我感兴趣的文件.例如,在 Node.JS 中,require('something') 将搜索路径列表,但 require('/specific/something') 将直接转到我告诉它的地方.

I was hoping to avoid the necessity to iterate through every item in @INC, and instead specify directly which file I am interested in. For example, in Node.JS, require('something') will search the list of paths, but require('/specific/something') will go directly where I tell it to.

在 Perl 中,我不确定这是否与 require 中的功能相同,但它似乎有效.

In Perl, I am not certain that this is the same functionality found in require, but it seems to work.

然而,use 语句需要裸字.这让我对如何进入绝对路径有点困惑.

However, use statements require barewords. That has left me a little stumped on how to enter an absolute path.

推荐答案

根据评论中的讨论,我建议使用 require 本身.如下图,

As per discussion in comments, I would suggest using require itself. Like below,

require "pathto/module/Newmodule.pm";

Newmodule::firstSub();

您也可以使用以下其他选项

Also you can use other options as below

  • use lib 'pathto/module'; 需要将此行添加到要在其中使用模块的每个文件中.
  • use lib 'pathto/module'; This line needs to be added to every file you want to use the module in.

使用 lib 'pathto/module';
使用新模块;

use lib 'pathto/module';
use Newmodule;

  • 使用 PERL5LIB 环境变量.使用 export 在命令行上设置或将其添加到 ~/.bashrc 以便每次登录时它都会添加到您的 @INC.记住 PERL5LIB 在所有 @INC 目录之前添加目录.所以会先用.您也可以使用

    • using PERL5LIB environment variable. Set this on command line using export or add this to ~/.bashrc so that with every login it will be added to your @INC. Remember PERL5LIB adds directory before all @INC directories. So it will be used first. Also you can set it in apache httpd.conf using

      SetEnv PERL5LIB /fullpath/to/module
      

    • 或者在 BEGIN 块中设置.

    • Or set it in BEGIN block.

      这篇关于Perl 使用/需要绝对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆