This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
lvref.t: To-do test for PADSTALE handling
[perl5.git] / t / op / lvref.t
CommitLineData
72ed4618
FC
1BEGIN {
2 chdir 't';
3 require './test.pl';
4 set_up_inc("../lib");
5}
6
81cb1af6 7plan 19;
72ed4618 8
53abf431
FC
9sub on { $::TODO = ' ' }
10sub off{ $::TODO = '' }
72ed4618
FC
11
12eval '\$x = \$y';
13like $@, qr/^Experimental lvalue references not enabled/,
14 'error when feature is disabled';
15
16use feature 'lvalue_refs';
17
18{
19 my($w,$c);
20 local $SIG{__WARN__} = sub { $c++; $w = shift };
21 eval '\$x = \$y';
22 is $c, 1, 'one warning from lv ref assignment';
23 like $w, qr/^Lvalue references are experimental/,
24 'experimental warning';
25}
26
27no warnings 'experimental::lvalue_refs';
28
29# Scalars
30
31eval '\$x = \$y';
32is \$x, \$y, '\$pkg_scalar = ...';
33my $m;
53abf431 34\$m = \$y;
72ed4618 35is \$m, \$y, '\$lexical = ...';
53abf431 36on;
72ed4618
FC
37eval '\my $n = \$y';
38is \$n, \$y, '\my $lexical = ...';
39@_ = \$_;
40eval '\($x) = @_';
41is \$x, \$_, '\($pkgvar) = ... gives list context';
42my $o;
43eval '\($o) = @_';
44is \$o, \$_, '\($lexical) = ... gives list cx';
45eval '\(my $p) = @_';
46is \$p, \$_, '\(my $lexical) = ... gives list cx';
47eval '\($_a, my $a) = @{[\$b, \$c]}';
48is \$_a, \$b, 'package scalar in \(...)';
49is \$a, \$c, 'lex scalar in \(...)';
50eval '(\$_b, \my $b) = @{[\$b, \$c]}';
51is \$_b, \$::b, 'package scalar in (\$foo, \$bar)';
52is \$b, \$c, 'lex scalar in (\$foo, \$bar)';
53
54# Array Elements
55
56# ...
57
58# Hash Elements
59
60# ...
61
62# Arrays
63
64# ...
65
66# Hashes
67
68# ...
69
70# Subroutines
71
72# ...
73
74# Mixed List Assignments
75
76# ...
77
78# Errors
79
b3717a0e
FC
80off;
81eval { my $x; \$x = 3 };
82like $@, qr/^Assigned value is not a reference at/, 'assigning non-ref';
83eval { my $x; \$x = [] };
84like $@, qr/^Assigned value is not a SCALAR reference at/,
85 'assigning non-scalar ref to scalar ref';
d8a875d9
FC
86eval { \$::x = [] };
87like $@, qr/^Assigned value is not a SCALAR reference at/,
88 'assigning non-scalar ref to package scalar ref';
b3717a0e 89on;
781ff25d
FC
90
91# Miscellaneous
92
93{
94 my($x,$y);
95 sub {
96 sub {
97 \$x = \$y;
98 }->();
99 is \$x, \$y, 'lexical alias affects outer closure';
100 }->();
101 is \$x, \$y, 'lexical alias affects outer sub where vars are declared';
102}
81cb1af6
FC
103
104{ # PADSTALE has a double meaning
105 use feature 'lexical_subs', 'signatures', 'state';
106 no warnings 'experimental';
107 my $c;
108 my sub s ($arg) {
109 state $x = ++$c;
110 if ($arg == 3) { return $c }
111 goto skip if $arg == 2;
112 my $y;
113 skip:
114 # $y is PADSTALE the 2nd time
115 \$x = \$y if $arg == 2;
116 }
117 s(1);
118 s(2);
119 is s(3), 1, 'padstale alias should not reset state'
120}