gdanko
gdanko

Reputation: 1992

Perl 1,000 subroutine redefined

I have a Perl script which uses Module::Reload::Selective to load a module. The perl script looks like this, more or less.

#!/usr/bin/perl

use strict;
use warnings;
use Module::Reload::Selective;

&reload;

sub reload {
    Module::Reload::Selective->reload(qw(myModule));
    import myModule qw($reloadtime);
    $reloadtime = ${myModule::reloadtime};
}

The module looks like this:

package myModule;

use Config::General;
use Data::Dumper;
use DBI;
use HTTP::Request::Common qw { POST };
use HTTP::Headers;
use JSON::XS;
use LWP::UserAgent;
use Module::Pluggable search_path => "Bot";
use POSIX qw(strftime ceil);

# stuff here
1;

When I run the main script I get about 100 like this:

Subroutine export_fail redefined at /usr/local/share/perl/5.14.2/Carp.pm line 64.
Subroutine _cgc redefined at /usr/local/share/perl/5.14.2/Carp.pm line 66.
Subroutine longmess redefined at /usr/local/share/perl/5.14.2/Carp.pm line 72.
Subroutine shortmess redefined at /usr/local/share/perl/5.14.2/Carp.pm line 92.
Subroutine croak redefined at /usr/local/share/perl/5.14.2/Carp.pm line 100.
Subroutine confess redefined at /usr/local/share/perl/5.14.2/Carp.pm line 101.
Subroutine carp redefined at /usr/local/share/perl/5.14.2/Carp.pm line 102.

I noticed if I comment out some of the "use" statements in my module these will go away. But I need those. I've searched all over and tried a number of things. to no avail.

Upvotes: 3

Views: 1710

Answers (2)

Hani Zaccarelli
Hani Zaccarelli

Reputation: 11

I had this too. In my case I tracked the error down to the plugins() call in Module::Pluggable. If that's where your noise comes from too this might work for you also.

answer is in the weeds here: http://cpansearch.perl.org/src/SIMONW/Module-Pluggable-5.1/lib/Module/Pluggable.pm

where it explains that plugins() is called more than necessary which can get expensive, so you do something like this:

package Foo;
  use strict;
  use Module::Pluggable sub_name => '_plugins';

  our @PLUGINS;
  sub plugins { @PLUGINS ||= shift->_plugins }
  1;

That didn't exactly work for me at first, but it did when I fleshed out the plugins() sub into several lines and populated/returned an array ref in my $self.

Upvotes: 1

mob
mob

Reputation: 118665

Sometimes it is ok to redefine subroutines. If you know what you are doing and want to suppress the warnings, just put

no warnings 'redefine';

at the top of your reload method.


Another option, again, so long as you know what you doing, is to temporarily disable the builtin warnings handler:

sub reload {
    local $SIG{__WARN__} = sub {};
    ... do something that warns ...
}

And as a last resort, since warnings are written to STDERR, you can temporarily redirect STDERR.

sub reload {
    open my $devnull, '>/dev/null';  # Windows: >nul
    local *STDERR = *$devnull;
    ... do something that warns ...
}

Upvotes: 2

Related Questions