in reply to How do I make one package use a module from another package?

Well, its easy, just use the packages in the caller's namespace:

use Carp qw(croak); require Exporter; our @ISA = qw(Exporter); sub import { my ($pkg) = (caller)[0]; eval qq[ package $pkg; use strict; use warnings; use Whateverelseyouwant; ]; croak $@ if $@; # optionally, if you still need import: goto \&Exporter::import; }

Update: Added error handling.

Replies are listed 'Best First'.
Re: Re: How do I make one package use a module from another package?
by Anonymous Monk on Jul 22, 2003 at 22:29 UTC
    Bob is right. This code doesn't work. :(
Re: Re: How do I make one package use a module from another package?
by bobn (Chaplain) on Jul 22, 2003 at 22:23 UTC

    It's a lot harder if you require the code to work. -- to you for no disclaimer.

    --Bob Niederman, http://bob-n.com

      Excuse me, that should have been (caller)[0]. However, -- to you for condemning the code without understanding that it was essentially correct sans a typo.

      The code I provided is the code that you want to use in this situation.

        The code I provided is the code that you want to use in this situation.

        No the code you initially provided was broken in multiple ways.

        Once you "fixed" it, it exported. This is interesting, and it's worth understanding how you did it, so I'm glad you made that contributuion. (I still think it's a really bad idea for reasons stated elsewhere.)

        Also, even "fixed", its "use strict" and "use warnings" still have no effect, which is expected from an understanding that these pragma apply lexically.

        I often test code before I post it here. If I don't test it, I usually manage to remember to say "untested" somewhere, in a noticeable fashion. I think doing one of those 2 things is important. But I may have been "over the top" in my previous.

        --Bob Niederman, http://bob-n.com