为什么即使在我使用 FindBin 调整 @INC 后,我的 Perl 脚本也找不到我的模块?
我希望能够使用保存在源代码存储库的 lib 目录中的模块,并且我希望开发人员使用我正在编写的脚本的唯一先决条件是拥有一个标准 Perl 安装,但我不知道如何完成这个。
在我的脚本中,我已将
use FindBin qw($Bin);
use lib "$Bin/lib"; # store non standard modules here
use Term::ANSIColor;
use Win32::Console::ANSI;
print Term::ANSIColor::colored("this should be in color\n", "bold red");
模块放入 ./lib 中。我验证了这是模块存在的实际位置(通过重命名它并导致它失败)。然而,即使模块位于任意 lib 目录中,似乎仍然要求 ppm 了解该模块。
如果没有先由ppm“安装”,我就无法让我的脚本在lib 中查找/使用它。我想应该有某种方法可以解决这个问题。
我知道这可能是一个非典型的请求,但我的目标可能是非典型的。我只希望开发人员进行签出并立即使用一些脚本,而无需运行一些额外的命令或使用包管理器。
感谢您的任何见解。
编辑:我更新了一个完整的示例。我还意识到,如果我通过 ppm 卸载它(但将 pm 保留在引用的目录中),我可能必须更改我的语法,而我之前没有考虑到这一点。因此,也许我必须提供完整路径或使用像 jheddings 或 BipedalShark 建议的 require (即,如果它没有“安装”,那么我必须使用“require”并向其附加“.pm”或使用 BEGIN 块。
如果这是这样的话,那么我还没有找到正确的语法。
编辑2:根据下面的评论,我意识到我可能有一个有缺陷的假设:如果我引用实际的代码。 ,“.pm”,那么我应该能够在不使用包管理器的情况下使用它,或者如果我想这样做,我可能必须以不同的方式进行。重构“.pm”中的代码。
编辑3:我认为我误解了一些事情,我的IDE中的错误消息“Compilation failed in require”,它突出显示了这一行。我用来包含该模块,并且控制台错误消息“无法找到模块 Win32::Console::ANSI 的可加载对象”
我将其视为加载模块本身的问题,但这似乎是一个由模块本身尝试加载的内容引起的问题。有趣的是,这只是一个问题,因为我没有使用 ppm install 。
它正在寻找实际的模块。我能够通过注释掉故障线来验证这一点。
感谢您的帮助,但我还得花更多时间。
I want to be able to use a module kept in the lib directory of my source code repository, and I want the only prerequisite for a developer to use the scripts that I'm writing is to have a standard Perl installation, but I'm not sure how to accomplish this.
In my scripts, I have
use FindBin qw($Bin);
use lib "$Bin/lib"; # store non standard modules here
use Term::ANSIColor;
use Win32::Console::ANSI;
print Term::ANSIColor::colored("this should be in color\n", "bold red");
and I put the module in ./lib. I verified that's the actual location where the module exists (by renaming it and causing it to fail). However, even if the module is in an arbitrary lib directory, it still seems to be a requirement that ppm be aware of the module.
I can not get my scripts to find/use it in lib without it being "installed" by ppm first. I would imagine that there should be some sort of way around this.
I know this may be an atypical request, but my goals are probably atypical. I just want a developer to do a checkout and immediately use some scripts without having to run some additional commands or use a package manager.
Thanks for any insight.
EDIT: I updated with a complete example. I also realized that if I uninstall it via ppm (but leave the pm in the referenced directory), I may have to change my syntax, and I was not considering that before. So maybe I have to give a full path or use require like jheddings or BipedalShark propose (ie. if it's not "installed", then I must use "require" and append ".pm" to it or use a BEGIN block.
If this is the case, then I have not found the correct syntax.
EDIT 2: Based on a comment below, I realize that I may have a flawed assumption. My reasoning is this: If I reference the actual code, the ".pm", directly then I should be able to use it without using a package manager. Maybe that's not the case, or if I want to do that maybe I have to do it a different way. Alternatively, I may have to refactor the code in the ".pm".
EDIT 3: I think that I was misunderstanding a few things. The error message in my IDE "Compilation failed in require", it's highlighting of the line that I was using to include the module, and the console error message of "Can't locate loadable object for module Win32::Console::ANSI"
I was reading that as a problem with loading the module itself, but it seems to be a problem that results from something the module itself is attempting to load. Interesting that this is only a problem since I didn't use ppm install though.
It is finding the actual module. I was able to verify that by commenting out the trouble lines.
Thanks for the help, but I'll have to spend some more time with it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请参阅 perldoc perldiag 在“无法找到模块的可加载对象...”下:
你是对的,这个问题是由模块试图加载的东西引起的——这就是 Dynaloader 确实如此。但是,Win32 的文档: :Console::ANSI 没有提及任何外部库要求。
See perldoc perldiag under "Can't locate loadable object for module ...":
You are correct that this problem is arising from something the module is trying to load -- that's what Dynaloader does. However, the documentation for Win32::Console::ANSI makes no mention of any external library requirements.
您是否在
lib
目录中保留模块路径结构?即您的模块应该位于路径
$Bin/lib/Some/Module.pm
中。Are you preserving your module path structure in your
lib
directory?i.e. your module should be in the path
$Bin/lib/Some/Module.pm
.来自 perlfaq8 的回答 如何将程序所在的目录添加到模块/库搜索路径中?
您似乎做得正确,但您需要给我们如果您希望获得帮助,请提供更多信息。
当您运行该脚本时,
@INC
中最终会出现什么?输入如下调试行:检查输出是否显示您期望的目录。如果没有,请从那里开始解决问题。
From perlfaq8's answer to How do I add the directory my program lives in to the module/library search path?
You appear to be doing it correctly, but you need to give us more if you expect to get help.
When you run that script, what ends up in
@INC
? Put in a debugging line like:Check that that output shows the directory that you expect. If it doesn't, start bisecting the problem from there.
试试这个:
Try this:
我一直手动安装模块,它似乎有效。我只是将目录和文件复制到某个位置并使用“use lib”指令,如您所示。有时我错过了一个文件,并且遇到运行时错误,它正在寻找某个文件,我会在互联网上找到该文件并将其放在正确的位置,然后它就可以工作了。不确定您的设置发生了什么情况。这应该有效。
我通常将 perl 模块放在与我的脚本相同的目录中,然后:使用 lib "."
但我不知道这有什么关系。
I manually install modules all the time and it seems to work. I just copy directories and files into a location and use the "use lib" directive like you've shown. Sometimes I miss a file and I get a runtime error that it's looking for a certain file and I go find the file on the Internet and put it in the right place and it works. Not sure what's going on with your setup. This should work.
I usually put the perl modules in the same directory as my script and then: use lib "."
But I don't know that it would matter.