in reply to Module Undefined Subroutines, works one way - not another

You have a case where ModA uses ModB (indirectly) and ModB uses ModA.

If ModA uses ModB, ModB uses ModA, and both ModA and ModB export symbols, you have a bad design.

If ModA uses ModB, ModB uses ModA, and both ModA and ModB export symbols, one needs to pay attention to code execution order. The best way I've found to avoid problems is to do:

# ModA.pm use strict; use warnings; package ModA; BEGIN { our @ISA = qw( Exporter ); our @EXPORT_OK = qw( ... ); require Exporter; } use This; use ModB; use That; ... 1;
# ModB.pm use strict; use warnings; package ModB; BEGIN { our @ISA = qw( Exporter ); our @EXPORT_OK = qw( ... ); require Exporter; } use This; use ModA; use That; ... 1;

Basically, move your use statments from where they to after your BEGIN.

Replies are listed 'Best First'.
Re^2: Module Undefined Subroutines, works one way - not another
by duane_ellis (Initiate) on Jan 11, 2007 at 22:24 UTC
    If ModA uses ModB, ModB uses ModA, and both ModA and ModB export symbols, you have a bad design.

    I do not think creating ONE huge source module with 300K to 500K of code is a good idea... That's why I broke the modules up into functionality areas. ie: "Report", "User", etc...

    Perhaps I come from an old school - no function - longer then 1 or 2 screens, and group "like functions" into the same file.

    The modules use each other - effectively via "callbacks", yea, I could pass pointers to functions etc... but that overly complicates the matter *because* they will never call any other function other then then one.

    I'll try what you suggest, move the USE ater the BEGIN statements.

    I note specifically your example also has a few USE items before, then the begin, then my rats nest of USE statements. Do you mean that specific order is important? I presume the problem (my) modules go last.

    Thanks... -Duane.

      I note specifically your example also has a few USE items before

      Aye, but strict and warnings don't use any module which use them, strict and warnings don't export anything to modules from which they import, and they are pragmas more than modules.

      They are before the BEGIN block so that they affect the code before and in the BEGIN block.