This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #33906] Missing \\ in perlre
[perl5.git] / t / comp / require.t
CommitLineData
f46d017c
GS
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61
MG
5 @INC = '.';
6 push @INC, '../lib';
f46d017c
GS
7}
8
9# don't make this lexical
10$i = 1;
d2f5bb60
PP
11
12my $Is_EBCDIC = (ord('A') == 193) ? 1 : 0;
8a220075 13my $Is_UTF8 = (${^OPEN} || "") =~ /:utf8/;
10ac9278
RD
14my $total_tests = 43;
15my $ebcdic_utf8_skips = 3;
16if ($Is_EBCDIC || $Is_UTF8) { $total_tests -= $ebcdic_utf8_skips; }
d2f5bb60 17print "1..$total_tests\n";
f46d017c
GS
18
19sub do_require {
20 %INC = ();
4a9ae47a 21 write_file('bleah.pm',@_);
f46d017c
GS
22 eval { require "bleah.pm" };
23 my @a; # magic guard for scope violations (must be first lexical in file)
24}
25
4a9ae47a
GS
26sub write_file {
27 my $f = shift;
28 open(REQ,">$f") or die "Can't write '$f': $!";
9f82a649 29 binmode REQ;
7d59b7e4 30 use bytes;
4a9ae47a 31 print REQ @_;
d1e4d418 32 close REQ or die "Could not close $f: $!";
4a9ae47a
GS
33}
34
9f3d182e
GS
35eval {require 5.005};
36print "# $@\nnot " if $@;
37print "ok ",$i++,"\n";
38
39eval { require 5.005 };
40print "# $@\nnot " if $@;
41print "ok ",$i++,"\n";
42
43eval { require 5.005; };
44print "# $@\nnot " if $@;
45print "ok ",$i++,"\n";
46
47eval {
48 require 5.005
49};
50print "# $@\nnot " if $@;
51print "ok ",$i++,"\n";
52
a7cb1f99
GS
53# new style version numbers
54
55eval { require v5.5.630; };
56print "# $@\nnot " if $@;
57print "ok ",$i++,"\n";
58
dd629d5b 59eval { require 10.0.2; };
a7cb1f99
GS
60print "# $@\nnot " unless $@ =~ /^Perl v10\.0\.2 required/;
61print "ok ",$i++,"\n";
62
63eval q{ use v5.5.630; };
64print "# $@\nnot " if $@;
65print "ok ",$i++,"\n";
66
dd629d5b 67eval q{ use 10.0.2; };
a7cb1f99
GS
68print "# $@\nnot " unless $@ =~ /^Perl v10\.0\.2 required/;
69print "ok ",$i++,"\n";
70
44dcb63b 71my $ver = 5.005_63;
a7cb1f99
GS
72eval { require $ver; };
73print "# $@\nnot " if $@;
74print "ok ",$i++,"\n";
75
44dcb63b 76# check inaccurate fp
dbe7b177
GS
77$ver = 10.2;
78eval { require $ver; };
d7aa5382 79print "# $@\nnot " unless $@ =~ /^Perl v10\.200 required/;
dbe7b177 80print "ok ",$i++,"\n";
44dcb63b
GS
81
82$ver = 10.000_02;
a7cb1f99 83eval { require $ver; };
44dcb63b 84print "# $@\nnot " unless $@ =~ /^Perl v10\.0\.20 required/;
a7cb1f99
GS
85print "ok ",$i++,"\n";
86
dd629d5b 87print "not " unless 5.5.1 gt v5.5;
a7cb1f99
GS
88print "ok ",$i++,"\n";
89
a7cb1f99 90{
a7cb1f99
GS
91 print "not " unless v5.5.640 eq "\x{5}\x{5}\x{280}";
92 print "ok ",$i++,"\n";
93
94 print "not " unless v7.15 eq "\x{7}\x{f}";
95 print "ok ",$i++,"\n";
96
97 print "not "
98 unless v1.20.300.4000.50000.600000 eq "\x{1}\x{14}\x{12c}\x{fa0}\x{c350}\x{927c0}";
99 print "ok ",$i++,"\n";
100}
101
4a9ae47a
GS
102# interaction with pod (see the eof)
103write_file('bleah.pm', "print 'ok $i\n'; 1;\n");
104require "bleah.pm";
105$i++;
106
f46d017c
GS
107# run-time failure in require
108do_require "0;\n";
109print "# $@\nnot " unless $@ =~ /did not return a true/;
110print "ok ",$i++,"\n";
111
4d8b06f1
RD
112print "not " if exists $INC{'bleah.pm'};
113print "ok ",$i++,"\n";
114
115my $flag_file = 'bleah.flg';
116# run-time error in require
117for my $expected_compile (1,0) {
118 write_file($flag_file, 1);
119 print "not " unless -e $flag_file;
120 print "ok ",$i++,"\n";
121 write_file('bleah.pm', "unlink '$flag_file' or die; \$a=0; \$b=1/\$a; 1;\n");
122 print "# $@\nnot " if eval { require 'bleah.pm' };
123 print "ok ",$i++,"\n";
124 print "not " unless -e $flag_file xor $expected_compile;
125 print "ok ",$i++,"\n";
4d8b06f1
RD
126}
127
f46d017c
GS
128# compile-time failure in require
129do_require "1)\n";
f0ec1f9a 130# bison says 'parse error' instead of 'syntax error',
d91e2bdb 131# various yaccs may or may not capitalize 'syntax'.
f0ec1f9a 132print "# $@\nnot " unless $@ =~ /(syntax|parse) error/mi;
f46d017c
GS
133print "ok ",$i++,"\n";
134
4d8b06f1
RD
135write_file($flag_file, 1);
136write_file('bleah.pm', "unlink '$flag_file'; 1");
137print "# $@\nnot " if eval { require 'bleah.pm' };
138print "ok ",$i++,"\n";
139print "# $@\nnot " unless $@ =~ /Compilation failed/i;
140print "ok ",$i++,"\n";
141print "not " unless -e $flag_file;
142print "ok ",$i++,"\n";
10ac9278
RD
143# [perl #31924]
144eval { $INC{'bleah.pm'} = 'bleah.pm' };
145print "# $@\nnot " if $@;
146print "ok ",$i++,"\n";
147print "not " unless $INC{'bleah.pm'} eq 'bleah.pm';
4d8b06f1
RD
148print "ok ",$i++,"\n";
149
f46d017c
GS
150# successful require
151do_require "1";
152print "# $@\nnot " if $@;
153print "ok ",$i++,"\n";
10ac9278
RD
154print "not " unless $INC{'bleah.pm'} eq 'bleah.pm';
155print "ok ",$i++,"\n";
f46d017c 156
faa7e5bb
GS
157# do FILE shouldn't see any outside lexicals
158my $x = "ok $i\n";
159write_file("bleah.do", <<EOT);
160\$x = "not ok $i\\n";
161EOT
162do "bleah.do";
163dofile();
164sub dofile { do "bleah.do"; };
165print $x;
faa7e5bb 166
a89be09a
RGS
167# Test that scalar context is forced for require
168
169write_file('bleah.pm', <<'**BLEAH**'
170print "not " if !defined wantarray || wantarray ne '';
0c58d367 171print "ok $i - require() context\n";
a89be09a
RGS
1721;
173**BLEAH**
174);
175 delete $INC{"bleah.pm"}; ++$::i;
176$foo = eval q{require bleah}; delete $INC{"bleah.pm"}; ++$::i;
177@foo = eval q{require bleah}; delete $INC{"bleah.pm"}; ++$::i;
178 eval q{require bleah}; delete $INC{"bleah.pm"}; ++$::i;
0c58d367 179 eval q{$_=$_+2;require bleah}; delete $INC{"bleah.pm"}; ++$::i;
a89be09a
RGS
180$foo = eval {require bleah}; delete $INC{"bleah.pm"}; ++$::i;
181@foo = eval {require bleah}; delete $INC{"bleah.pm"}; ++$::i;
182 eval {require bleah};
183
8a220075 184# UTF-encoded things - skipped on EBCDIC machines and on UTF-8 input
d2f5bb60 185
8a220075 186if ($Is_EBCDIC || $Is_UTF8) { exit; }
d2f5bb60 187
b250498f 188my $utf8 = chr(0xFEFF);
dea0fc0b
JH
189
190$i++; do_require(qq(${utf8}print "ok $i\n"; 1;\n));
191
192sub bytes_to_utf16 {
193 my $utf16 = pack("$_[0]*", unpack("C*", $_[1]));
7d59b7e4 194 return @_ == 3 && $_[2] ? pack("$_[0]", 0xFEFF) . $utf16 : $utf16;
dea0fc0b
JH
195}
196
197$i++; do_require(bytes_to_utf16('n', qq(print "ok $i\\n"; 1;\n), 1)); # BE
198$i++; do_require(bytes_to_utf16('v', qq(print "ok $i\\n"; 1;\n), 1)); # LE
b250498f 199
4d8b06f1
RD
200END {
201 1 while unlink 'bleah.pm';
202 1 while unlink 'bleah.do';
203 1 while unlink 'bleah.flg';
204}
4a9ae47a
GS
205
206# ***interaction with pod (don't put any thing after here)***
207
208=pod