开发者

How to use a module in a different file

I am trying to use a module located in the same folder (cgi-bin) in another file

I have something like this

package ModuleName;
sub …

and in the other file I have

use ModuleName;

is there a special way to import not library files, or files in the cgi-bin along side perl scripts?

Everything worked when they were both in the same file.

开发者_JAVA技巧

Both files have the appropriate head #!…


Unless the module exports your sub (with Exporter and @EXPORT_OK (or @EXPORT, but that's less polite)), you'll need to refer to it as ModuleName::my_sub instead of just my_sub in the code that uses the module.

Edit: Seeing the error message in your comment on the earlier answer, your first problem is that modules must return a true value when loaded. This is conventionally accomplished by adding the line:

1;

as the final line of the module file.


You could add:

use lib '/path/to/directory';

which will allow you to set an additional directory for loading modules from. Make sure your module file name ends in a .pm too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜