This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
lvret.t: To-do tests for \local $scalar=
[perl5.git] / t / op / lvref.t
CommitLineData
72ed4618
FC
1BEGIN {
2 chdir 't';
3 require './test.pl';
4 set_up_inc("../lib");
5}
6
11ea28ee 7plan 22;
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)';
11ea28ee
FC
53is eval '\local $l = \3; $l', 3, '\local $scalar assignment';
54off;
55is $l, undef, 'localisation unwound';
56on;
72ed4618
FC
57
58# Array Elements
59
60# ...
61
62# Hash Elements
63
64# ...
65
66# Arrays
67
68# ...
69
70# Hashes
71
72# ...
73
74# Subroutines
75
76# ...
77
78# Mixed List Assignments
79
80# ...
81
82# Errors
83
b3717a0e
FC
84off;
85eval { my $x; \$x = 3 };
86like $@, qr/^Assigned value is not a reference at/, 'assigning non-ref';
87eval { my $x; \$x = [] };
88like $@, qr/^Assigned value is not a SCALAR reference at/,
89 'assigning non-scalar ref to scalar ref';
d8a875d9
FC
90eval { \$::x = [] };
91like $@, qr/^Assigned value is not a SCALAR reference at/,
92 'assigning non-scalar ref to package scalar ref';
b3717a0e 93on;
781ff25d
FC
94
95# Miscellaneous
96
97{
98 my($x,$y);
99 sub {
100 sub {
101 \$x = \$y;
102 }->();
103 is \$x, \$y, 'lexical alias affects outer closure';
104 }->();
105 is \$x, \$y, 'lexical alias affects outer sub where vars are declared';
106}
81cb1af6
FC
107
108{ # PADSTALE has a double meaning
109 use feature 'lexical_subs', 'signatures', 'state';
110 no warnings 'experimental';
111 my $c;
112 my sub s ($arg) {
113 state $x = ++$c;
114 if ($arg == 3) { return $c }
115 goto skip if $arg == 2;
116 my $y;
117 skip:
118 # $y is PADSTALE the 2nd time
119 \$x = \$y if $arg == 2;
120 }
121 s(1);
122 s(2);
123 is s(3), 1, 'padstale alias should not reset state'
124}
cf5d2d91
FC
125
126off;
127SKIP: {
128 skip_without_dynamic_extension('List/Util');
129 require Scalar::Util;
130 my $a;
131 Scalar::Util::weaken($r = \$a);
132 \$a = $r;
133 pass 'no crash when assigning \$lex = $weakref_to_lex'
134}