[tpm] Perl modules

Uri Guttman uri at StemSystems.com
Thu Jan 14 17:59:21 PST 2010

>>>>> "a" == arocker  <arocker at vex.net> writes:

  a> I defined a set of subroutines in a module.

  a> They worked perfectly well when the module was in the same directory as
  a> the caller.

  a> Then I moved the module to a directory on the @INC array, so it would
  a> become generally available, and now it's broken. Specifically, although
  a> the program compiles, when running it dies with an "Undefined subroutine
  a> &main::whatever" message.

  a> What have I done wrong?

sounds like namespace issues. did you put a package name in the module?
if you did, you need to export those subs to the main:: space (or
whereever the module is used) so those sub names are visible. this is
easily done with the Exporter module and some others do it too. the
basic way would be something like this:

package FooBar ;

use base 'Exporter' ;

our @EXPORT = qw( &foo &bar ) ;

and in the main code:

use FooBar ;

then subs foo() and bar() should be available.

if you didn't use a package command (which isn't needed but useful) then
the subs would be in main:: anyhow and you wouldn't get that
problem. but using a namespace for a module is a good idea as you can
manage larger amounts of code that way.

one other solution is to use fully qualified names in the main:: code:

this calls the foo() sub in that module which you loaded earlier.

FooBar::foo() ;


Uri Guttman  ------  uri at stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

More information about the toronto-pm mailing list