Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Modules included more than once

by physgreg (Scribe)
on Aug 22, 2002 at 10:50 UTC ( #191975=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks, I'm currently on a hunt for warnings through our codebase. I've got rid of all of them except a lot of
Subroutine XXXX redefined at YYYY.pm line 1584
which forms a list of all subroutines in YYYY. This implies that the package is being included more than once. However, there are a lot of interdependencies between these modules, so simply removing references to YYYY from all but the parent package did not work.
What I would like to know is if there is any way to stop this happening, or will I just have to live with the warnings? The feature I have in mind is similar to the PHP command 'require_once'.
Thanks,
Greg.

Replies are listed 'Best First'.
Re: Modules included more than once
by bart (Canon) on Aug 22, 2002 at 11:11 UTC
    Subroutine XXXX redefined at YYYY.pm line 1584

    which forms a list of all subroutines in YYYY. This implies that the package is being included more than once.

    What I would like to know is if there is any way to stop this happening, or will I just have to live with the warnings? The feature I have in mind is similar to the PHP command 'require_once'.

    You shouldn't have to remove those interdependencies. They shouldn't matter.

    What platform? Windows? I bet it is.

    My guess is that there's a problem with the case. If you do

    use Foo; use foo;
    on Windows, that will indeed load Foo.pm twice, as foo.pm is the same file.

    Just use the proper case everywhere.

      Thanks for the input, however we're running on a Linux box, so the use command is working as expected. These packages are used by each other, but are also used in other modules, which may or may not use both of them. It really is terrible spaghetti code, and I'm the poor sucker who's got the job of maintaining it!
        Thanks for the input, however we're running on a Linux box, so the use command is working as expected.

        Blimey, there goes the simple theory. Whatever you do, do not simply ignore these warnings! There's something really fishy going on.

        As for your snippet:

        use SUPPORT; use SUPPORT qw( %typeConvert %enumeratedTypes);
        This should be perfectly fine. What happens is that the module, SUPPORT.pm, is loaded (thus compiled) once, but that SUPPORT->import is called twice, once without any extra parameters, and once with those two (as strings). So this can't trigger this warning.

        I do suspect that indeed the same subs are defined twice, perhaps in different files. Scan your library file tree.

        And perhaps one of the development tool modules can help analyze your compiled code. I'd try with B::Xref (call script with -MO=Xref) first.

Re: Modules included more than once
by Sifmole (Chaplain) on Aug 22, 2002 at 11:06 UTC
    The "use" command is similar to PHP's "require_once" command, so you may want to look into that. Also have you confirmed that there is only one subroutine with the name XXXX in all the packages you are including? Perhaps you left an old version of the routine laying around...
      I've done a bit more digging, and it appears that there is a circular include going on:
      package OBJECT_SERVICES; use SUPPORT; use SUPPORT qw( %typeConvert %enumeratedTypes);
      and
      package SUPPORT; use OBJECT_SERVICES;
      I'm not sure how to get round this, as both packages use functions from the other package. I know this looks terrible.
      Would it make sense to make another package containing the functions used by both packages, and use that in both, rather than the way it's done now?
      Thanks,
      Greg.
        the problem is not in circular includes. circular includes are just fine. I suspect the problem is in:
        package OBJECT_SERVICES; use SUPPORT; use SUPPORT qw( %typeConvert %enumeratedTypes);
        why do you use SUPPORT twice? the second use should be enough.

        cheers,
        Aldo

        King of Laziness, Wizard of Impatience, Lord of Hubris

Re: Modules included more than once
by dada (Chaplain) on Aug 22, 2002 at 11:05 UTC
    well, you can filter off these warnings. put these lines at the top of your script:
    BEGIN { $SIG{__WARN__} = sub { print STDERR $_[0] unless $_[0] =~ /Subroutine .* redefined/ } }
    cheers,
    Aldo

    King of Laziness, Wizard of Impatience, Lord of Hubris

Re: Modules included more than once
by blakem (Monsignor) on Aug 22, 2002 at 16:30 UTC
    You wouldn't happen to be using Apache::Reload would you? If so, you should be able to safely ignore the redefined warnings it sometimes throws. My mod_perl modules always start with:
    use strict; use warnings; use Apache::Reload; no warnings 'redefine';
    which will suppress the redefined warnings.

    -Blake

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://191975]
Approved by andye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2022-10-05 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (25 votes). Check out past polls.

    Notices?