Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

In a nutshell:

sub reload { my ($PM) = @_ or return; $PM =~ s!::!/!g; $PM .= ".pm"; delete $INC{$PM};

We return unless we get something to load. After that, we convert all ::'s to / and append .pm to the end of our module name because that's how it's stored in %INC and it keeps us from needing to eval string.

no strict 'refs'; no warnings 'redefine';

Just what it says.

my $warnings = \&warnings::import; local *warnings::import = sub { &{$warnings}; unimport warnings "redefine"; };

Ok. The magic happens here. $warnings contains a coderef to the warnings import method. It's very important that we keep this since warnings.pm may change between versions and we'd very much enjoy this not breaking just because we have a new warnings module. Anywho, the next line localizes the warnings::import typeglob. To this, we assign a subroutine that first calls our old warnings::import method with all the same parameters. Notice that I call it as &{$warnings} instead of $warnings->(). I could have done $warnings->(@_) as well, but what fun is that? Anyhow, since we imported whatever the calling script/module wanted we need to unimport the redefine warning immediately afterwards. Remember that this gets called from the module in question whenever it uses warnings. Thus it will affect the $^WARNING_BITS variable at that point.

eval { require $PM }; }

I love eval braces. Of course, it's used here because if require can't find the module it will kill the script. Just check the return from reload::reload to make certain the module did in fact load. Also of note is that at the end of the subroutine, warnings::import is magically restored to its old import method.


In reply to Re: Re: Re: Reloading modules- suppressing warnings works sometimes? by !1
in thread Reloading modules- suppressing warnings works sometimes? by JPaul

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-24 22:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found