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

I've written an IRC bot (a useful one, not the annoying type). I'm trying to set it up so there is a separate module/file that contains most of the program so that I can make changes and reload without restarting the program.

Also, I want the functions in the separate module/file to be in the main program namespace to make things less (or more depending on how you look at it) complicated, although this isn't necessary.

Since this is Perl, I'm sure there's 7 different ways to do this...unfortunately, I don't know any of them :P
  • Comment on importing/removing functions from a module into the main namespace

Replies are listed 'Best First'.
Re: importing/removing functions from a module into the main namespace
by Zaxo (Archbishop) on Mar 14, 2005 at 07:54 UTC

    If the arguments to use after the module name are function names, they will be imported. use MyBot qw/afunc another thethird/; The Exporter module can be used as a base class in MyBot.pm to get tagged exports and other goodies.

    After Compline,
    Zaxo

      What about reloading the module? Do I just 'use MyModule qw/foo bar baz/;' again?
        use and require normally only load a module once during execution of the script, even if they're called multiple times. Any imports done by use will be done for each use statement, though.

        If you really want to reload a module while running the program, you can do something like this (not 100% foolproof):

        use Symbol qw(delete_package); sub re_use { my $module = shift; # "Module::To::Reload" my $fname = $module; $fname =~ s#::#/#g; $fname .= ".pm"; # "Module/To/Reload.pm" delete_package($module); # remove the Module::To::Reload namespace delete $INC{$fname}; # delete the filename from the # "loaded" list eval "use $module" or die $@; }
Re: importing/removing functions from a module into the main namespace
by tphyahoo (Vicar) on Mar 14, 2005 at 10:42 UTC
    Do Dominus's very very short tutorial on Modules.

    Download the entire thing as a zip. Do all the exercises carefully. It should take half a day or so. You should then be able to start moving stuff out of your code into modules

    Then, if you want more clarification, do perlboot and perltoot. And the other perl core documentation.

    But Dominus's tutorial is what got me started if you just want the basics. Good luck!

      I think my problem is that I'm thinking in Python. I was forced to learn Python for a project I'm working on and have been using that almost exclusively for 4+ months. I've pulled out my copy of Advanced Perl Programming to get me back in the right mindset :P
Re: importing/removing functions from a module into the main namespace
by brian_d_foy (Abbot) on Mar 14, 2005 at 17:48 UTC

    It sounds like you want to do the same thing that Apache::StatINC does. It's a short bit of code that should show you the way.

    Good luck :)

    --
    brian d foy <bdfoy@cpan.org>