Perl - Subroutine redefined

后端 未结 9 1832
别那么骄傲
别那么骄傲 2020-12-14 16:19

I have asked this question before or searched and seen others ask - why am I getting the warning \"Subroutine mySub redefined at ../lib/Common.pm line x\"? and you

9条回答
  •  难免孤独
    2020-12-14 17:12

    This sounds like a problem caused by circular dependencies. Here is how to track it down. If your problem class looks like this:

    package AlientPlanet;
    use Dinosaurs;
    sub has_dinosaurs {...}
    1;
    

    Then change your example to look like this:

    package AlienPlanet;
    sub has_dinosaurs {...}     # <-- swap
    use Dinosaurs;              # <-- swap
    1;
    

    Now compile your code with Carp::Always like this:

    ⚡ perl -MCarp::Always -c lib/AlienPlanet.pm                                                                                                            
    Subroutine has_dinosaurs redefined at lib/AlienPlanet.pm line 4.
        require AlienPlanet.pm called at lib/Dinosaurs.pm line 4
        Dinosaurs::BEGIN() called at lib/AlienPlanet.pm line 4
        eval {...} called at lib/AlienPlanet.pm line 4
        require Dinosaurs.pm called at lib/AlienPlanet.pm line 5
        AlienPlanet::BEGIN() called at lib/AlienPlanet.pm line 4
        eval {...} called at lib/AlienPlanet.pm line 4
    lib/AlienPlanet.pm syntax OK
    

    Now that you have a stacktrace you can see where the loop is. The quick and dirty solution is to use Class::Load in Dinosaurs.pm.

    For a more detailed explanation try my blog post.

提交回复
热议问题