我如何告诉 perl 模块的路径?

发布于 2024-11-14 06:08:47 字数 544 浏览 1 评论 0原文

我在 Perl 脚本中使用 Perl 模块文件:

printtab.pl

use Table;

Table.pmprinttab.pl 位于同一目录中,因此只要我从目录中执行 printtab,它就可以正常执行。

但是,如果我从其他地方执行它,例如使用 cronjob,我会收到一条错误,指出在 @INC 中找不到该模块。

解决这个问题的正确方法是什么?

我尝试过

push @INC, "/path/Table.pm";

,但不起作用。 你能告诉我为什么吗?

我发现use lib并且它工作正常

use lib "/path";

在这种情况下use lib是最好的方法吗?

I use a perl module file in my perl script:

printtab.pl

use Table;

Table.pm is present in the same directory as printtab.pl, so as long as I am executing printtab from the directory, it executes fine.

But If I execute it from some other place, for example, using a cronjob, I get an error mentioning that the module is not found in @INC.

What is the correct way to resolve this?

I tried

push @INC, "/path/Table.pm";

but it does not work. Can you tell me why?

I found about use lib and it works correctly

use lib "/path";

Is use lib the best method in such a situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

动听の歌 2024-11-21 06:08:47

使用 lib 是一个不错的选择。但是,如果您将模块放置在与程序相同的目录中(或者放置在与包含程序的目录相关的子目录中),则可以使用 use FindBin; ,例如:

use FindBin;
use lib "$FindBin::Bin/../lib";

use lib is a good option. But is you place your modules in the same directory as your programs (or in a sub-directory relative to the one containing your programs), you could use use FindBin; like:

use FindBin;
use lib "$FindBin::Bin/../lib";
我一直都在从未离去 2024-11-21 06:08:47
push @INC, "/path/Table.pm";

不起作用,因为 @INC 应该是目录列表,而不是模块的完整路径。所以如果你这样做:

push @INC, "/path";

它应该有效。

话虽如此,use lib "/path";也是一个不错的选择。

根据程序的安装目录结构,使用 FindBin 确定脚本的真实位置可能有助于构建模块的正确路径。

push @INC, "/path/Table.pm";

Does not work because @INC is supposed to be a list of directories, not of full paths to modules. So if you do:

push @INC, "/path";

It should work.

Having said that, use lib "/path"; is also a good option.

Depending on your programs' install directory structure using the FindBin to determine the real location of your script may prove useful to construct the correct path to your module.

时光礼记 2024-11-21 06:08:47

一个巧妙的小技巧:

#! /usr/bin/env perl

use strict;
use warnings;

BEGIN {
    our $moduleIsMissing;
    eval {
        require My::Module;
    };

    if ($@) {
        $moduleIsMissing = 1;
    }
}

if ($main::moduleIsMissing) {
    print "Module is missing. I've got to do something about it!\n"
}
else {
    print "Module is there. I'm fine!\n";
}

我对 $main::moduleIsMissing 事情不满意,但我还没有找到解决方法。

如果 My::Module 可用,则它已加载并且一切正常。否则,$main::moduleIsMissing 标志被设置。这允许您测试模块是否已加载,如果没有加载,则采取规避操作。例如,如果模块 Term::Cap 可用,您的程序在打印文本时将使用粗体,但如果不可用,则将简单地打印出没有粗体的文本。


回到问题:这样做要好得多:

use lib "MyDir";

而不是 @INC

use lib 在编译时包含该目录。因此,如果我在目录“MyDir”中有一个模块“Foo::Bar”,我可以这样做:

use lib qw(MyDir);
use Foo::Bar;

没有 Perl 抱怨。如果我这样做:

push (@INC, qw(MyDir));
use Foo::Bar;;

Perl 会抱怨它找不到我的模块。

A neat little trick:

#! /usr/bin/env perl

use strict;
use warnings;

BEGIN {
    our $moduleIsMissing;
    eval {
        require My::Module;
    };

    if ($@) {
        $moduleIsMissing = 1;
    }
}

if ($main::moduleIsMissing) {
    print "Module is missing. I've got to do something about it!\n"
}
else {
    print "Module is there. I'm fine!\n";
}

I'm not happy about the $main::moduleIsMissing thing, but I haven't figured a way around it.

If My::Module is available, it's loaded and everything is fine. Otherwise, the $main::moduleIsMissing flag is set. This allows you to test to see if the module was loaded, and if not, take evasive action. For example, your program will use boldfacing when printing text if module Term::Cap is available, but if not, will simply print out the text sans the boldface.


Back to the question: It's much better to do:

use lib "MyDir";

Rather than the @INC.

The use lib includes the directory at compile time. Thus, if I had a module "Foo::Bar" in directory "MyDir", I could do this:

use lib qw(MyDir);
use Foo::Bar;

without Perl complaining. If I did this:

push (@INC, qw(MyDir));
use Foo::Bar;;

Perl would complain it can't find my module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文