Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

'use' falls out of scope at runtime

by arpx (Acolyte)
on Apr 23, 2009 at 13:45 UTC ( #759528=perlquestion: print w/replies, xml ) Need Help??

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

Hej monks,

probably easy solution here, but i just cant find it.. rubbing my head for days :)

eval("use $module");
triggered inside the code at runtime keeps falling out of scope till next run.

im trying to autoload packages from a 'plugin' folder by user request, since the content of this folder grows quite frequently with new packages i dont want to restart apache each time a new package is added. (running mpm worker on busy server)

thankx for all that can help

Replies are listed 'Best First'.
Re: 'use' falls out of scope at runtime
by perrin (Chancellor) on Apr 23, 2009 at 14:02 UTC
    Are you saying that the second time you run that use, it doesn't reload an already loaded module? That's just how Perl works, but you can use workarounds like Apache2::Reload.
      no, it falls out... the module is not available.

      Thu Apr 23 14:20:57 2009 error client xxxx Undefined subroutine &plugin23::test called at ..

      the only way to keep it in enviroment is to trigger the function calling 'use' for each run

        use called a second time doesn't reload the module, since it checks %INC (see perlvar) and, if the module is already in that hash, the underlying require just returns 1, so no loading. You would have to undef $INC{$module} for the use to reload the module. Keep in mind that in %INC the key for Some::Module is Some/

        Aa Corion said, you could use Apache::Reload for those packages; if you have changing packages where methods/functions are organized into files, you could use AutoReloader.

Re: 'use' falls out of scope at runtime
by ikegami (Pope) on Apr 23, 2009 at 13:49 UTC
    Is this a CGI script or a mod_perl Registry script (or ...)? Could you please clarify what you mean by "falling out of scope"?
      its all MP scripts, .pm modules with a set of functions each im trying to import from the main program which is a mod_perl script running backend in apache

      the first run after restart loads the extra modules together with the initial enviroment. but the 2nd run doesnt want to find them without reloading them, the initial enviroment is stable though

        I can see three possibilities.

        • You don't actually have a module.

          As I define a module, it must have its own namespace and placed there using a package statement.

        • The runs are in different Apache processes.

          Apache has many children to handle requests. Loading a module in one won't load it in the others (unless you load it in the mod_perl startup script).

        • The module is loaded, but the symbols haven't been imported into the current namespace.

          use Some::Such; has two effects: require and import. A module only needs to be required once. (Subsequent calls to require are no-ops.) But you need to call import from each namespace into which you want to import.

        Apache also restarts its children from time to time.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://759528]
Approved by ikegami
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2020-10-20 18:05 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (210 votes). Check out past polls.