On Tue, Jan 25, 2011 at 12:23:44PM -0600, Jesse Luehrs wrote:
> On Tue, Jan 25, 2011 at 12:05:41PM -0600, Nick Perez wrote:
> > Honestly, I feel this is an MXD bug. Florian might have a work around,
> > but the real answer is for this odd behavior to be fixed.
> 
> Not sure I agree... circular use/require things are just as much of a
> problem in normal perl:
> 
>   # Foo.pm
>   package Foo;
>   use Baz;
>   sub quux { }
>   warn "Foo loaded";
>   1;
> 
>   # Bar.pm
>   package Bar;
>   use Baz;
>   sub quux { }
>   warn "Bar loaded";
>   1;
> 
>   # Baz.pm
>   package Baz;
>   require Foo;
>   require Bar;
>   warn Foo->can('quux');
>   warn Bar->can('quux');
>   1;
> 
> $ perl -MFoo -e1
> Bar loaded at Bar.pm line 4.
> Warning: something's wrong at Baz.pm line 4.
> CODE(0x1423730) at Baz.pm line 5.
> Foo loaded at Foo.pm line 4.
> 
> $ perl -MBar -e1
> Foo loaded at Foo.pm line 4.
> CODE(0xd28730) at Baz.pm line 4.
> Warning: something's wrong at Baz.pm line 5.
> Bar loaded at Bar.pm line 4.
> 
> $ perl -MBaz -e1
> Foo loaded at Foo.pm line 4.
> Bar loaded at Bar.pm line 4.
> CODE(0x21be4a8) at Baz.pm line 4.
> CODE(0x21be700) at Baz.pm line 5.
> 
> You really just have to be aware of how perl handles this, and avoid
> having circular things to begin with.

I just released circular::require to CPAN to help diagnose these kinds
of issues:
http://search.cpan.org/~doy/circular-require-0.01/lib/circular/require.pm

Running the above snippets with this produces output like this:

$ perl -M-circular::require -MFoo -e1
Circular require detected: Foo.pm (from Baz)
Circular require detected: Baz.pm (from Bar)
Bar loaded at Bar.pm line 4.
Warning: something's wrong at Baz.pm line 4.
CODE(0xc078c8) at Baz.pm line 5.
Foo loaded at Foo.pm line 4.

Keep in mind that not all circular dependencies like this are
necessarily bad (there are a few within Moose, for instance), but if you
notice them in your own code, and you didn't put them there
intentionally, this can be a good indicator of possible problems.

-doy

Reply via email to