This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
require.t needs binmode() to work on windows
[perl5.git] / t / comp / require.t
CommitLineData
f46d017c
GS
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
93430cb4 5 unshift @INC, ('.', '../lib');
f46d017c
GS
6}
7
8# don't make this lexical
9$i = 1;
b250498f 10print "1..23\n";
f46d017c
GS
11
12sub do_require {
13 %INC = ();
4a9ae47a 14 write_file('bleah.pm',@_);
f46d017c
GS
15 eval { require "bleah.pm" };
16 my @a; # magic guard for scope violations (must be first lexical in file)
17}
18
4a9ae47a
GS
19sub write_file {
20 my $f = shift;
21 open(REQ,">$f") or die "Can't write '$f': $!";
9f82a649 22 binmode REQ;
4a9ae47a
GS
23 print REQ @_;
24 close REQ;
25}
26
9f3d182e
GS
27eval {require 5.005};
28print "# $@\nnot " if $@;
29print "ok ",$i++,"\n";
30
31eval { require 5.005 };
32print "# $@\nnot " if $@;
33print "ok ",$i++,"\n";
34
35eval { require 5.005; };
36print "# $@\nnot " if $@;
37print "ok ",$i++,"\n";
38
39eval {
40 require 5.005
41};
42print "# $@\nnot " if $@;
43print "ok ",$i++,"\n";
44
a7cb1f99
GS
45# new style version numbers
46
47eval { require v5.5.630; };
48print "# $@\nnot " if $@;
49print "ok ",$i++,"\n";
50
dd629d5b 51eval { require 10.0.2; };
a7cb1f99
GS
52print "# $@\nnot " unless $@ =~ /^Perl v10\.0\.2 required/;
53print "ok ",$i++,"\n";
54
55eval q{ use v5.5.630; };
56print "# $@\nnot " if $@;
57print "ok ",$i++,"\n";
58
dd629d5b 59eval q{ use 10.0.2; };
a7cb1f99
GS
60print "# $@\nnot " unless $@ =~ /^Perl v10\.0\.2 required/;
61print "ok ",$i++,"\n";
62
44dcb63b 63my $ver = 5.005_63;
a7cb1f99
GS
64eval { require $ver; };
65print "# $@\nnot " if $@;
66print "ok ",$i++,"\n";
67
44dcb63b 68# check inaccurate fp
dbe7b177
GS
69$ver = 10.2;
70eval { require $ver; };
71print "# $@\nnot " unless $@ =~ /^Perl v10\.200\.0 required/;
72print "ok ",$i++,"\n";
44dcb63b
GS
73
74$ver = 10.000_02;
a7cb1f99 75eval { require $ver; };
44dcb63b 76print "# $@\nnot " unless $@ =~ /^Perl v10\.0\.20 required/;
a7cb1f99
GS
77print "ok ",$i++,"\n";
78
dd629d5b 79print "not " unless 5.5.1 gt v5.5;
a7cb1f99
GS
80print "ok ",$i++,"\n";
81
a7cb1f99
GS
82{
83 use utf8;
84 print "not " unless v5.5.640 eq "\x{5}\x{5}\x{280}";
85 print "ok ",$i++,"\n";
86
87 print "not " unless v7.15 eq "\x{7}\x{f}";
88 print "ok ",$i++,"\n";
89
90 print "not "
91 unless v1.20.300.4000.50000.600000 eq "\x{1}\x{14}\x{12c}\x{fa0}\x{c350}\x{927c0}";
92 print "ok ",$i++,"\n";
93}
94
4a9ae47a
GS
95# interaction with pod (see the eof)
96write_file('bleah.pm', "print 'ok $i\n'; 1;\n");
97require "bleah.pm";
98$i++;
99
f46d017c
GS
100# run-time failure in require
101do_require "0;\n";
102print "# $@\nnot " unless $@ =~ /did not return a true/;
103print "ok ",$i++,"\n";
104
105# compile-time failure in require
106do_require "1)\n";
f0ec1f9a 107# bison says 'parse error' instead of 'syntax error',
d91e2bdb 108# various yaccs may or may not capitalize 'syntax'.
f0ec1f9a 109print "# $@\nnot " unless $@ =~ /(syntax|parse) error/mi;
f46d017c
GS
110print "ok ",$i++,"\n";
111
112# successful require
113do_require "1";
114print "# $@\nnot " if $@;
115print "ok ",$i++,"\n";
116
faa7e5bb
GS
117# do FILE shouldn't see any outside lexicals
118my $x = "ok $i\n";
119write_file("bleah.do", <<EOT);
120\$x = "not ok $i\\n";
121EOT
122do "bleah.do";
123dofile();
124sub dofile { do "bleah.do"; };
125print $x;
faa7e5bb 126
b250498f
GS
127# UTF-encoded things
128my $utf8 = chr(0xFEFF);
dea0fc0b
JH
129
130$i++; do_require(qq(${utf8}print "ok $i\n"; 1;\n));
131
132sub bytes_to_utf16 {
133 my $utf16 = pack("$_[0]*", unpack("C*", $_[1]));
134 return @_ == 3 && $_[2] ? pack("$_[0]", 0xFEFF) . $utf16 : $utf16;
135}
136
137$i++; do_require(bytes_to_utf16('n', qq(print "ok $i\\n"; 1;\n), 1)); # BE
138$i++; do_require(bytes_to_utf16('v', qq(print "ok $i\\n"; 1;\n), 1)); # LE
b250498f 139
faa7e5bb 140END { 1 while unlink 'bleah.pm'; 1 while unlink 'bleah.do'; }
4a9ae47a
GS
141
142# ***interaction with pod (don't put any thing after here)***
143
144=pod