xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/comp/require.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateBEGIN {
4*0Sstevel@tonic-gate    chdir 't' if -d 't';
5*0Sstevel@tonic-gate    @INC = '.';
6*0Sstevel@tonic-gate    push @INC, '../lib';
7*0Sstevel@tonic-gate}
8*0Sstevel@tonic-gate
9*0Sstevel@tonic-gate# don't make this lexical
10*0Sstevel@tonic-gate$i = 1;
11*0Sstevel@tonic-gate
12*0Sstevel@tonic-gatemy $Is_EBCDIC = (ord('A') == 193) ? 1 : 0;
13*0Sstevel@tonic-gatemy $Is_UTF8   = (${^OPEN} || "") =~ /:utf8/;
14*0Sstevel@tonic-gatemy $total_tests = 30;
15*0Sstevel@tonic-gateif ($Is_EBCDIC || $Is_UTF8) { $total_tests = 27; }
16*0Sstevel@tonic-gateprint "1..$total_tests\n";
17*0Sstevel@tonic-gate
18*0Sstevel@tonic-gatesub do_require {
19*0Sstevel@tonic-gate    %INC = ();
20*0Sstevel@tonic-gate    write_file('bleah.pm',@_);
21*0Sstevel@tonic-gate    eval { require "bleah.pm" };
22*0Sstevel@tonic-gate    my @a; # magic guard for scope violations (must be first lexical in file)
23*0Sstevel@tonic-gate}
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gatesub write_file {
26*0Sstevel@tonic-gate    my $f = shift;
27*0Sstevel@tonic-gate    open(REQ,">$f") or die "Can't write '$f': $!";
28*0Sstevel@tonic-gate    binmode REQ;
29*0Sstevel@tonic-gate    use bytes;
30*0Sstevel@tonic-gate    print REQ @_;
31*0Sstevel@tonic-gate    close REQ or die "Could not close $f: $!";
32*0Sstevel@tonic-gate}
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gateeval {require 5.005};
35*0Sstevel@tonic-gateprint "# $@\nnot " if $@;
36*0Sstevel@tonic-gateprint "ok ",$i++,"\n";
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gateeval { require 5.005 };
39*0Sstevel@tonic-gateprint "# $@\nnot " if $@;
40*0Sstevel@tonic-gateprint "ok ",$i++,"\n";
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gateeval { require 5.005; };
43*0Sstevel@tonic-gateprint "# $@\nnot " if $@;
44*0Sstevel@tonic-gateprint "ok ",$i++,"\n";
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gateeval {
47*0Sstevel@tonic-gate    require 5.005
48*0Sstevel@tonic-gate};
49*0Sstevel@tonic-gateprint "# $@\nnot " if $@;
50*0Sstevel@tonic-gateprint "ok ",$i++,"\n";
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gate# new style version numbers
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gateeval { require v5.5.630; };
55*0Sstevel@tonic-gateprint "# $@\nnot " if $@;
56*0Sstevel@tonic-gateprint "ok ",$i++,"\n";
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gateeval { require 10.0.2; };
59*0Sstevel@tonic-gateprint "# $@\nnot " unless $@ =~ /^Perl v10\.0\.2 required/;
60*0Sstevel@tonic-gateprint "ok ",$i++,"\n";
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gateeval q{ use v5.5.630; };
63*0Sstevel@tonic-gateprint "# $@\nnot " if $@;
64*0Sstevel@tonic-gateprint "ok ",$i++,"\n";
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gateeval q{ use 10.0.2; };
67*0Sstevel@tonic-gateprint "# $@\nnot " unless $@ =~ /^Perl v10\.0\.2 required/;
68*0Sstevel@tonic-gateprint "ok ",$i++,"\n";
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gatemy $ver = 5.005_63;
71*0Sstevel@tonic-gateeval { require $ver; };
72*0Sstevel@tonic-gateprint "# $@\nnot " if $@;
73*0Sstevel@tonic-gateprint "ok ",$i++,"\n";
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gate# check inaccurate fp
76*0Sstevel@tonic-gate$ver = 10.2;
77*0Sstevel@tonic-gateeval { require $ver; };
78*0Sstevel@tonic-gateprint "# $@\nnot " unless $@ =~ /^Perl v10\.200\.0 required/;
79*0Sstevel@tonic-gateprint "ok ",$i++,"\n";
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate$ver = 10.000_02;
82*0Sstevel@tonic-gateeval { require $ver; };
83*0Sstevel@tonic-gateprint "# $@\nnot " unless $@ =~ /^Perl v10\.0\.20 required/;
84*0Sstevel@tonic-gateprint "ok ",$i++,"\n";
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gateprint "not " unless 5.5.1 gt v5.5;
87*0Sstevel@tonic-gateprint "ok ",$i++,"\n";
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gate{
90*0Sstevel@tonic-gate    print "not " unless v5.5.640 eq "\x{5}\x{5}\x{280}";
91*0Sstevel@tonic-gate    print "ok ",$i++,"\n";
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gate    print "not " unless v7.15 eq "\x{7}\x{f}";
94*0Sstevel@tonic-gate    print "ok ",$i++,"\n";
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gate    print "not "
97*0Sstevel@tonic-gate      unless v1.20.300.4000.50000.600000 eq "\x{1}\x{14}\x{12c}\x{fa0}\x{c350}\x{927c0}";
98*0Sstevel@tonic-gate    print "ok ",$i++,"\n";
99*0Sstevel@tonic-gate}
100*0Sstevel@tonic-gate
101*0Sstevel@tonic-gate# interaction with pod (see the eof)
102*0Sstevel@tonic-gatewrite_file('bleah.pm', "print 'ok $i\n'; 1;\n");
103*0Sstevel@tonic-gaterequire "bleah.pm";
104*0Sstevel@tonic-gate$i++;
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate# run-time failure in require
107*0Sstevel@tonic-gatedo_require "0;\n";
108*0Sstevel@tonic-gateprint "# $@\nnot " unless $@ =~ /did not return a true/;
109*0Sstevel@tonic-gateprint "ok ",$i++,"\n";
110*0Sstevel@tonic-gate
111*0Sstevel@tonic-gate# compile-time failure in require
112*0Sstevel@tonic-gatedo_require "1)\n";
113*0Sstevel@tonic-gate# bison says 'parse error' instead of 'syntax error',
114*0Sstevel@tonic-gate# various yaccs may or may not capitalize 'syntax'.
115*0Sstevel@tonic-gateprint "# $@\nnot " unless $@ =~ /(syntax|parse) error/mi;
116*0Sstevel@tonic-gateprint "ok ",$i++,"\n";
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate# successful require
119*0Sstevel@tonic-gatedo_require "1";
120*0Sstevel@tonic-gateprint "# $@\nnot " if $@;
121*0Sstevel@tonic-gateprint "ok ",$i++,"\n";
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate# do FILE shouldn't see any outside lexicals
124*0Sstevel@tonic-gatemy $x = "ok $i\n";
125*0Sstevel@tonic-gatewrite_file("bleah.do", <<EOT);
126*0Sstevel@tonic-gate\$x = "not ok $i\\n";
127*0Sstevel@tonic-gateEOT
128*0Sstevel@tonic-gatedo "bleah.do";
129*0Sstevel@tonic-gatedofile();
130*0Sstevel@tonic-gatesub dofile { do "bleah.do"; };
131*0Sstevel@tonic-gateprint $x;
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gate# Test that scalar context is forced for require
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gatewrite_file('bleah.pm', <<'**BLEAH**'
136*0Sstevel@tonic-gateprint "not " if !defined wantarray || wantarray ne '';
137*0Sstevel@tonic-gateprint "ok $i - require() context\n";
138*0Sstevel@tonic-gate1;
139*0Sstevel@tonic-gate**BLEAH**
140*0Sstevel@tonic-gate);
141*0Sstevel@tonic-gate                              delete $INC{"bleah.pm"}; ++$::i;
142*0Sstevel@tonic-gate$foo = eval q{require bleah}; delete $INC{"bleah.pm"}; ++$::i;
143*0Sstevel@tonic-gate@foo = eval q{require bleah}; delete $INC{"bleah.pm"}; ++$::i;
144*0Sstevel@tonic-gate       eval q{require bleah}; delete $INC{"bleah.pm"}; ++$::i;
145*0Sstevel@tonic-gate       eval q{$_=$_+2;require bleah}; delete $INC{"bleah.pm"}; ++$::i;
146*0Sstevel@tonic-gate$foo = eval  {require bleah}; delete $INC{"bleah.pm"}; ++$::i;
147*0Sstevel@tonic-gate@foo = eval  {require bleah}; delete $INC{"bleah.pm"}; ++$::i;
148*0Sstevel@tonic-gate       eval  {require bleah};
149*0Sstevel@tonic-gate
150*0Sstevel@tonic-gate# UTF-encoded things - skipped on EBCDIC machines and on UTF-8 input
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gateif ($Is_EBCDIC || $Is_UTF8) { exit; }
153*0Sstevel@tonic-gate
154*0Sstevel@tonic-gatemy $utf8 = chr(0xFEFF);
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate$i++; do_require(qq(${utf8}print "ok $i\n"; 1;\n));
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gatesub bytes_to_utf16 {
159*0Sstevel@tonic-gate    my $utf16 = pack("$_[0]*", unpack("C*", $_[1]));
160*0Sstevel@tonic-gate    return @_ == 3 && $_[2] ? pack("$_[0]", 0xFEFF) . $utf16 : $utf16;
161*0Sstevel@tonic-gate}
162*0Sstevel@tonic-gate
163*0Sstevel@tonic-gate$i++; do_require(bytes_to_utf16('n', qq(print "ok $i\\n"; 1;\n), 1)); # BE
164*0Sstevel@tonic-gate$i++; do_require(bytes_to_utf16('v', qq(print "ok $i\\n"; 1;\n), 1)); # LE
165*0Sstevel@tonic-gate
166*0Sstevel@tonic-gateEND { 1 while unlink 'bleah.pm'; 1 while unlink 'bleah.do'; }
167*0Sstevel@tonic-gate
168*0Sstevel@tonic-gate# ***interaction with pod (don't put any thing after here)***
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate=pod
171