Search code examples
perlperl-module

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.

Both files have the appropriate head #!…


Solution

  • 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.