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

Hello, monks... I have a problem. I have a module (M), "use"d by a package (P), and the package is imported into main with "use". I want to "use" the module M in main's namespace as well. When i do the "use", however, the symbols from module M are not imported into the namespace because module M is already in %INC. What is the cleanest way to handle such a situation? I cannot find this problem addressed in the camel. Thanks...
  • Comment on Importing symbols into main when library is already in %INC

Replies are listed 'Best First'.
Re: Importing symbols into main when library is already in %INC
by merlyn (Sage) on May 16, 2001 at 21:07 UTC
    %INC prevents only the re-requiring of the module M. Although your description is unclear, after three re-readings it appears you have use M in both main and P. If that's true, then M.pm will get read once, but import will be called twice, and properly import the symbols into both P and main.

    To go further, I'd need to see code, or a clearer explanation.

    -- Randal L. Schwartz, Perl hacker

      Ok, I paired it down a little and noticed something different this time. NETWORK::SNMP is a module I use to define OIDs and community strings. NETWORK::Discovery is a package that uses NETWORK::SNMP. I wrote and executed the two different scripts below. Why is it that if I say "use NETWORK::Discovery;" before I say "use NETWORK::SNMP", the variable $commstring is not defined in package main?
      nmss_/var/scripts # cat test.pl #!/usr/bin/perl -w use NETWORK::SNMP; use NETWORK::Discovery; print "$commstring\n"; nmss_/var/scripts # ./test.pl not_real_community_string nmss_/var/scripts # cat test2.pl #!/usr/bin/perl -w use NETWORK::Discovery; use NETWORK::SNMP; print "$commstring\n"; nmss_/var/scripts # ./test2.pl Name "main::commstring" used only once: possible typo at ./test2.pl li +ne 6. Use of uninitialized value in concatenation (.) at ./test2.pl line 6.