This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
install libgdbm and libdb in GitHub Actions
[perl5.git] / t / comp / require.t
CommitLineData
f46d017c
GS
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '.';
cc7e6304 6 push @INC, '../lib', '../ext/re';
f46d017c
GS
7}
8
9e7c6af3
NC
9sub do_require {
10 %INC = ();
11 write_file('bleah.pm',@_);
12 eval { require "bleah.pm" };
13 my @a; # magic guard for scope violations (must be first lexical in file)
14}
15
f46d017c
GS
16# don't make this lexical
17$i = 1;
d2f5bb60 18
c106f444 19my @files_to_delete = qw (bleah.pm bleah.do bleah.flg urkkk.pm urkkk.pmc
b0927e10
NC
20krunch.pm krunch.pmc whap.pm whap.pmc);
21
c106f444
DM
22# there may be another copy of this test script running, or the files may
23# just not have been deleted at the end of the last run; if the former, we
24# wait a while so that creating and unlinking these files won't interfere
25# with the other process; if the latter, then the delay is harmless. As
26# to why there might be multiple execution of this test file, I don't
27# know; but this is an experiment to see if random smoke failures go away.
28
29if (grep -e, @files_to_delete) {
30 print "# Sleeping for 20 secs waiting for other process to finish\n";
31 sleep 20;
32}
33
b0927e10 34
8a220075 35my $Is_UTF8 = (${^OPEN} || "") =~ /:utf8/;
5bad2b39 36my $total_tests = 58;
f07fa165 37if ($Is_UTF8) { $total_tests -= 3; }
d2f5bb60 38print "1..$total_tests\n";
f46d017c 39
4a9ae47a
GS
40sub write_file {
41 my $f = shift;
42 open(REQ,">$f") or die "Can't write '$f': $!";
9f82a649 43 binmode REQ;
4a9ae47a 44 print REQ @_;
d1e4d418 45 close REQ or die "Could not close $f: $!";
4a9ae47a
GS
46}
47
9f3d182e
GS
48eval {require 5.005};
49print "# $@\nnot " if $@;
b3008037 50print "ok ",$i++," - require 5.005 try 1\n";
9f3d182e
GS
51
52eval { require 5.005 };
53print "# $@\nnot " if $@;
b3008037 54print "ok ",$i++," - require 5.005 try 2\n";
9f3d182e
GS
55
56eval { require 5.005; };
57print "# $@\nnot " if $@;
b3008037 58print "ok ",$i++," - require 5.005 try 3\n";
9f3d182e
GS
59
60eval {
61 require 5.005
62};
63print "# $@\nnot " if $@;
b3008037 64print "ok ",$i++," - require 5.005 try 4\n";
9f3d182e 65
a7cb1f99
GS
66# new style version numbers
67
68eval { require v5.5.630; };
69print "# $@\nnot " if $@;
b3008037 70print "ok ",$i++," - require 5.5.630\n";
a7cb1f99 71
2b79cfef
FC
72eval { require(v5.5.630); };
73print "# $@\nnot " if $@;
74print "ok ",$i++," - require(v5.5.630) with parens [perl #124153]\n";
75
1647a8f2
FC
76sub v5 { die }
77eval { require v5; };
78print "# $@\nnot " if $@;
79print "ok ",$i++," - require v5 ignores sub named v5\n";
80
dd629d5b 81eval { require 10.0.2; };
c80a2203 82print "# $@\nnot " unless $@ =~ /^\QPerl v10.0.2 required\E/;
b3008037 83print "ok ",$i++," - require 10.0.2\n";
a7cb1f99 84
44dcb63b 85my $ver = 5.005_63;
a7cb1f99
GS
86eval { require $ver; };
87print "# $@\nnot " if $@;
b3008037 88print "ok ",$i++," - require 5.005_63\n";
a7cb1f99 89
44dcb63b 90# check inaccurate fp
dbe7b177
GS
91$ver = 10.2;
92eval { require $ver; };
c80a2203 93print "# $@\nnot " unless $@ =~ /^\QPerl v10.200.0 required\E/;
b3008037 94print "ok ",$i++," - require 10.2\n";
44dcb63b
GS
95
96$ver = 10.000_02;
a7cb1f99 97eval { require $ver; };
c80a2203 98print "# $@\nnot " unless $@ =~ /^\QPerl v10.0.20 required\E/;
b3008037 99print "ok ",$i++," - require 10.000_02\n";
a7cb1f99 100
dd629d5b 101print "not " unless 5.5.1 gt v5.5;
b3008037 102print "ok ",$i++," - 5.5.1 gt v5.5\n";
a7cb1f99 103
a7cb1f99 104{
a7cb1f99 105 print "not " unless v5.5.640 eq "\x{5}\x{5}\x{280}";
b3008037 106 print "ok ",$i++," - v5.5.640 eq \\x{5}\\x{5}\\x{280}\n";
a7cb1f99
GS
107
108 print "not " unless v7.15 eq "\x{7}\x{f}";
b3008037 109 print "ok ",$i++," - v7.15 eq \\x{7}\\x{f}\n";
a7cb1f99
GS
110
111 print "not "
112 unless v1.20.300.4000.50000.600000 eq "\x{1}\x{14}\x{12c}\x{fa0}\x{c350}\x{927c0}";
b3008037 113 print "ok ",$i++," - v1.20.300.4000.50000.600000 eq ...\n";
a7cb1f99
GS
114}
115
91937335
NC
116# "use 5.11.0" (and higher) loads strictures.
117# check that this doesn't happen with require
118eval 'require 5.11.0; ${"foo"} = "bar";';
119print "# $@\nnot " if $@;
b3008037 120print "ok ",$i++," - require 5.11.0\n";
88e9444c
NC
121eval 'BEGIN {require 5.11.0} ${"foo"} = "bar";';
122print "# $@\nnot " if $@;
b3008037 123print "ok ",$i++,"\ - BEGIN { require 5.11.0}\n";
91937335 124
4a9ae47a 125# interaction with pod (see the eof)
b3008037 126write_file('bleah.pm', "print 'ok $i - require bleah.pm\n'; 1;\n");
4a9ae47a
GS
127require "bleah.pm";
128$i++;
129
f46d017c
GS
130# run-time failure in require
131do_require "0;\n";
132print "# $@\nnot " unless $@ =~ /did not return a true/;
b3008037 133print "ok ",$i++," - require returning 0\n";
f46d017c 134
4d8b06f1 135print "not " if exists $INC{'bleah.pm'};
b3008037 136print "ok ",$i++," - %INC not updated\n";
4d8b06f1
RD
137
138my $flag_file = 'bleah.flg';
139# run-time error in require
140for my $expected_compile (1,0) {
141 write_file($flag_file, 1);
142 print "not " unless -e $flag_file;
b3008037 143 print "ok ",$i++," - exp $expected_compile; bleah.flg\n";
4d8b06f1
RD
144 write_file('bleah.pm', "unlink '$flag_file' or die; \$a=0; \$b=1/\$a; 1;\n");
145 print "# $@\nnot " if eval { require 'bleah.pm' };
b3008037 146 print "ok ",$i++," - exp $expected_compile; require bleah.pm with flag file\n";
4d8b06f1 147 print "not " unless -e $flag_file xor $expected_compile;
b3008037 148 print "ok ",$i++," - exp $expected_compile; -e flag_file\n";
27bcc0a7 149 print "not " unless exists $INC{'bleah.pm'};
b3008037 150 print "ok ",$i++," - exp $expected_compile; exists \$INC{'bleah.pm}\n";
4d8b06f1
RD
151}
152
f46d017c
GS
153# compile-time failure in require
154do_require "1)\n";
f0ec1f9a 155# bison says 'parse error' instead of 'syntax error',
d91e2bdb 156# various yaccs may or may not capitalize 'syntax'.
c80a2203 157print "# $@\nnot " unless $@ =~ /(?:syntax|parse) error/mi;
b3008037 158print "ok ",$i++," - syntax error\n";
f46d017c 159
27bcc0a7
RGS
160# previous failure cached in %INC
161print "not " unless exists $INC{'bleah.pm'};
b3008037 162print "ok ",$i++," - cached %INC\n";
4d8b06f1
RD
163write_file($flag_file, 1);
164write_file('bleah.pm', "unlink '$flag_file'; 1");
165print "# $@\nnot " if eval { require 'bleah.pm' };
b3008037 166print "ok ",$i++," - eval { require 'bleah.pm' }\n";
4d8b06f1 167print "# $@\nnot " unless $@ =~ /Compilation failed/i;
b3008037 168print "ok ",$i++," - Compilation failed\n";
4d8b06f1 169print "not " unless -e $flag_file;
b3008037 170print "ok ",$i++," - -e flag_file\n";
27bcc0a7 171print "not " unless exists $INC{'bleah.pm'};
b3008037 172print "ok ",$i++," - \$INC{'bleah.pm'}\n";
4d8b06f1 173
f46d017c
GS
174# successful require
175do_require "1";
176print "# $@\nnot " if $@;
b3008037 177print "ok ",$i++," - do_require '1';\n";
f46d017c 178
faa7e5bb 179# do FILE shouldn't see any outside lexicals
b3008037 180my $x = "ok $i - bleah.do\n";
faa7e5bb 181write_file("bleah.do", <<EOT);
b3008037 182\$x = "not ok $i - bleah.do\\n";
faa7e5bb 183EOT
e81465be 184do "bleah.do" or die $@;
faa7e5bb 185dofile();
e81465be 186sub dofile { do "bleah.do" or die $@; };
faa7e5bb 187print $x;
faa7e5bb 188
a89be09a
RGS
189# Test that scalar context is forced for require
190
191write_file('bleah.pm', <<'**BLEAH**'
192print "not " if !defined wantarray || wantarray ne '';
021f53de 193print "ok $i - require() context\n";
a89be09a
RGS
1941;
195**BLEAH**
196);
197 delete $INC{"bleah.pm"}; ++$::i;
198$foo = eval q{require bleah}; delete $INC{"bleah.pm"}; ++$::i;
199@foo = eval q{require bleah}; delete $INC{"bleah.pm"}; ++$::i;
200 eval q{require bleah}; delete $INC{"bleah.pm"}; ++$::i;
0c58d367 201 eval q{$_=$_+2;require bleah}; delete $INC{"bleah.pm"}; ++$::i;
5255f1f4 202 eval q{return require bleah}; delete $INC{"bleah.pm"}; ++$::i;
a89be09a
RGS
203$foo = eval {require bleah}; delete $INC{"bleah.pm"}; ++$::i;
204@foo = eval {require bleah}; delete $INC{"bleah.pm"}; ++$::i;
614273ad
NC
205 eval {require bleah}; delete $INC{"bleah.pm"}; ++$::i;
206
5bad2b39
DM
207eval 'require ::bleah;';
208print "# $@\nnot " unless $@ =~ /^Bareword in require must not start with a double-colon:/;
209print "ok ", $i," - require ::bleah is banned\n";
a89be09a 210
2d6f15ea
NC
211# Test for fix of RT #24404 : "require $scalar" may load a directory
212my $r = "threads";
213eval { require $r };
214$i++;
6b845e56 215if($@ =~ /Can't locate threads in \@INC/) {
b3008037 216 print "ok $i - RT #24404\n";
2d6f15ea 217} else {
b3008037 218 print "not ok - RT #24404$i\n";
2d6f15ea
NC
219}
220
01b5ef50
FC
221# require CORE::foo
222eval ' require CORE::lc "THREADS" ';
223$i++;
224if($@ =~ /Can't locate threads in \@INC/) {
225 print "ok $i - [perl #24482] require CORE::foo\n";
226} else {
227 print "not ok - [perl #24482] require CORE::foo\n";
228}
229
57f3ff92
NC
230
231write_file('bleah.pm', qq(die "This is an expected error";\n));
232delete $INC{"bleah.pm"}; ++$::i;
233eval { CORE::require bleah; };
234if ($@ =~ /^This is an expected error/) {
b3008037 235 print "ok $i - expected error\n";
57f3ff92 236} else {
b3008037 237 print "not ok $i - expected error\n";
57f3ff92
NC
238}
239
b0927e10
NC
240sub write_file_not_thing {
241 my ($file, $thing, $test) = @_;
242 write_file($file, <<"EOT");
b3008037 243 print "not ok $test - write_file_not_thing $file\n";
b0927e10
NC
244 die "The $thing file should not be loaded";
245EOT
246}
247
248{
249 # Right. We really really need Config here.
250 require Config;
251 die "Failed to load Config for some reason"
252 unless $Config::Config{version};
b0927e10
NC
253
254 my $simple = ++$i;
255 my $pmc_older = ++$i;
256 my $pmc_dies = ++$i;
ebfad2b9
DD
257 my $no_pmc;
258 foreach(Config::non_bincompat_options()) {
259 if($_ eq "PERL_DISABLE_PMC"){
260 $no_pmc = 1;
261 last;
262 }
263 }
264 if ($no_pmc) {
b0927e10
NC
265 print "# .pmc files are ignored, so test that\n";
266 write_file_not_thing('krunch.pmc', '.pmc', $pmc_older);
b3008037 267 write_file('urkkk.pm', qq(print "ok $simple - urkkk.pm branch A\n"));
b0927e10
NC
268 write_file('whap.pmc', qq(die "This is not an expected error"));
269
270 print "# Sleeping for 2 seconds before creating some more files\n";
271 sleep 2;
272
b3008037 273 write_file('krunch.pm', qq(print "ok $pmc_older - krunch.pm branch A\n"));
b0927e10
NC
274 write_file_not_thing('urkkk.pmc', '.pmc', $simple);
275 write_file('whap.pm', qq(die "This is an expected error"));
276 } else {
277 print "# .pmc files should be loaded, so test that\n";
b3008037 278 write_file('krunch.pmc', qq(print "ok $pmc_older - krunch.pm branch B\n";));
b0927e10
NC
279 write_file_not_thing('urkkk.pm', '.pm', $simple);
280 write_file('whap.pmc', qq(die "This is an expected error"));
281
282 print "# Sleeping for 2 seconds before creating some more files\n";
283 sleep 2;
284
285 write_file_not_thing('krunch.pm', '.pm', $pmc_older);
b3008037 286 write_file('urkkk.pmc', qq(print "ok $simple - urkkk.pm branch B\n";));
b0927e10
NC
287 write_file_not_thing('whap.pm', '.pm', $pmc_dies);
288 }
289 require urkkk;
290 require krunch;
291 eval {CORE::require whap; 1} and die;
292
293 if ($@ =~ /^This is an expected error/) {
b3008037 294 print "ok $pmc_dies - pmc_dies\n";
b0927e10 295 } else {
b3008037 296 print "not ok $pmc_dies - pmc_dies\n";
b0927e10
NC
297 }
298}
299
b2da7ead
DM
300
301{
302 # if we 'require "op"', since we're in the t/ directory and '.' is the
303 # first thing in @INC, it will try to load t/op/; it should fail and
304 # move onto the next path; however, the previous value of $! was
305 # leaking into implementation if it was EACCES and we're accessing a
306 # directory.
307
308 $! = eval 'use Errno qw(EACCES); EACCES' || 0;
309 eval q{require 'op'};
310 $i++;
311 print "not " if $@ =~ /Permission denied/;
312 print "ok $i - require op\n";
313}
314
eb70bb4a
FC
315# Test "require func()" with abs path when there is no .pmc file.
316++$::i;
3712c77b
TC
317if (defined &DynaLoader::boot_DynaLoader) {
318 require Cwd;
319 require File::Spec::Functions;
320 eval {
321 CORE::require(File::Spec::Functions::catfile(Cwd::getcwd(),"bleah.pm"));
322 };
323 if ($@ =~ /^This is an expected error/) {
b3008037 324 print "ok $i - require(func())\n";
3712c77b 325 } else {
b3008037 326 print "not ok $i - require(func())\n";
3712c77b 327 }
eb70bb4a 328} else {
3712c77b 329 print "ok $i # SKIP Cwd may not be available in miniperl\n";
eb70bb4a
FC
330}
331
6406c527
FC
332{
333 BEGIN { ${^OPEN} = ":utf8\0"; }
334 %INC = ();
335 write_file('bleah.pm',"package F; \$x = '\xD1\x9E';\n");
336 eval { require "bleah.pm" };
337 $i++;
338 my $not = $F::x eq "\xD1\x9E" ? "" : "not ";
339 print "${not}ok $i - require ignores I/O layers\n";
340}
341
f45b078d
FC
342{
343 BEGIN { ${^OPEN} = ":utf8\0"; }
344 %INC = ();
345 write_file('bleah.pm',"require re; re->import('/x'); 1;\n");
346 my $not = eval 'use bleah; "ab" =~ /a b/' ? "" : "not ";
347 $i++;
348 print "${not}ok $i - require does not localise %^H at run time\n";
349}
eb70bb4a 350
d20ea72b
RGS
351##########################################
352# What follows are UTF-8 specific tests. #
353# Add generic tests before this point. #
354##########################################
2d6f15ea 355
54c2f002 356# UTF-encoded things - skipped on UTF-8 input
d2f5bb60 357
54c2f002 358if ($Is_UTF8) { exit; }
d2f5bb60 359
b6357110 360my %templates = (
ee6ba15d
EB
361 'UTF-8' => 'C0U',
362 'UTF-16BE' => 'n',
363 'UTF-16LE' => 'v',
b6357110
NC
364 );
365
366sub bytes_to_utf {
367 my ($enc, $content, $do_bom) = @_;
368 my $template = $templates{$enc};
369 die "Unsupported encoding $enc" unless $template;
370 return pack "$template*", ($do_bom ? 0xFEFF : ()), unpack "C*", $content;
371}
dea0fc0b 372
b6357110 373foreach (sort keys %templates) {
91229223 374 $i++; do_require(bytes_to_utf($_, qq(print "ok $i # $_\\n"; 1;\n), 1));
ee6ba15d
EB
375 if ($@ =~ /^(Unsupported script encoding \Q$_\E)/) {
376 print "ok $i # skip $1\n";
377 }
dea0fc0b
JH
378}
379
4d8b06f1 380END {
c106f444 381 foreach my $file (@files_to_delete) {
b0927e10
NC
382 1 while unlink $file;
383 }
4d8b06f1 384}
4a9ae47a
GS
385
386# ***interaction with pod (don't put any thing after here)***
387
388=pod