G'day choroba,

++ Many thanks for this.

The namespace extension ($ns_extension) can contain several parts, e.g. A::B::C, so I made a slight adjustment to handle that. I also pared the code, bit by bit, to end up with the simplified:

sub dynamic_require { my ($self, $ns_extension) = @_; my $class = join '::', __PACKAGE__, $ns_extension; my $source = <<~EOF; package $class; use parent 'MooselessRequireHookTest'; 1; EOF push @INC, sub { my ($coderef, $filename) = @_; my $wanted_filename = $class =~ s{::}{/}gr . '.pm'; return unless $filename eq $wanted_filename; return \$source; }; eval "require $class;"; return; }

I added a couple more tests:

$rht->dynamic_require('A::B::C'); my $rht_abc = MooselessRequireHookTest::A::B::C->new(); is(defined $rht_abc, 1, 'Test MooselessRequireHookTest::A::B::C->new() +'); isa_ok($rht_abc, 'MooselessRequireHookTest::A::B::C', 'Test MooselessR +equireHookTest::A::B::C->new() ISA');

I also added this temporary debug line after unpacking @_:

warn "\$coderef[$coderef] \$filename[$filename]\n";

Now, all tests are successful and I have a little more insight into what is going on:

sscce_mooseless_require_hook_test.t .. 1..8 ok 1 - Test MooselessRequireHookTest::->new() ok 2 - 'Test MooselessRequireHookTest::->new() ISA' isa 'MooselessRequ +ireHookTest' ok 3 - Test MooselessRequireHookTest::Test1->new() ok 4 - 'Test MooselessRequireHookTest::Test1->new() ISA' isa 'Mooseles +sRequireHookTest::Test1' ok 5 - Test MooselessRequireHookTest::Test2->new() ok 6 - 'Test MooselessRequireHookTest::Test2->new() ISA' isa 'Mooseles +sRequireHookTest::Test2' ok 7 - Test MooselessRequireHookTest::A::B::C->new() ok 8 - 'Test MooselessRequireHookTest::A::B::C->new() ISA' isa 'Moosel +essRequireHookTest::A::B::C' $coderef[CODE(0x60008aa00)] $filename[MooselessRequireHookTest/Test1.p +m] $coderef[CODE(0x60008aa00)] $filename[MooselessRequireHookTest/Test2.p +m] $coderef[CODE(0x600868c78)] $filename[MooselessRequireHookTest/Test2.p +m] $coderef[CODE(0x60008aa00)] $filename[MooselessRequireHookTest/A/B/C.p +m] $coderef[CODE(0x600868c78)] $filename[MooselessRequireHookTest/A/B/C.p +m] $coderef[CODE(0x60086da88)] $filename[MooselessRequireHookTest/A/B/C.p +m] ok All tests successful. Files=1, Tests=8, 0 wallclock secs ( 0.00 usr 0.03 sys + 0.08 cusr + 0.03 csys = 0.14 CPU) Result: PASS

The mechanism behind this is a mystery to me. Why is it calling the subroutine with all the previous CODEREF values then the newly created CODEREF? Anyway, what you provided weeds out all of the unwanted parts. @INC ends up with: the use lib dir; the 4 usual dirs; and 3 (hook) CODEREFs.

/home/ken/tmp/pm_sscce_require_hook/lib /home/ken/perl5/perlbrew/perls/perl-5.32.0/lib/site_perl/5.32.0/cygwin +-thread-multi /home/ken/perl5/perlbrew/perls/perl-5.32.0/lib/site_perl/5.32.0 /home/ken/perl5/perlbrew/perls/perl-5.32.0/lib/5.32.0/cygwin-thread-mu +lti /home/ken/perl5/perlbrew/perls/perl-5.32.0/lib/5.32.0 CODE(0x60008aa00) CODE(0x600868c78) CODE(0x60086da88)

As I suspected, the issue had nothing to do with Moose. I applied these changes to my original module and all of its tests now pass. Thanks again.

— Ken


In reply to Re^6: require() @INC hooks problem [non-Moose] by kcott
in thread require() @INC hooks problem by kcott

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.