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;";
23 close $fh or die "Couldn't close: $!";
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 my $evalret = eval { require Bar; 1 };
43 ok( !$evalret, 'Trying non-magic package' );
45 $evalret = eval { require Foo; 1 };
47 ok( $evalret, 'require Foo; magic via code ref' );
48 ok( exists $INC{'Foo.pm'}, ' %INC sees Foo.pm' );
49 is( ref $INC{'Foo.pm'}, 'CODE', ' val Foo.pm is a coderef in %INC' );
50 is( $INC{'Foo.pm'}, \&fooinc, ' val Foo.pm is correct in %INC' );
52 $evalret = eval "use Foo1; 1;";
54 ok( $evalret, 'use Foo1' );
55 ok( exists $INC{'Foo1.pm'}, ' %INC sees Foo1.pm' );
56 is( ref $INC{'Foo1.pm'}, 'CODE', ' val Foo1.pm is a coderef in %INC' );
57 is( $INC{'Foo1.pm'}, \&fooinc, ' val Foo1.pm is correct in %INC' );
59 $evalret = eval { do 'Foo2.pl'; 1 };
61 ok( $evalret, 'do "Foo2.pl"' );
62 ok( exists $INC{'Foo2.pl'}, ' %INC sees Foo2.pl' );
63 is( ref $INC{'Foo2.pl'}, 'CODE', ' val Foo2.pl is a coderef in %INC' );
64 is( $INC{'Foo2.pl'}, \&fooinc, ' val Foo2.pl is correct in %INC' );
70 my ($self, $filename) = @_;
71 if (substr($filename, 0, length($self->[1])) eq $self->[1]) {
72 return get_temp_fh($filename);
79 my $arrayref = [ \&fooinc2, 'Bar' ];
82 $evalret = eval { require Foo; 1; };
84 ok( $evalret, 'Originally loaded packages preserved' );
85 $evalret = eval { require Foo3; 1; };
86 ok( !$evalret, 'Original magic INC purged' );
88 $evalret = eval { require Bar; 1 };
90 ok( $evalret, 'require Bar; magic via array ref' );
91 ok( exists $INC{'Bar.pm'}, ' %INC sees Bar.pm' );
92 is( ref $INC{'Bar.pm'}, 'ARRAY', ' val Bar.pm is an arrayref in %INC' );
93 is( $INC{'Bar.pm'}, $arrayref, ' val Bar.pm is correct in %INC' );
95 ok( eval "use Bar1; 1;", 'use Bar1' );
96 ok( exists $INC{'Bar1.pm'}, ' %INC sees Bar1.pm' );
97 is( ref $INC{'Bar1.pm'}, 'ARRAY', ' val Bar1.pm is an arrayref in %INC' );
98 is( $INC{'Bar1.pm'}, $arrayref, ' val Bar1.pm is correct in %INC' );
100 ok( eval { do 'Bar2.pl'; 1 }, 'do "Bar2.pl"' );
101 ok( exists $INC{'Bar2.pl'}, ' %INC sees Bar2.pl' );
102 is( ref $INC{'Bar2.pl'}, 'ARRAY', ' val Bar2.pl is an arrayref in %INC' );
103 is( $INC{'Bar2.pl'}, $arrayref, ' val Bar2.pl is correct in %INC' );
108 my ($self, $filename) = @_;
109 if (substr($filename,0,4) eq 'Quux') {
110 return get_temp_fh($filename);
117 my $href = bless( {}, 'FooLoader' );
120 $evalret = eval { require Quux; 1 };
122 ok( $evalret, 'require Quux; magic via hash object' );
123 ok( exists $INC{'Quux.pm'}, ' %INC sees Quux.pm' );
124 is( ref $INC{'Quux.pm'}, 'FooLoader',
125 ' val Quux.pm is an object in %INC' );
126 is( $INC{'Quux.pm'}, $href, ' val Quux.pm is correct in %INC' );
130 my $aref = bless( [], 'FooLoader' );
133 $evalret = eval { require Quux1; 1 };
135 ok( $evalret, 'require Quux1; magic via array object' );
136 ok( exists $INC{'Quux1.pm'}, ' %INC sees Quux1.pm' );
137 is( ref $INC{'Quux1.pm'}, 'FooLoader',
138 ' val Quux1.pm is an object in %INC' );
139 is( $INC{'Quux1.pm'}, $aref, ' val Quux1.pm is correct in %INC' );
143 my $sref = bless( \(my $x = 1), 'FooLoader' );
146 $evalret = eval { require Quux2; 1 };
148 ok( $evalret, 'require Quux2; magic via scalar object' );
149 ok( exists $INC{'Quux2.pm'}, ' %INC sees Quux2.pm' );
150 is( ref $INC{'Quux2.pm'}, 'FooLoader',
151 ' val Quux2.pm is an object in %INC' );
152 is( $INC{'Quux2.pm'}, $sref, ' val Quux2.pm is correct in %INC' );
157 my ($self, $filename) = @_;
158 if (substr($filename,0,4) eq 'Toto') {
159 $INC{$filename} = 'xyz';
160 return get_temp_fh($filename);
167 $evalret = eval { require Toto; 1 };
169 ok( $evalret, 'require Toto; magic via anonymous code ref' );
170 ok( exists $INC{'Toto.pm'}, ' %INC sees Toto.pm' );
171 ok( ! ref $INC{'Toto.pm'}, q/ val Toto.pm isn't a ref in %INC/ );
172 is( $INC{'Toto.pm'}, 'xyz', ' val Toto.pm is correct in %INC' );
176 my $filename = $^O eq 'MacOS' ? ':Foo:Foo.pm' : './Foo.pm';
179 @INC = sub { $filename = 'seen'; return undef; };
180 eval { require $filename; };
181 is( $filename, 'seen', 'the coderef sees fully-qualified pathnames' );