jdetloff has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I have a very perplexing (to me at least) and frusterating problem. I seem to be somehow messing up a very simple subroutine call, but I can't for the life of me discover what I'm doing wrong

#!/usr/bin/perl use lib "C:/Program Files/Perl Express/Scripts"; package ClanBotFire; sub heyhey { print "hello world"; } 1;
#!/usr/bin/perl use lib "C:/Program Files/Perl Express/Scripts"; ClanBotFire::heyhey();

A google search reveals people having problems with this (getting undefined subroutine error when your subroutine should be defined) when they have more than one subroutine with the same name, or when there's a namespace conflict with two packages, but my routine and module are uniquely named. Any ideas of what could be going wrong?

Replies are listed 'Best First'.
Re: Undefined Subroutine Called
by Corion (Patriarch) on Jan 24, 2010 at 21:38 UTC

    I'm a bit unclear on what your actual file structure is. If you put something into a module, you need to use that module afterwards. Perl will not automatically load a module for you, you have to tell Perl that it should load a module.

    If I may interpret your scenario, it might be that the first piece of code lives in a file called ClanBotFire.pm, and the second piece of code is your "main program" in which you want to use the aforementioned subroutine. Then you will need to load that module in your main program, via the use statement.

    As a general approach when seeking wisdom, showing us the exact, verbatim error message is usually far better than some vague description of some keywords that you remember from the error message.

      *facepalm

      Adding "use ClanBotFire;" to my main file solved the problem, thanks for the help

Re: Undefined Subroutine Called
by biohisham (Priest) on Jan 24, 2010 at 21:58 UTC
    use ClanBotFire; ClanBotFire->heyhey(); #or #&ClanBotFire::heyhey()
    If you treated this package as a module, you would get to allow the package symbols to be exported to the current namespace and hence you would not need to qualify the subroutine call with the package name as in the previous snippet.
    #"ClanBotFire.pm" package ClanBotFire; use lib "C:/Program Files/Perl Express/Scripts"; BEGIN{ use Exporter(); @ISA = qw(Exporter); @EXPORT = qw(&heyhey); } sub heyhey { print "hello world"; } 1;
    use ClanBotFire; heyhey;


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.