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

I am writing a program that needs to be able to load and unload a plugin type module without halting the program and for the life of me I cant find any way to do this.
I need to be able to load, jump to a function in the module, and then unload the module when the function returns. I have tried various forms of trickery to get this to happen and looked around CPAN but I haven't come up with much. Hopefully someone here has done this before?

Replies are listed 'Best First'.
(tye)Re: Load and unload modules ala dlopen();
by tye (Sage) on Oct 07, 2000 at 08:31 UTC

    I'm pretty sure that the "module" here is not a Perl module, due to the reference to dlopen(), which is the C interface to shared libraries.

    So what you need is the Perl module C::DynaLib. You can also try the FFI module which supports call-backs. They both allow you to attach to a shared library (or *.DLL under Windows) and call routines in it.

    You can also try the Inline module which lets you, at run time, write C code to interface to the shared library, compile that C code into a shared library module that Perl can load, and then use that shared library to access the library you are interested in -- all without leaving the currently running script. Subsequent requests to use that C code only go through the compilation step if you have changed the C code. A pretty neat module but probably not as good for what you seem to want as the first two I mentioned.

    All of these are available from CPAN. Go to and search for any of these modules to browse the read-me files or documentation. Then download the latest version, probably as a *.tar.gz file. I hope you already have gunzip or GNU tar [and I assume that you already have tar since you mentioned dlopen()]. That way you can unpack the archive and do the standard:

    perl Makefile.PL make test make install
    to build, test, and install the Perl module.

    Once the module is installed, you can type "perldoc C::DynaLib" (for example) to read the documentation that came with the module.

            - tye (but my friends call me "Tye")
Re: Load and unload modules ala dlopen();
by AgentM (Curate) on Oct 07, 2000 at 05:24 UTC
    Runtime or precompile? Only diff is the BEGIN{}. This is for runtime.
    require module; import module; #dynamic runtime load

    And thissa here is using use:
    use module; no module; #precompiled include/ import
    You ain't gonna need no CPAN, this is built-in dynamically-loading functionality, somethin' that i really miss in *NIX C. perl is the alpha and the omega. sorry, i just had sushi.
    AgentM Systems or Nasca Enterprises is not responsible for the comments made by AgentM- anywhere.
Re: Load and unload modules ala dlopen();
by cianoz (Friar) on Oct 07, 2000 at 14:20 UTC
    eval("use That::Module"); print "load module failed: ($@)\n" if $@;
    perldoc -f eval
RE: Load and unload modules ala dlopen();
by hydo (Monk) on Oct 08, 2000 at 01:27 UTC
    Ok. I am obviously not explaining my question correctly. I am writing a MUD in perl. The core functionality runs as a deamon. Logged into the program I need to be able to load and unload modules that are different "rooms" so to speak. Unloading the module is just as important as loading the module because I want people to be able to add sections and test thier code without having to take the whole MUD down. Here is a little pseudo code example:
    my $fname = < what the user entered in as thier new room name> my $t = $fname->new(); $t->init_room( $myself ); # do some stuff to the room. free( $t ) # hehehe... ok well something like this anyway.
    It needs to be able to do this many, many times without eating up memory every time. Hopefully, this makes more sense.
Re: Load and unload modules ala dlopen();
by pope (Friar) on Oct 10, 2000 at 01:40 UTC
    free-ing is something like calling the corresponding destructor.
    DESTROY is called when your lexical variable goes out of its scope. That's simple.