1

I am a newbie to Perl. Sorry for my stupid question.

I have some code like this:

bintest.pl
subfolder/sample.pl

And in the sample.pl, some is defined like this:

#!/usr/bin/perl
use lib '.';
use lib '..';
use bintest qw(:DEFAULT 
    $client_routine
    );

However, when I use perl sample.pl in the subfolder, I got this error:

Can't locate bintest.pm in @INC (@INC contains: .. . /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl) at sample.pl line 6.

Am I missed anything? Could anyone give me some help?

lllllllllllll
  • 8,519
  • 9
  • 45
  • 80

2 Answers2

8

use does something very specific:

It is exactly equivalent to

BEGIN { require Module; Module->import( LIST ); }

require also does something very specific:

If EXPR is a bareword, the require assumes a ".pm" extension and replaces "::" with "/" in the filename for you, to make it easy to load standard modules.

So, when you say

use bintest ( .. );

perl first tries to locate bintest.pm in one of the directories in @INC (which now also contains . and .. thanks to your use lib statements).

It can't find it, because what you have is bintest.pl.

You could require '../bintest.pl', and then invoke bintest->import( ... ) yourself in a BEGIN block, but, I am assuming you don't want to go there.

I will recommend that you use a different directory structure:

  • $project_root/bin: Your scripts live here
  • $project_root/lib: Your .pm files live here

Put in $project_root/lib/My/Bintest.pm the following file

package My::Bintest;

use strict;
use warnings;
use Exporter qw( import );

1;
__END__

Then, from your script in $project_root/bin/script.pl, you can do:

use FindBin qw( $RealBin );
use lib "$RealBin/../lib";
use My::Bintest qw( ... );
Community
  • 1
  • 1
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
1

You have two Perl programs called bintest.pl and sample.pl. You have no Perl modules. The function use is used to load Perl modules. When use converts the module name it is given to a filename to search for, one of the things it does is to append ".pm" to the name.

So given that you have a program (bintest.pl) and not a module (which would be called bintest.pm), it's not at all surprising that use can't find it.

The real question is, of course, what can you do to fix this problem? And that's hard to answer without a lot more information. It's possible that bintest should be a module and that someone has just given it the wrong name - in which case just renaming it would work. It's possible that it is an old-style Perl library - in which case loading it with require rather than use might well work. But it's equally possible that it's really a program and you shouldn't be trying to use it in this way.

With knowing more about the code (where it comes from, how it is documented, what it looks like) it is impossible to give a definitive answer.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • Please see "[In Perl, what is the difference between a .pm (Perl module) and .pl (Perl script) file?](http://stackoverflow.com/questions/3402821/in-perl-what-is-the-difference-between-a-pm-perl-module-and-pl-perl-script)" – Sinan Ünür Aug 21 '15 at 15:43