This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
integrate cfgperl contents into mainline
[perl5.git] / t / op / gv.t
1 #!./perl
2
3 #
4 # various typeglob tests
5 #
6
7 BEGIN {
8     chdir 't' if -d 't';
9     unshift @INC, '../lib';
10 }   
11
12 use warnings;
13
14 print "1..30\n";
15
16 # type coersion on assignment
17 $foo = 'foo';
18 $bar = *main::foo;
19 $bar = $foo;
20 print ref(\$bar) eq 'SCALAR' ? "ok 1\n" : "not ok 1\n";
21 $foo = *main::bar;
22
23 # type coersion (not) on misc ops
24
25 if ($foo) {
26   print ref(\$foo) eq 'GLOB' ? "ok 2\n" : "not ok 2\n";
27 }
28
29 unless ($foo =~ /abcd/) {
30   print ref(\$foo) eq 'GLOB' ? "ok 3\n" : "not ok 3\n";
31 }
32
33 if ($foo eq '*main::bar') {
34   print ref(\$foo) eq 'GLOB' ? "ok 4\n" : "not ok 4\n";
35 }
36
37 # type coersion on substitutions that match
38 $a = *main::foo;
39 $b = $a;
40 $a =~ s/^X//;
41 print ref(\$a) eq 'GLOB' ? "ok 5\n" : "not ok 5\n";
42 $a =~ s/^\*//;
43 print $a eq 'main::foo' ? "ok 6\n" : "not ok 6\n";
44 print ref(\$b) eq 'GLOB' ? "ok 7\n" : "not ok 7\n";
45
46 # typeglobs as lvalues
47 substr($foo, 0, 1) = "XXX";
48 print ref(\$foo) eq 'SCALAR' ? "ok 8\n" : "not ok 8\n";
49 print $foo eq 'XXXmain::bar' ? "ok 9\n" : "not ok 9\n";
50
51 # returning glob values
52 sub foo {
53   local($bar) = *main::foo;
54   $foo = *main::bar;
55   return ($foo, $bar);
56 }
57
58 ($fuu, $baa) = foo();
59 if (defined $fuu) {
60   print ref(\$fuu) eq 'GLOB' ? "ok 10\n" : "not ok 10\n";
61 }
62
63 if (defined $baa) {
64   print ref(\$baa) eq 'GLOB' ? "ok 11\n" : "not ok 11\n";
65 }
66
67 # nested package globs
68 # NOTE:  It's probably OK if these semantics change, because the
69 #        fact that %X::Y:: is stored in %X:: isn't documented.
70 #        (I hope.)
71
72 { package Foo::Bar; no warnings 'once'; $test=1; }
73 print exists $Foo::{'Bar::'} ? "ok 12\n" : "not ok 12\n";
74 print $Foo::{'Bar::'} eq '*Foo::Bar::' ? "ok 13\n" : "not ok 13\n";
75
76 # test undef operator clearing out entire glob
77 $foo = 'stuff';
78 @foo = qw(more stuff);
79 %foo = qw(even more random stuff);
80 undef *foo;
81 print +($foo || @foo || %foo) ? "not ok" : "ok", " 14\n";
82
83 # test warnings from assignment of undef to glob
84 {
85     my $msg;
86     local $SIG{__WARN__} = sub { $msg = $_[0] };
87     use warnings;
88     *foo = 'bar';
89     print $msg ? "not ok" : "ok", " 15\n";
90     *foo = undef;
91     print $msg ? "ok" : "not ok", " 16\n";
92 }
93
94 # test *glob{THING} syntax
95 $x = "ok 17\n";
96 @x = ("ok 18\n");
97 %x = ("ok 19" => "\n");
98 sub x { "ok 20\n" }
99 print ${*x{SCALAR}}, @{*x{ARRAY}}, %{*x{HASH}}, &{*x{CODE}};
100 *x = *STDOUT;
101 print *{*x{GLOB}} eq "*main::STDOUT" ? "ok 21\n" : "not ok 21\n";
102 print {*x{IO}} "ok 22\n";
103 print {*x{FILEHANDLE}} "ok 23\n";
104
105 # test if defined() doesn't create any new symbols
106
107 {
108     my $test = 23;
109
110     my $a = "SYM000";
111     print "not " if defined *{$a};
112     ++$test; print "ok $test\n";
113
114     print "not " if defined @{$a} or defined *{$a};
115     ++$test; print "ok $test\n";
116
117     print "not " if defined %{$a} or defined *{$a};
118     ++$test; print "ok $test\n";
119
120     print "not " if defined ${$a} or defined *{$a};
121     ++$test; print "ok $test\n";
122
123     print "not " if defined &{$a} or defined *{$a};
124     ++$test; print "ok $test\n";
125
126     *{$a} = sub { print "ok $test\n" };
127     print "not " unless defined &{$a} and defined *{$a};
128     ++$test; &{$a};
129 }
130
131 # does pp_readline() handle glob-ness correctly?
132
133 {
134     my $g = *foo;
135     $g = <DATA>;
136     print $g;
137 }
138
139 __END__
140 ok 30