Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Recursive use causes subroutine redefinition

by prowler (Friar)
on Sep 08, 2004 at 07:33 UTC ( [id://389286]=perlquestion: print w/replies, xml ) Need Help??

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

I have some modules that use each other and am getting subroutine redefined warnings when compilation is done (using perl -c) on one of the modules.

Some sample code that does what I mean is:

Foo.pm:
#!/usr/bin/perl use warnings; use strict; use lib '/tmp/'; package Foo; use Bar; sub new { my $invocant = shift; my $class = ref($invocant) || $invocant; my $self = {}; return $self; } sub get_bar { return Bar->new(); } 1;
Bar.pm
#!/usr/bin/perl use warnings; use strict; use lib '/tmp/'; package Bar; use Foo; sub new { my $invocant = shift; my $class = ref($invocant) || $invocant; my $self = {}; return $self; } sub get_foo { return Foo->new(); } 1;
Running perl -c Foo.pm gives
Subroutine new redefined at Foo.pm line 13. Subroutine get_bar redefined at Foo.pm line 22. Foo.pm syntax OK

This is mainly irritating as it adds to the list of errors and warnings that appear in the Problems tab of Eclipse when editing.

I know that I can disable warning check for those problems (which I don't want to do as it may hide a real problem), or I can remove each module's use of the other and just ensure that the scripts that call these modules use both of them, but it seems to me that this sort of design should be valid.

Does anyone have any ideas of how to cross use modules without having subroutine redefinition issues?

Thanks,

Prowler
 - Spelling is a demanding task that requies you full attention.

Replies are listed 'Best First'.
Re: Recursive use causes subroutine redefinition
by ysth (Canon) on Sep 08, 2004 at 08:49 UTC
    Nothing is wrong with your recursive uses; the problem is that you are also loading Foo.pm as a script, bypassing the multiple load checks that exist for require or use. The same thing happens without the recursion:
    $ cat>Baz.pm package Baz; use strict; use warnings; use Baz; sub baz { } 1; $ perl Baz.pm Subroutine baz redefined at Baz.pm line 5.
    If you are going to use the same file both via use or require and as a main script or via do, you should manually put a "BEGIN { $INC{Module.pm} ||= __FILE__ }" in Module.pm before any possible use/require.

      So the use/require check is based on whether the module has been loaded by use/require before, and doesn't count the explicit loading of the module at the command line? Thanks for the info - that explains why no warnings were output when the script was actually in use then.

      As I hinted at in the original message (but didn't make very clear) the reason that I noticed (and was annoyed by) those warnings was that Eclipse (with the Perl plugin) does compile time checking of code and provides a list of the errors/warnings it can find - this was being cluttered up by those warnings.

      Prowler
       - Spelling is a demanding task that requies you full attention.

Re: Recursive use causes subroutine redefinition
by tachyon (Chancellor) on Sep 08, 2004 at 07:49 UTC

    Change the use to require and scope it to where it is actually required the problem will go away.

    cheers

    tachyon

      Thanks for that.

      Prowler
       - Spelling is a demanding task that requies you full attention.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-19 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found