3 # Tests for the coderef-in-@INC feature
11 use File::Temp qw/tempfile/;
12 use Test::More tests => 30;
15 my ($fh,$f) = tempfile("DummyModuleXXXX", DIR => File::Spec->curdir,
17 print $fh "package ".substr($_[0],0,-3)."; 1;";
19 open $fh, $f or die "Can't open $f: $!";
25 $str =~ /(0x[0-9a-f]+)/i;
30 my ($self, $filename) = @_;
31 if (substr($filename,0,3) eq 'Foo') {
32 return get_temp_fh($filename);
41 ok( !eval { require Bar; 1 }, 'Trying non-magic package' );
43 ok( eval { require Foo; 1 }, 'require() magic via code ref' );
44 ok( exists $INC{'Foo.pm'}, ' %INC sees it' );
45 is( get_addr($INC{'Foo.pm'}), get_addr(\&fooinc),
46 ' key is correct in %INC' );
48 ok( eval "use Foo1; 1;", 'use()' );
49 ok( exists $INC{'Foo1.pm'}, ' %INC sees it' );
50 is( get_addr($INC{'Foo1.pm'}), get_addr(\&fooinc),
51 ' key is correct in %INC' );
53 ok( eval { do 'Foo2.pl'; 1 }, 'do()' );
54 ok( exists $INC{'Foo2.pl'}, ' %INC sees it' );
55 is( get_addr($INC{'Foo2.pl'}), get_addr(\&fooinc),
56 ' key is correct in %INC' );
62 my ($self, $filename) = @_;
63 if (substr($filename, 0, length($self->[1])) eq $self->[1]) {
64 return get_temp_fh($filename);
71 my $arrayref = [ \&fooinc2, 'Bar' ];
74 ok( eval { require Foo; 1; }, 'Originally loaded packages preserved' );
75 ok( !eval { require Foo3; 1; }, 'Original magic INC purged' );
77 ok( eval { require Bar; 1 }, 'require() magic via array ref' );
78 ok( exists $INC{'Bar.pm'}, ' %INC sees it' );
79 is( get_addr($INC{'Bar.pm'}), get_addr($arrayref),
80 ' key is correct in %INC' );
82 ok( eval "use Bar1; 1;", 'use()' );
83 ok( exists $INC{'Bar1.pm'}, ' %INC sees it' );
84 is( get_addr($INC{'Bar1.pm'}), get_addr($arrayref),
85 ' key is correct in %INC' );
87 ok( eval { do 'Bar2.pl'; 1 }, 'do()' );
88 ok( exists $INC{'Bar2.pl'}, ' %INC sees it' );
89 is( get_addr($INC{'Bar2.pl'}), get_addr($arrayref),
90 ' key is correct in %INC' );
95 my ($self, $filename) = @_;
96 if (substr($filename,0,4) eq 'Quux') {
97 return get_temp_fh($filename);
104 my $href = bless( {}, 'FooLoader' );
107 ok( eval { require Quux; 1 }, 'require() magic via hash object' );
108 ok( exists $INC{'Quux.pm'}, ' %INC sees it' );
109 is( get_addr($INC{'Quux.pm'}), get_addr($href),
110 ' key is correct in %INC' );
114 my $aref = bless( [], 'FooLoader' );
117 ok( eval { require Quux1; 1 }, 'require() magic via array object' );
118 ok( exists $INC{'Quux1.pm'}, ' %INC sees it' );
119 is( get_addr($INC{'Quux1.pm'}), get_addr($aref),
120 ' key is correct in %INC' );
124 my $sref = bless( \(my $x = 1), 'FooLoader' );
127 ok( eval { require Quux2; 1 }, 'require() magic via scalar object' );
128 ok( exists $INC{'Quux2.pm'}, ' %INC sees it' );
129 is( get_addr($INC{'Quux2.pm'}), get_addr($sref),
130 ' key is correct in %INC' );