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

> "Why is it calling the subroutine with all the previous CODEREF values then the newly created CODEREF?"

I don't know what that means.

> With regard to your suggestion of "Just eval the code and update %INC", I'm not following you in terms of how that might be implemented. Could you provide a code example?

something like (totally untested)

sub create_class { my ($class) = @_; my $source = <<~EOF; package $class; use parent 'MooselessRequireHookTest'; 1; EOF eval $source; my $name = $class =~ s(::)(\/)gr; $INC{"$name.pm"} = 'imported via eval'; }

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

Replies are listed 'Best First'.
Re^6: require() @INC hooks problem
by LanX (Saint) on Dec 28, 2020 at 16:35 UTC
    here tested code
    use strict; use warnings; use Data::Dump qw/pp dd/; sub create_class { my ($class) = @_; my $source = <<"__CODE__"; package $class; sub import { warn "$class imported"; } __CODE__ eval $source; my $name = $class =~ s(::)(\/)gr; $INC{"$name.pm"} = 'imported via eval'; } BEGIN { create_class('Jabba::Dabba'); } pp \%INC; use Jabba::Dabba;

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

      fixed module template, less Perl version dependencies, more flexible and untangled templating

      use strict; use warnings; use Data::Dumper; sub test_tmpl { my ($name) = @_; return $name, <<"__CODE__"; package $name; sub import { warn "$name imported"; } 1; __CODE__ } sub create_module { my ( $module, $source ) = @_; eval $source; (my $name = $module) =~ s(::)(\/)g; my @caller = caller(); $INC{"$name.pm"} = "imported via eval at @caller"; } BEGIN { create_module( test_tmpl('Jabba::Dabba') ); } use Jabba::Dabba; warn Dumper \%INC;

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

Re^6: require() @INC hooks problem
by kcott (Archbishop) on Dec 29, 2020 at 04:11 UTC

    I wrote my last reply at 3.13am (my time). I did see your response about 10 minutes later but my eyes were getting pretty blurry by then: I decided some sleep was in order before looking into that further. :-)

    Twelve hours later, and much refreshed, I see you've posted a lot more code. Thanks for that — I'll look into it and see how that fits with my existing code.

    — Ken