1

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.

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.

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.

Edit: Sample Code

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.

4

2 回答 2

2

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

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.

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.

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.

于 2012-09-20T13:04:27.053 回答
0

if like me you are here and used Notepad++ with NppExec, I may have the good answer for you:

go into NppExec options: plugins > NppExec clic on "Follow $(CURRENT_DIRECTORY)"

the thing is I'm sure it was the only thing I changed but when I disable this option I don't get the error anymore ...

hth someone

于 2013-04-15T12:05:35.870 回答