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

Good Morning Monks,

I've spent a good amount of time reading previous posts about how this error is generally created: by redefining a subroutine that has previously been defined in the script or an imported script.

What if after searching through those modules which are imported, I don't find a matching subroutine that I'm apparently redefining?

Could there be yet another reason why this is occuring?

As far as I'm aware (this could be embarassing...), I'm not calling the subroutines incorrectly...

$return_value = &greater($arg1, $arg2);
sub greater { my ($arg1, $arg2) = @_; return $arg1 > $arg2 ? $arg1 : $arg2; }

If anyone has any suggestions, that'd be fantastic. I don't want to strictly disable the particular warning either. I'm just not that ecstatic about my logs being bloated.

I've also checked all of my other completely seperate scripts on a whim and found no other subroutines with the same name.

I could post an actual subroutine, if someone might find it of some value...

Replies are listed 'Best First'.
Re: Another Subroutine Redefined Post
by toolic (Bishop) on Nov 10, 2009 at 18:15 UTC
    Two things you can try:

    Start deleting lines of code until the warning disappears. I can not reproduce the warning with the code that you have posted.

    Inspect %INC to make sure you are looking in all the modules you think you are looking in:

    use Data::Dumper; print Dumper(\%INC);

    Update: Or try this hack:

    for my $file (values %INC) { open my $fh, '<', $file; while (<$fh>) { print "$file: $_" if /\bsub\s+greater\b/ } }

      Well, the code I provided was more of a "this is how I do it" in the case that I do it wrong, someone can point it out.

      That's a good idea though; I didn't think of %INC.

      I've checked out all of the included files in %INC, still nothing shows up. Any other suggestions?

        Maybe Devel::Symdump can help you locate the duplicate subs:
        use Data::Dumper; use Devel::Symdump; my $s = Devel::Symdump->new(); print Dumper($s->functions());
Re: Another Subroutine Redefined Post
by JadeNB (Chaplain) on Nov 10, 2009 at 23:24 UTC
    I could post an actual subroutine, if someone might find it of some value...

    Yes, let's see! As others have mentioned, your code doesn't have anything visibly wrong with it in isolation (although you probably want to call your subroutine using the modern syntax greater(@args) rather than &greater(@args)).

    I think just as useful as the actual code snippet is the exact error message, wording, line numbers, and all.

      Rather than posting an entire script (because I don't have a small example), here's another thought to mull over...

      In one particular case, the error "subroutine X redefined at ..." appears only sometimes. I was able to duplicate it, not by refreshing my page, but by navigating back and reclicking the link.

      Could this have something to do with a previously-loaded subroutine in my server process' memory that's being redefined?

        I'm a little confused—you say “I could post an actual subroutine”, but then say that you don't want to post a code snippet?

        Anyway, I think that it's important to realise that Perlmonks are happy to give you all the help we can, but that you have to show that you're willing to meet us halfway—saying “I won't take the effort to make a small code sample, but you should take the effort to guess why a problem might be occurring anyway” isn't likely to lead anywhere useful.

Re: Another Subroutine Redefined Post
by Khen1950fx (Canon) on Nov 10, 2009 at 23:03 UTC
    I used Sub::Installer to reinstall the sub. I haven't been able to replicate the warning though:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Devel::Symdump; use Sub::Installer; my $PackageName = 'mysub'; my $sub = \&greater; $PackageName->reinstall_sub({$sub => \&greater}); my $s = Devel::Symdump->new(); print Dumper($s->functions()); sub greater { my ($arg1, $arg2) = @_; print $arg1 > $arg2 ? $arg1 : $arg2; }