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

Hi,

Is there a way to either override or ignore the export of variables/subroutines from a module? I ask because sometimes I run into modules either in CPAN or some homebrew monstrosity where the namespaces collide.

thanks

Jason L. Froebe

Team Sybase member

No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

  • Comment on how NOT to import exported subs/variables from a module?

Replies are listed 'Best First'.
Re: how NOT to import exported subs/variables from a module?
by Sandy (Curate) on May 30, 2006 at 19:36 UTC
    From the docs...

    perldoc -f use ... If you do not want to call the package's "import" method (for instance, to stop your namespace from being altered), explicitly supply the empty list: use Module ();
    This should do it.

    Sandy

      The next two lines of that documentation are:

      That is exactly equivalent to BEGIN { require Module }

      So unless you need the compile time action from BEGIN, plain require works also. It's only one extra character over use with an empty list and I find it stands out better visually as being a module load without import side-effects.

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

        So unless you need the compile time action from BEGIN, plain require works also. It's only one extra character over use with an empty list and I find it stands out better visually as being a module load without import side-effects.

        That's interesting. I would far prefer use Module () since it explicitly says to me "I am deliberately not importing anything' where require Module might be saying any of:

        • I don't want to run at compile time
        • I don't want to run import() yet
        • I don't want to run import() at all (for whatever reason)
        • I don't want to run import() because I don't want to add any subs/variables to my namespace

      DOH!

      You know, I could swear that was just added to my perldoc in the last 10 seconds by elves or something... Thanks!! :)

      Jason L. Froebe

      Team Sybase member

      No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

        IMHO, that sounds more like something the wee folk would do... ;-)

        planetscape