Perl中出现“无法在@INC中找到file.pl ..."错误 [英] 'Can't locate file.pl in @INC...' error in Perl

查看:122
本文介绍了Perl中出现“无法在@INC中找到file.pl ..."错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件夹,其中包含几个通过main.pl运行的.pl文件.我已经通过在主文件开头使用require 'file.pl';成功地完成了这些工作.我也使用了一些模块,没有问题.

I have a folder with several .pl files that run through main.pl. I have successfully had these working by using require 'file.pl'; at the beginning of the main file. I have used some modules, too, without issue.

我现在正在尝试使用自制"模块,但是当我在要使用的文件顶部(或主文件顶部)添加use Add::Location::CSearch qw(c_search);并运行主文件时,它会产生Can't locate file.pl in @INC...错误,其中file.pl是主文件中require列表中的第一个文件.

I am now trying to use a 'home-made' module, but when I add use Add::Location::CSearch qw(c_search); to the top of the file that it is being used in (or the top of the main file) and run the main file, it produces the Can't locate file.pl in @INC... error with file.pl being the first file in the require list in the main file.

据我了解,在添加use行之后,它现在尝试在同一目录中查找所有require.错误消息末尾的(@INC contains: ...)列出了一些库/模块文件夹.在当前目录的末尾也有..

From how I understand it, after adding the use line it now tries to find all the require in that same directory. The (@INC contains: ...) at the end of the error message lists some library/module folders. It also has . at the end for the current directory.

我希望我已经解释清楚了;令人困惑!我不知道为什么会这样以及如何解决它,所以我将不胜感激.

I hope I have explained this well; it is confusing! I don't know why this happens and how to solve it, so I would be grateful for any ideas.

示例代码

main.pl

#!/usr/bin/perl
package testpack;

use lib '/hlib/...';
use Add::DB;
use List::Util qw(sum);
use List::MoreUtils qw(uniq);
use List::MoreUtils qw(firstidx);
use strict;
use warnings;

require 'file1.pl';
require 'file2.pl'; 
require 'file3.pl';

.
.
.

file1.pl

#!/usr/bin/perl
package testpack;
use Add::Location::CSearch qw(c_search);

这才是真正解决问题的方法...其余文件具有可调用子例程等的有效代码.

This is all that really counts for the problem... the rest of the files have working code that calls subroutines etc.

推荐答案

我不确定自己的设置如何,但是我会在这里扔些东西.

Without seeing your setup I can't be sure, but I'm going to throw something out there.

我怀疑main.plfile1.pl都在同一个目录中. require 'file1.pl'之所以有效,是因为您一直在该目录中执行perl main.pl. Perl正在找到file1.pl,因为.@INC中,它是查找模块的列表.

I suspect what's going on is main.pl and file1.pl are all in the same directory. require 'file1.pl' is working because you've been doing perl main.pl from that directory. Perl is finding file1.pl because . is in @INC, the list of where it looks for modules.

现在,您正在开发一个新模块.您已经将目录更改为要开发该模块的目录,并且可能正在运行perl /path/to/main.pl. Perl找不到file1.pl,因为当前目录不同.

Now you're developing a new module. You've changed directories to where you're developing that module, and maybe you're running perl /path/to/main.pl. Perl can no longer find file1.pl because the current directory is different.

典型的解决方案是使用 FindBin use lib将目录放在您的main.pl驻留在@INC中.然后,无论您从哪个目录运行main.pl,它都可以在同一目录中找到其他库.

The typical solution for this is to use FindBin and use lib to put the directory where your main.pl resides into @INC. Then it can find other libraries in that same directory no matter what directory you run main.pl from.

这篇关于Perl中出现“无法在@INC中找到file.pl ..."错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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