in reply to Loading a module into many packages

I don't have any package declarations in mymodule.pm; it's just a file with a lot of functions.
That's your problem. Use declaration package mymodule; in it and use Exporter to export your functions.

Also note that Class::Contract isn't meant for production according to Categorized Damian Modules.

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Replies are listed 'Best First'.
Re^2: Loading a module into many packages
by arkturuz (Curate) on Oct 27, 2006 at 09:09 UTC
    That's your problem. Use declaration package mymodule; in it and use Exporter to export your functions.

    Indeed, that is my problem because I inherited a large Perl code base written exactly in that way: a lot of files with a lot of perl functions. No packages, no classes. I try to put some common modules into that mess, but I really think I should remove all that crap and write the stuff from the very beginning.

    I know about Class::Contract and Damian's classification. I use Class::Contract for merely non-serious software. It has nice and clean interface I like. Actually my next question on SoPW was planned to be: why not use Class::Contract in production, but I think I'm going to explore Object::InsideOut and forget about C::C.

      Here's what happens when you use() your mymodule.pm the first time. The file mymodule.pm is located and compiled. You didn't declare a new package so everything is loaded into your current package. Then mymodule->import() is attempted. That doesn't exist so nothing happens. Your next module use()'s mymodule. Now only mymodule->import() is called. This is how you tell perl that this file has things to export. When you don't use packages and Exporter then perl will assume it's already compiled the thing and not bother trying again.

      Here are some workarounds:

      • use do() instead and name the file specifically. That'll run regardless of whether perl has seen it before or not.
      • Remove the mymodule.pm entry in %INC in a BEGIN block just prior to use()ing it. BEGIN { delete $INC{'mymodule.pm'} }
      • Add the code I said you were missing. It can't be helped, really.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        Thanks for clarification. I removed mymodule into main namespace (and call those functions as main::some_func() within package). Everything works fine now.