3 # Tests for the coderef-in-@INC feature
8 my $minitest = $ENV{PERL_CORE_MINITEST};
9 my $has_perlio = $Config{useperlio};
17 if ($Config{d_fork} && eval 'require POSIX; 1') {
26 plan(tests => 49 + !$minitest * (3 + 14 * $can_fork));
30 open my $fh, ">$f" or die "Can't create $f: $!";
31 print $fh "package ".substr($_[0],0,-3).";\n1;\n";
32 print $fh $_[1] if @_ > 1;
33 close $fh or die "Couldn't close: $!";
34 open $fh, $f or die "Can't open $f: $!";
39 my ($self, $filename) = @_;
40 if (substr($filename,0,3) eq 'Foo') {
41 return get_temp_fh($filename);
50 my $evalret = eval { require Bar; 1 };
51 ok( !$evalret, 'Trying non-magic package' );
53 $evalret = eval { require Foo; 1 };
55 ok( $evalret, 'require Foo; magic via code ref' );
56 ok( exists $INC{'Foo.pm'}, ' %INC sees Foo.pm' );
57 is( ref $INC{'Foo.pm'}, 'CODE', ' val Foo.pm is a coderef in %INC' );
58 is( $INC{'Foo.pm'}, \&fooinc, ' val Foo.pm is correct in %INC' );
60 $evalret = eval "use Foo1; 1;";
62 ok( $evalret, 'use Foo1' );
63 ok( exists $INC{'Foo1.pm'}, ' %INC sees Foo1.pm' );
64 is( ref $INC{'Foo1.pm'}, 'CODE', ' val Foo1.pm is a coderef in %INC' );
65 is( $INC{'Foo1.pm'}, \&fooinc, ' val Foo1.pm is correct in %INC' );
67 $evalret = eval { do 'Foo2.pl'; 1 };
69 ok( $evalret, 'do "Foo2.pl"' );
70 ok( exists $INC{'Foo2.pl'}, ' %INC sees Foo2.pl' );
71 is( ref $INC{'Foo2.pl'}, 'CODE', ' val Foo2.pl is a coderef in %INC' );
72 is( $INC{'Foo2.pl'}, \&fooinc, ' val Foo2.pl is correct in %INC' );
78 my ($self, $filename) = @_;
79 if (substr($filename, 0, length($self->[1])) eq $self->[1]) {
80 return get_temp_fh($filename);
87 my $arrayref = [ \&fooinc2, 'Bar' ];
90 $evalret = eval { require Foo; 1; };
92 ok( $evalret, 'Originally loaded packages preserved' );
93 $evalret = eval { require Foo3; 1; };
94 ok( !$evalret, 'Original magic INC purged' );
96 $evalret = eval { require Bar; 1 };
98 ok( $evalret, 'require Bar; magic via array ref' );
99 ok( exists $INC{'Bar.pm'}, ' %INC sees Bar.pm' );
100 is( ref $INC{'Bar.pm'}, 'ARRAY', ' val Bar.pm is an arrayref in %INC' );
101 is( $INC{'Bar.pm'}, $arrayref, ' val Bar.pm is correct in %INC' );
103 ok( eval "use Bar1; 1;", 'use Bar1' );
104 ok( exists $INC{'Bar1.pm'}, ' %INC sees Bar1.pm' );
105 is( ref $INC{'Bar1.pm'}, 'ARRAY', ' val Bar1.pm is an arrayref in %INC' );
106 is( $INC{'Bar1.pm'}, $arrayref, ' val Bar1.pm is correct in %INC' );
108 ok( eval { do 'Bar2.pl'; 1 }, 'do "Bar2.pl"' );
109 ok( exists $INC{'Bar2.pl'}, ' %INC sees Bar2.pl' );
110 is( ref $INC{'Bar2.pl'}, 'ARRAY', ' val Bar2.pl is an arrayref in %INC' );
111 is( $INC{'Bar2.pl'}, $arrayref, ' val Bar2.pl is correct in %INC' );
116 my ($self, $filename) = @_;
117 if (substr($filename,0,4) eq 'Quux') {
118 return get_temp_fh($filename);
125 my $href = bless( {}, 'FooLoader' );
128 $evalret = eval { require Quux; 1 };
130 ok( $evalret, 'require Quux; magic via hash object' );
131 ok( exists $INC{'Quux.pm'}, ' %INC sees Quux.pm' );
132 is( ref $INC{'Quux.pm'}, 'FooLoader',
133 ' val Quux.pm is an object in %INC' );
134 is( $INC{'Quux.pm'}, $href, ' val Quux.pm is correct in %INC' );
138 my $aref = bless( [], 'FooLoader' );
141 $evalret = eval { require Quux1; 1 };
143 ok( $evalret, 'require Quux1; magic via array object' );
144 ok( exists $INC{'Quux1.pm'}, ' %INC sees Quux1.pm' );
145 is( ref $INC{'Quux1.pm'}, 'FooLoader',
146 ' val Quux1.pm is an object in %INC' );
147 is( $INC{'Quux1.pm'}, $aref, ' val Quux1.pm is correct in %INC' );
151 my $sref = bless( \(my $x = 1), 'FooLoader' );
154 $evalret = eval { require Quux2; 1 };
156 ok( $evalret, 'require Quux2; magic via scalar object' );
157 ok( exists $INC{'Quux2.pm'}, ' %INC sees Quux2.pm' );
158 is( ref $INC{'Quux2.pm'}, 'FooLoader',
159 ' val Quux2.pm is an object in %INC' );
160 is( $INC{'Quux2.pm'}, $sref, ' val Quux2.pm is correct in %INC' );
165 my ($self, $filename) = @_;
166 if (substr($filename,0,4) eq 'Toto') {
167 $INC{$filename} = 'xyz';
168 return get_temp_fh($filename);
175 $evalret = eval { require Toto; 1 };
177 ok( $evalret, 'require Toto; magic via anonymous code ref' );
178 ok( exists $INC{'Toto.pm'}, ' %INC sees Toto.pm' );
179 ok( ! ref $INC{'Toto.pm'}, q/ val Toto.pm isn't a ref in %INC/ );
180 is( $INC{'Toto.pm'}, 'xyz', ' val Toto.pm is correct in %INC' );
185 my ($self, $filename) = @_;
186 if ($filename eq 'abc.pl') {
187 return get_temp_fh($filename, qq(return "abc";\n));
195 $ret ||= do 'abc.pl';
196 is( $ret, 'abc', 'do "abc.pl" sees return value' );
199 my $filename = './Foo.pm';
200 #local @INC; # local fails on tied @INC
201 my @old_INC = @INC; # because local doesn't work on tied arrays
202 @INC = sub { $filename = 'seen'; return undef; };
203 eval { require $filename; };
204 is( $filename, 'seen', 'the coderef sees fully-qualified pathnames' );
208 # this will segfault if it fails
210 sub PVBM () { 'foo' }
211 { my $dummy = index 'foo', PVBM }
213 # I don't know whether these requires should succeed or fail. 5.8 failed
214 # all of them; 5.10 with an ordinary constant in place of PVBM lets the
215 # latter two succeed. For now I don't care, as long as they don't
218 unshift @INC, sub { PVBM };
220 ok( 1, 'returning PVBM doesn\'t segfault require' );
222 ok( 1, 'returning PVBM doesn\'t segfault use' );
224 unshift @INC, sub { \PVBM };
226 ok( 1, 'returning PVBM ref doesn\'t segfault require' );
228 ok( 1, 'returning PVBM ref doesn\'t segfault use' );
234 skip( "No PerlIO available", 3 ) unless $has_perlio;
238 my ($cr, $filename) = @_;
239 my $module = $filename; $module =~ s,/,::,g; $module =~ s/\.pm$//;
241 \"package $module; sub complain { warn q() }; \$::file = __FILE__;"
243 $INC{$filename} = "/custom/path/to/$filename";
247 require Publius::Vergilius::Maro;
248 is( $INC{'Publius/Vergilius/Maro.pm'},
249 '/custom/path/to/Publius/Vergilius/Maro.pm', '%INC set correctly');
250 is( our $file, '/custom/path/to/Publius/Vergilius/Maro.pm',
251 '__FILE__ set correctly' );
254 local $SIG{__WARN__} = sub { $warning = shift };
255 Publius::Vergilius::Maro::complain();
256 like( $warning, qr{something's wrong at /custom/path/to/Publius/Vergilius/Maro.pm}, 'warn() reports correct file source' );
262 require PerlIO::scalar;
263 # This little bundle of joy generates n more recursive use statements,
264 # with each module chaining the next one down to 0. If it works, then we
265 # can safely nest subprocesses
268 return unless $_[1] =~ /^BBBLPLAST(\d+)\.pm/;
269 my $pid = open my $fh, "-|";
272 return $fh unless $use_filter_too;
273 # Try filters and state in addition.
274 return ($fh, sub {s/$_[1]/pass/; return}, "die")
276 die "Can't fork self: $!" unless defined $pid;
280 # Lets force some fun with odd sized reads.
282 print 'push @main::bbblplast, ';
285 print "use BBBLPLAST$count;\n";
287 if ($use_filter_too) {
288 print "die('In $_[1]');";
290 print "pass('In $_[1]');";
294 die "Can't get here: $!";
299 is ("@::bbblplast", "0 1 2 3 4 5", "All ran");
301 foreach (keys %INC) {
302 delete $INC{$_} if /^BBBLPLAST/;
310 is ("@::bbblplast", "0 1 2 3 4 5", "All ran with a filter");