Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: require() @INC hooks problem

by kcott (Archbishop)
on Dec 27, 2020 at 21:06 UTC ( [id://11125829]=note: print w/replies, xml ) Need Help??


in reply to Re: require() @INC hooks problem
in thread require() @INC hooks problem

G'day Rolf,

Short answer: no.

The module that I'm working on is part of a series, all of which use the Moose framework. The dynamic_require() idea seemed like a nice thing to have; but, at the end of the day, if it doesn't work with Moose for some reason, I can live without it.

In earlier testing with the real module, I did find that hard-coding the $source into a test file worked fine, even with multiple instances (e.g. the source generated with "Test1" and "Test2").

The problem does seem to be somewhere in dynamic_require(); however, I've no idea where that might be. The fact that it works once, but not twice, is just very puzzling.

— Ken

Replies are listed 'Best First'.
Re^3: require() @INC hooks problem
by LanX (Saint) on Dec 27, 2020 at 21:11 UTC
    Short answer: I'm willing to look into it as soon as Moose is not involved in your SSCCE ! :)

    (Moose doesn't qualify as "short" or "self contained" in my book ;-)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Out of curiousity, I took Moose out of the equation. There was no change to the problem; however, if you or someone else can solve the non-Moose issue, I suspect that solution can probably be applied to the Moose version.

      I've made no changes to the module structure: just added a new() routine; changed extends to use parent; and so on. Everything is pretty much the same except all classes are now prefixed with Mooseless.

      MooselessRequireHookTest.pm:

      package MooselessRequireHookTest; use 5.032; use warnings; sub new { my ($class) = @_; return bless {} => $class; } sub dynamic_require { my ($self, $ns_extension) = @_; { my $class = join '::', __PACKAGE__, $ns_extension; my $source = <<~EOF; package $class; use parent 'MooselessRequireHookTest'; 1; EOF my sub for_inc { my ($coderef, $filename) = @_; return \$source }; my $for_inc_ref = \&for_inc; push @INC, $for_inc_ref; eval "require $class;"; } return; } 1;

      sscce_mooseless_require_hook_test.t:

      #!perl use 5.032; use warnings; use FindBin; use lib "$FindBin::Bin/lib"; use Test::More tests => 6; use MooselessRequireHookTest; my $rht = MooselessRequireHookTest::->new(); is(defined $rht, 1, 'Test MooselessRequireHookTest::->new()'); isa_ok($rht, 'MooselessRequireHookTest', 'Test MooselessRequireHookTes +t::->new() ISA'); $rht->dynamic_require('Test1'); my $rht_test1 = MooselessRequireHookTest::Test1->new(); is(defined $rht_test1, 1, 'Test MooselessRequireHookTest::Test1->new() +'); isa_ok($rht_test1, 'MooselessRequireHookTest::Test1', 'Test MooselessR +equireHookTest::Test1->new() ISA'); $rht->dynamic_require('Test2'); my $rht_test2 = MooselessRequireHookTest::Test2->new(); is(defined $rht_test2, 1, 'Test MooselessRequireHookTest::Test2->new() +'); isa_ok($rht_test2, 'MooselessRequireHookTest::Test2', 'Test MooselessR +equireHookTest::Test2->new() ISA');

      Example run:

      $ prove -v sscce_mooseless_require_hook_test.t sscce_mooseless_require_hook_test.t .. 1..6 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' Can't locate object method "new" via package "MooselessRequireHookTest +::Test2" at sscce_mooseless_require_hook_test.t line 23. # Looks like your test exited with 255 just after 4. Dubious, test returned 255 (wstat 65280, 0xff00) Failed 2/6 subtests Test Summary Report ------------------- sscce_mooseless_require_hook_test.t (Wstat: 65280 Tests: 4 Failed: 0) Non-zero exit status: 255 Parse errors: Bad plan. You planned 6 tests but ran 4. Files=1, Tests=4, 1 wallclock secs ( 0.02 usr 0.01 sys + 0.06 cusr + 0.05 csys = 0.14 CPU) Result: FAIL

      — Ken

        The problem is the first generated code is run for the second generated module, too. You can verify it by adding the following line before the push @INC:
        pop @INC if ref $INC[-1];

        Cleaner way is to check the name in the hook itself, e.g. before returning \$source, insert

        return if -1 == index $filename, $ns_extension;

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      OK, that's fine. Thanks for taking the time to make some guesses.

      — Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 05:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found