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

When I say:

use asdf;

What I am really saying is use warnings Or maybe use warnings; use strict;

In other words, I want a single module that pollutes the caller's namespace with other modules that it didn't request. How do I do this? I guess I want to do something similar to Acme::Code::Police. But according to its pod, Ovid's module has problems.

Replies are listed 'Best First'.
Re: How do I make one package use a module from another package?
by antirice (Priest) on Jul 22, 2003 at 05:47 UTC

    Ok. I may be opening Pandora's box on this one, but if you wish to force a calling module to use strict and use warnings, then this will do the trick for perl 5.8.0...WARNING...I cannot guarntee this code will work for all versions of perl since the handling of strict and warnings has changed over the years and may continue to change...DO NOT USE IN PRODUCTION CODE:

    package strictandwarnings; sub import { shift; $^H = 1538; # turn on strict $^W = 1; # turn on warnings } 1;

    This is a dirty dirty hack. Check out perldoc perlvar for information on the variables used. Instead of making them use this module to get them to use strict and warnings, try a cattle prod. It worked at my office.

    Update: But this does the trick and isn't as bad...unless strict->import() and warnings->import() stop returning the value of $^H and ${^WARNING_BITS}, respectively:

    package strictandwarnings; sub import { require strict; $^H = strict->import(); require warnings; ${^WARNING_BITS} = warnings->import(); }

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      If you are going to do something like this, you should probably observe the same rules that strict.pm does.
      $^H |= 1538; # turn on strict
Re: How do I make one package use a module from another package?
by jryan (Vicar) on Jul 22, 2003 at 14:03 UTC

    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.

      Bob is right. This code doesn't work. :(

      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.

Re: How do I make one package use a module from another package?
by rir (Vicar) on Jul 22, 2003 at 13:16 UTC
    A side issue: You should keep in mind that  warnings and  strictare not modules. They are pragmas. As such they go into block scopes not into packages.

    You cannot do what you want with pragmas only with modules.

    A side side issue: requireing pragmas is not what one is apt to want.

    #!/usr/bin/perl require strict; # require is too late $var = 1; # strictures are not in effect
Re: How do I make one package use a module from another package?
by Anonymous Monk on Jul 22, 2003 at 04:33 UTC
    Let me reword this please:

    Forget about Acme::Code::Police. I didn't get the joke or examine the source code as I should have. Just forget that I brought it up.

    What I want to do:

    I want a single module that pollutes the caller's namespace with other modules that it didn't request. Bob says I should use inheritance. What's wrong with exporting the unrequested modules by default?

Re: How do I make one package use a module from another package?
by NetWallah (Canon) on Jul 22, 2003 at 05:35 UTC
    If I understand this right, you want to export unsolicited names into the caller's namespace. The code below(untested) should work:
    #Caller program use GIVEMEJUNK;
    The GIVEMEJUNK module can be written as:
    package GIVEMEJUNK; my @importfrom = qw(DBI Acme::Code::Police ...); # List of modules you want to get stuff from. foreach(@importfrom){ use $_; #Export everything we get from each module push @EXPORT, $_::@EXPORT; } 1;

      It can be written that way. It doesn't work, however, at least not in 5.8.0. on linux. -- for no disclaimer.

      Update: Ooops, it did say "(untested)".

      --Bob Niederman, http://bob-n.com
Re: How do I make one package use a module from another package?
by Anonymous Monk on Jul 23, 2003 at 00:26 UTC
    Why can't I export subroutines and modules???

    Perhaps I could use the global @ISA variable? I am really grabing at straws here. I don't know how to figure this one out.

      It can easily be done. I posted in the above node some example code on how to do it, but unfortunately the solution contained a typo, and the node was clouded over by critism from those who couldn't look past that. The example should work fine now; please give it a try.

        Hi jryan,

        What about doing this?:

        package whatever; require Exporter; use anything; push(@EXPORT, @anything::EXPORT);

        I got this snippet from the source of LWP::Simple. I am not sure what is going on, but it worked. But I can't export strict for some reason. I could export other modules like diagnostics. What is going on here? And is your way better? Thanks.

Re: How do I make one package use a module from another package?
by bobn (Chaplain) on Jul 22, 2003 at 04:03 UTC

    The question isn't 'how' but 'why'? The thing you say you want to do,nobody should do. Is this some sort of joke?

    You should read the source for the module you reference. Hint: Don't use from any program you'd ever like to use again.

    --Bob Niederman, http://bob-n.com
      He he... yes, I just noticed the unlink in there. Silly me. I guess that module was a bad example!

      Oh great, now I can't modify my original post because I did it anonymously. I wonder how many people are going to point out what you just did. :)

        I may have meisunderstood your original post. If you want to use strict; use warnings, why not just do so?

        Those 2 things can't be doen from a separate module to affect the calling program - their effect is lexically scoped, so it can apply, at most, to the file it is in.

        Anyhow, I saw the word "pollute" and thought you were talking about a module that would import from yet other modules and then export to main:: and I can't help thnking that that way lies madness. Large function modules that break things down into smaller modules do so by inheritance, not import/export. Look at LWP::UserAgent for example, and try to find where the headers are set/interpreted, etc.

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

        Reply to your original post with your corrections. Use tags to emphasize your corrections and catch peoples' eyes, say <b>corrected stuff</b>

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