xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/op/localref.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gatechdir 't' if -d 't';
4*0Sstevel@tonic-gate@INC = qw(. ../lib);
5*0Sstevel@tonic-gaterequire "test.pl";
6*0Sstevel@tonic-gateplan( tests => 64 );
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gate$aa = 1;
9*0Sstevel@tonic-gate{ local $aa;     $aa = 2; is($aa,2); }
10*0Sstevel@tonic-gateis($aa,1);
11*0Sstevel@tonic-gate{ local ${aa};   $aa = 3; is($aa,3); }
12*0Sstevel@tonic-gateis($aa,1);
13*0Sstevel@tonic-gate{ local ${"aa"}; $aa = 4; is($aa,4); }
14*0Sstevel@tonic-gateis($aa,1);
15*0Sstevel@tonic-gate$x = "aa";
16*0Sstevel@tonic-gate{ local ${$x};   $aa = 5; is($aa,5); undef $x; is($aa,5); }
17*0Sstevel@tonic-gateis($aa,1);
18*0Sstevel@tonic-gate$x = "a";
19*0Sstevel@tonic-gate{ local ${$x x2};$aa = 6; is($aa,6); undef $x; is($aa,6); }
20*0Sstevel@tonic-gateis($aa,1);
21*0Sstevel@tonic-gate$x = "aa";
22*0Sstevel@tonic-gate{ local $$x;     $aa = 7; is($aa,7); undef $x; is($aa,7); }
23*0Sstevel@tonic-gateis($aa,1);
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gate@aa = qw/a b/;
26*0Sstevel@tonic-gate{ local @aa;     @aa = qw/c d/; is("@aa","c d"); }
27*0Sstevel@tonic-gateis("@aa","a b");
28*0Sstevel@tonic-gate{ local @{aa};   @aa = qw/e f/; is("@aa","e f"); }
29*0Sstevel@tonic-gateis("@aa","a b");
30*0Sstevel@tonic-gate{ local @{"aa"}; @aa = qw/g h/; is("@aa","g h"); }
31*0Sstevel@tonic-gateis("@aa","a b");
32*0Sstevel@tonic-gate$x = "aa";
33*0Sstevel@tonic-gate{ local @{$x};   @aa = qw/i j/; is("@aa","i j"); undef $x; is("@aa","i j"); }
34*0Sstevel@tonic-gateis("@aa","a b");
35*0Sstevel@tonic-gate$x = "a";
36*0Sstevel@tonic-gate{ local @{$x x2};@aa = qw/k l/; is("@aa","k l"); undef $x; is("@aa","k l"); }
37*0Sstevel@tonic-gateis("@aa","a b");
38*0Sstevel@tonic-gate$x = "aa";
39*0Sstevel@tonic-gate{ local @$x;     @aa = qw/m n/; is("@aa","m n"); undef $x; is("@aa","m n"); }
40*0Sstevel@tonic-gateis("@aa","a b");
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate%aa = qw/a b/;
43*0Sstevel@tonic-gate{ local %aa;     %aa = qw/c d/; is($aa{c},"d"); }
44*0Sstevel@tonic-gateis($aa{a},"b");
45*0Sstevel@tonic-gate{ local %{aa};   %aa = qw/e f/; is($aa{e},"f"); }
46*0Sstevel@tonic-gateis($aa{a},"b");
47*0Sstevel@tonic-gate{ local %{"aa"}; %aa = qw/g h/; is($aa{g},"h"); }
48*0Sstevel@tonic-gateis($aa{a},"b");
49*0Sstevel@tonic-gate$x = "aa";
50*0Sstevel@tonic-gate{ local %{$x};   %aa = qw/i j/; is($aa{i},"j"); undef $x; is($aa{i},"j"); }
51*0Sstevel@tonic-gateis($aa{a},"b");
52*0Sstevel@tonic-gate$x = "a";
53*0Sstevel@tonic-gate{ local %{$x x2};%aa = qw/k l/; is($aa{k},"l"); undef $x; is($aa{k},"l"); }
54*0Sstevel@tonic-gateis($aa{a},"b");
55*0Sstevel@tonic-gate$x = "aa";
56*0Sstevel@tonic-gate{ local %$x;     %aa = qw/m n/; is($aa{m},"n"); undef $x; is($aa{m},"n"); }
57*0Sstevel@tonic-gateis($aa{a},"b");
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gatesub test_err_localref () {
60*0Sstevel@tonic-gate    like($@,qr/Can't localize through a reference/,'error');
61*0Sstevel@tonic-gate}
62*0Sstevel@tonic-gate$x = \$aa;
63*0Sstevel@tonic-gatemy $y = \$aa;
64*0Sstevel@tonic-gateeval { local $$x; };      test_err_localref;
65*0Sstevel@tonic-gateeval { local ${$x}; };    test_err_localref;
66*0Sstevel@tonic-gateeval { local $$y; };      test_err_localref;
67*0Sstevel@tonic-gateeval { local ${$y}; };    test_err_localref;
68*0Sstevel@tonic-gateeval { local ${\$aa}; };  test_err_localref;
69*0Sstevel@tonic-gateeval { local ${\'aa'}; }; test_err_localref;
70*0Sstevel@tonic-gate$x = \@aa;
71*0Sstevel@tonic-gate$y = \@aa;
72*0Sstevel@tonic-gateeval { local @$x; };      test_err_localref;
73*0Sstevel@tonic-gateeval { local @{$x}; };    test_err_localref;
74*0Sstevel@tonic-gateeval { local @$y; };      test_err_localref;
75*0Sstevel@tonic-gateeval { local @{$y}; };    test_err_localref;
76*0Sstevel@tonic-gateeval { local @{\@aa}; };  test_err_localref;
77*0Sstevel@tonic-gateeval { local @{[]}; };    test_err_localref;
78*0Sstevel@tonic-gate$x = \%aa;
79*0Sstevel@tonic-gate$y = \%aa;
80*0Sstevel@tonic-gateeval { local %$x; };      test_err_localref;
81*0Sstevel@tonic-gateeval { local %{$x}; };    test_err_localref;
82*0Sstevel@tonic-gateeval { local %$y; };      test_err_localref;
83*0Sstevel@tonic-gateeval { local %{$y}; };    test_err_localref;
84*0Sstevel@tonic-gateeval { local %{\%aa}; };  test_err_localref;
85*0Sstevel@tonic-gateeval { local %{{a=>1}}; };test_err_localref;
86*0Sstevel@tonic-gate
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate{
89*0Sstevel@tonic-gate    # [perl #27638] when restoring a localized variable, the thing being
90*0Sstevel@tonic-gate    # freed shouldn't be visible
91*0Sstevel@tonic-gate    my $ok;
92*0Sstevel@tonic-gate    $x = 0;
93*0Sstevel@tonic-gate    sub X::DESTROY { $ok = !ref($x); }
94*0Sstevel@tonic-gate    {
95*0Sstevel@tonic-gate	local $x = \ bless {}, 'X';
96*0Sstevel@tonic-gate	1;
97*0Sstevel@tonic-gate    }
98*0Sstevel@tonic-gateok($ok,'old value not visible during restore');
99*0Sstevel@tonic-gate}
100