3 # Tests for the coderef-in-@INC feature
18 my $f = "DummyModule0000";
21 open my $fh, ">$f" or die "Can't create $f: $!";
22 print $fh "package ".substr($_[0],0,-3)."; 1;";
24 open $fh, $f or die "Can't open $f: $!";
28 END { 1 while unlink @tempfiles }
31 my ($self, $filename) = @_;
32 if (substr($filename,0,3) eq 'Foo') {
33 return get_temp_fh($filename);
42 ok( !eval { require Bar; 1 }, 'Trying non-magic package' );
44 ok( eval { require Foo; 1 }, 'require() magic via code ref' );
45 ok( exists $INC{'Foo.pm'}, ' %INC sees it' );
46 is( ref $INC{'Foo.pm'}, 'CODE', ' key is a coderef in %INC' );
47 is( $INC{'Foo.pm'}, \&fooinc, ' key is correct in %INC' );
49 ok( eval "use Foo1; 1;", 'use()' );
50 ok( exists $INC{'Foo1.pm'}, ' %INC sees it' );
51 is( ref $INC{'Foo1.pm'}, 'CODE', ' key is a coderef in %INC' );
52 is( $INC{'Foo1.pm'}, \&fooinc, ' key is correct in %INC' );
54 ok( eval { do 'Foo2.pl'; 1 }, 'do()' );
55 ok( exists $INC{'Foo2.pl'}, ' %INC sees it' );
56 is( ref $INC{'Foo2.pl'}, 'CODE', ' key is a coderef in %INC' );
57 is( $INC{'Foo2.pl'}, \&fooinc, ' key is correct in %INC' );
63 my ($self, $filename) = @_;
64 if (substr($filename, 0, length($self->[1])) eq $self->[1]) {
65 return get_temp_fh($filename);
72 my $arrayref = [ \&fooinc2, 'Bar' ];
75 ok( eval { require Foo; 1; }, 'Originally loaded packages preserved' );
76 ok( !eval { require Foo3; 1; }, 'Original magic INC purged' );
78 ok( eval { require Bar; 1 }, 'require() magic via array ref' );
79 ok( exists $INC{'Bar.pm'}, ' %INC sees it' );
80 is( ref $INC{'Bar.pm'}, 'ARRAY', ' key is an arrayref in %INC' );
81 is( $INC{'Bar.pm'}, $arrayref, ' key is correct in %INC' );
83 ok( eval "use Bar1; 1;", 'use()' );
84 ok( exists $INC{'Bar1.pm'}, ' %INC sees it' );
85 is( ref $INC{'Bar1.pm'}, 'ARRAY', ' key is an arrayref in %INC' );
86 is( $INC{'Bar1.pm'}, $arrayref, ' key is correct in %INC' );
88 ok( eval { do 'Bar2.pl'; 1 }, 'do()' );
89 ok( exists $INC{'Bar2.pl'}, ' %INC sees it' );
90 is( ref $INC{'Bar2.pl'}, 'ARRAY', ' key is an arrayref in %INC' );
91 is( $INC{'Bar2.pl'}, $arrayref, ' key is correct in %INC' );
96 my ($self, $filename) = @_;
97 if (substr($filename,0,4) eq 'Quux') {
98 return get_temp_fh($filename);
105 my $href = bless( {}, 'FooLoader' );
108 ok( eval { require Quux; 1 }, 'require() magic via hash object' );
109 ok( exists $INC{'Quux.pm'}, ' %INC sees it' );
110 is( ref $INC{'Quux.pm'}, 'FooLoader',
111 ' key is an object in %INC' );
112 is( $INC{'Quux.pm'}, $href, ' key is correct in %INC' );
116 my $aref = bless( [], 'FooLoader' );
119 ok( eval { require Quux1; 1 }, 'require() magic via array object' );
120 ok( exists $INC{'Quux1.pm'}, ' %INC sees it' );
121 is( ref $INC{'Quux1.pm'}, 'FooLoader',
122 ' key is an object in %INC' );
123 is( $INC{'Quux1.pm'}, $aref, ' key is correct in %INC' );
127 my $sref = bless( \(my $x = 1), 'FooLoader' );
130 ok( eval { require Quux2; 1 }, 'require() magic via scalar object' );
131 ok( exists $INC{'Quux2.pm'}, ' %INC sees it' );
132 is( ref $INC{'Quux2.pm'}, 'FooLoader',
133 ' key is an object in %INC' );
134 is( $INC{'Quux2.pm'}, $sref, ' key is correct in %INC' );
139 my ($self, $filename) = @_;
140 if (substr($filename,0,4) eq 'Toto') {
141 $INC{$filename} = 'xyz';
142 return get_temp_fh($filename);
149 ok( eval { require Toto; 1 }, 'require() magic via anonymous code ref' );
150 ok( exists $INC{'Toto.pm'}, ' %INC sees it' );
151 ok( ! ref $INC{'Toto.pm'}, q/ key isn't a ref in %INC/ );
152 is( $INC{'Toto.pm'}, 'xyz', ' key is correct in %INC' );