11 no warnings 'experimental::smartmatch';
15 # The behaviour of the feature pragma should be tested by lib/feature.t
16 # using the tests in t/lib/feature/*. This file tests the behaviour of
17 # the switch ops themselves.
20 # Before loading feature, test the switch ops with CORE::
22 CORE::when(3) { pass "CORE::given and CORE::when"; continue }
23 CORE::default { pass "continue (without feature) and CORE::default" }
30 like($@, qr/^Can't "continue" outside/, "continue outside");
33 like($@, qr/^Can't "break" outside/, "break outside");
39 given(my $x = "bar") {
40 is($x, "bar", "given scope starts");
42 is($x, "foo", "given scope ends");
47 given(my $x = "foo") {
48 when(be_true(my $x = "bar")) {
49 is($x, "bar", "given scope starts");
51 is($x, "foo", "given scope ends");
55 given("inside") { check_outside1() }
56 sub check_outside1 { is($_, "inside", "\$_ is not lexically scoped") }
58 # Basic string/numeric comparisons and control flow
63 when(2) { $ok = 'two'; }
64 when(3) { $ok = 'three'; }
65 when(4) { $ok = 'four'; }
66 default { $ok = 'd'; }
68 is($ok, 'three', "numeric comparison");
75 when(2) { $ok = 'two'; }
76 when(3) { $ok = 'three'; }
77 when(4) { $ok = 'four'; }
78 default { $ok = 'd'; }
80 is($ok, 'three', "integer comparison");
86 when(3.1) { $ok1 = 'n'; }
87 when(3.0) { $ok1 = 'y'; continue }
88 when("3.0") { $ok2 = 'y'; }
89 default { $ok2 = 'n'; }
91 is($ok1, 'y', "more numeric (pt. 1)");
92 is($ok2, 'y', "more numeric (pt. 2)");
98 when("b") { $ok = 'B'; }
99 when("c") { $ok = 'C'; }
100 when("d") { $ok = 'D'; }
101 default { $ok = 'def'; }
103 is($ok, 'C', "string comparison");
109 when("b") { $ok = 'B'; }
110 when("c") { $ok = 'C'; continue }
111 when("c") { $ok = 'CC'; }
112 default { $ok = 'D'; }
114 is($ok, 'CC', "simple continue");
120 given (0) { when(undef) {$ok = 0} }
121 is($ok, 1, "Given(0) when(undef)");
126 given (0) { when($undef) {$ok = 0} }
127 is($ok, 1, 'Given(0) when($undef)');
132 given (0) { when($undef++) {$ok = 1} }
133 is($ok, 1, "Given(0) when($undef++)");
136 no warnings "uninitialized";
138 given (undef) { when(0) {$ok = 0} }
139 is($ok, 1, "Given(undef) when(0)");
142 no warnings "uninitialized";
145 given ($undef) { when(0) {$ok = 0} }
146 is($ok, 1, 'Given($undef) when(0)');
151 given ("") { when(undef) {$ok = 0} }
152 is($ok, 1, 'Given("") when(undef)');
157 given ("") { when($undef) {$ok = 0} }
158 is($ok, 1, 'Given("") when($undef)');
161 no warnings "uninitialized";
163 given (undef) { when("") {$ok = 0} }
164 is($ok, 1, 'Given(undef) when("")');
167 no warnings "uninitialized";
170 given ($undef) { when("") {$ok = 0} }
171 is($ok, 1, 'Given($undef) when("")');
176 given (undef) { when(undef) {$ok = 1} }
177 is($ok, 1, "Given(undef) when(undef)");
182 given (undef) { when($undef) {$ok = 1} }
183 is($ok, 1, 'Given(undef) when($undef)');
188 given ($undef) { when(undef) {$ok = 1} }
189 is($ok, 1, 'Given($undef) when(undef)');
194 given ($undef) { when($undef) {$ok = 1} }
195 is($ok, 1, 'Given($undef) when($undef)');
199 # Regular expressions
202 given("Hello, world!") {
204 { $ok1 = 'y'; continue}
206 { $ok1 = 'n'; continue}
207 when(/^(Hello,|Goodbye cruel) world[!.?]/)
208 { $ok2 = 'Y'; continue}
209 when(/^(Hello cruel|Goodbye,) world[!.?]/)
210 { $ok2 = 'n'; continue}
212 is($ok1, 'y', "regex 1");
213 is($ok2, 'Y', "regex 2");
218 my $test = "explicit numeric comparison (<)";
219 my $twenty_five = 25;
221 given($twenty_five) {
222 when ($_ < 10) { $ok = "ten" }
223 when ($_ < 20) { $ok = "twenty" }
224 when ($_ < 30) { $ok = "thirty" }
225 when ($_ < 40) { $ok = "forty" }
226 default { $ok = "default" }
228 is($ok, "thirty", $test);
233 my $test = "explicit numeric comparison (integer <)";
234 my $twenty_five = 25;
236 given($twenty_five) {
237 when ($_ < 10) { $ok = "ten" }
238 when ($_ < 20) { $ok = "twenty" }
239 when ($_ < 30) { $ok = "thirty" }
240 when ($_ < 40) { $ok = "forty" }
241 default { $ok = "default" }
243 is($ok, "thirty", $test);
247 my $test = "explicit numeric comparison (<=)";
248 my $twenty_five = 25;
250 given($twenty_five) {
251 when ($_ <= 10) { $ok = "ten" }
252 when ($_ <= 20) { $ok = "twenty" }
253 when ($_ <= 30) { $ok = "thirty" }
254 when ($_ <= 40) { $ok = "forty" }
255 default { $ok = "default" }
257 is($ok, "thirty", $test);
262 my $test = "explicit numeric comparison (integer <=)";
263 my $twenty_five = 25;
265 given($twenty_five) {
266 when ($_ <= 10) { $ok = "ten" }
267 when ($_ <= 20) { $ok = "twenty" }
268 when ($_ <= 30) { $ok = "thirty" }
269 when ($_ <= 40) { $ok = "forty" }
270 default { $ok = "default" }
272 is($ok, "thirty", $test);
277 my $test = "explicit numeric comparison (>)";
278 my $twenty_five = 25;
280 given($twenty_five) {
281 when ($_ > 40) { $ok = "forty" }
282 when ($_ > 30) { $ok = "thirty" }
283 when ($_ > 20) { $ok = "twenty" }
284 when ($_ > 10) { $ok = "ten" }
285 default { $ok = "default" }
287 is($ok, "twenty", $test);
291 my $test = "explicit numeric comparison (>=)";
292 my $twenty_five = 25;
294 given($twenty_five) {
295 when ($_ >= 40) { $ok = "forty" }
296 when ($_ >= 30) { $ok = "thirty" }
297 when ($_ >= 20) { $ok = "twenty" }
298 when ($_ >= 10) { $ok = "ten" }
299 default { $ok = "default" }
301 is($ok, "twenty", $test);
306 my $test = "explicit numeric comparison (integer >)";
307 my $twenty_five = 25;
309 given($twenty_five) {
310 when ($_ > 40) { $ok = "forty" }
311 when ($_ > 30) { $ok = "thirty" }
312 when ($_ > 20) { $ok = "twenty" }
313 when ($_ > 10) { $ok = "ten" }
314 default { $ok = "default" }
316 is($ok, "twenty", $test);
321 my $test = "explicit numeric comparison (integer >=)";
322 my $twenty_five = 25;
324 given($twenty_five) {
325 when ($_ >= 40) { $ok = "forty" }
326 when ($_ >= 30) { $ok = "thirty" }
327 when ($_ >= 20) { $ok = "twenty" }
328 when ($_ >= 10) { $ok = "ten" }
329 default { $ok = "default" }
331 is($ok, "twenty", $test);
336 my $test = "explicit string comparison (lt)";
337 my $twenty_five = "25";
339 given($twenty_five) {
340 when ($_ lt "10") { $ok = "ten" }
341 when ($_ lt "20") { $ok = "twenty" }
342 when ($_ lt "30") { $ok = "thirty" }
343 when ($_ lt "40") { $ok = "forty" }
344 default { $ok = "default" }
346 is($ok, "thirty", $test);
350 my $test = "explicit string comparison (le)";
351 my $twenty_five = "25";
353 given($twenty_five) {
354 when ($_ le "10") { $ok = "ten" }
355 when ($_ le "20") { $ok = "twenty" }
356 when ($_ le "30") { $ok = "thirty" }
357 when ($_ le "40") { $ok = "forty" }
358 default { $ok = "default" }
360 is($ok, "thirty", $test);
364 my $test = "explicit string comparison (gt)";
365 my $twenty_five = 25;
367 given($twenty_five) {
368 when ($_ ge "40") { $ok = "forty" }
369 when ($_ ge "30") { $ok = "thirty" }
370 when ($_ ge "20") { $ok = "twenty" }
371 when ($_ ge "10") { $ok = "ten" }
372 default { $ok = "default" }
374 is($ok, "twenty", $test);
378 my $test = "explicit string comparison (ge)";
379 my $twenty_five = 25;
381 given($twenty_five) {
382 when ($_ ge "40") { $ok = "forty" }
383 when ($_ ge "30") { $ok = "thirty" }
384 when ($_ ge "20") { $ok = "twenty" }
385 when ($_ ge "10") { $ok = "ten" }
386 default { $ok = "default" }
388 is($ok, "twenty", $test);
391 # Optimized-away comparisons
395 when (2 + 2 == 4) { $ok = 'y'; continue }
396 when (2 + 2 == 5) { $ok = 'n' }
398 is($ok, 'y', "Optimized-away comparison");
404 when (scalar 24) { $ok = 'n'; continue }
405 default { $ok = 'y' }
407 is($ok,'y','scalar()');
411 # (How to be both thorough and portable? Pinch a few ideas
412 # from t/op/filetest.t. We err on the side of portability for
416 my ($ok_d, $ok_f, $ok_r);
418 when(-d) {$ok_d = 1; continue}
419 when(!-f) {$ok_f = 1; continue}
420 when(-r) {$ok_r = 1; continue}
422 ok($ok_d, "Filetest -d");
423 ok($ok_f, "Filetest -f");
424 ok($ok_r, "Filetest -r");
427 # Sub and method calls
432 when(notfoo()) {$ok = 1}
434 ok($ok, "Sub call acts as boolean")
440 when(main->notfoo()) {$ok = 1}
442 ok($ok, "Class-method call acts as boolean")
449 when($obj->notfoo()) {$ok = 1}
451 ok($ok, "Object-method call acts as boolean")
454 # Other things that should not be smart matched
458 when( /(\d+)/ and ( 1 <= $1 and $1 <= 12 ) ) {
462 ok($ok, "bool not smartmatches");
472 ok($ok, "eof() not smartmatched");
477 my %foo = ("bar", 0);
479 when(exists $foo{bar}) {
483 ok($ok, "exists() not smartmatched");
493 ok($ok, "defined() not smartmatched");
499 when((1 == 1) && "bar") {
502 when((1 == 1) && $_ eq "foo") {
506 is($ok, 2, "((1 == 1) && \"bar\") not smartmatched");
511 for my $l (qw(a b c d)) {
513 when ($_ eq "b" .. $_ eq "c") { $n = 1 }
516 ok(($n xor $l =~ /[ad]/), 'when(E1..E2) evaluates in boolean context');
522 for my $l (qw(a b c d)) {
524 when ($_ eq "b" ... $_ eq "c") { $n = 1 }
527 ok(($n xor $l =~ /[ad]/), 'when(E1...E2) evaluates in boolean context');
534 when((1 == $ok) || "foo") {
538 ok($ok, '((1 == $ok) || "foo") smartmatched');
544 when((1 == $ok || undef) // "foo") {
548 ok($ok, '((1 == $ok || undef) // "foo") smartmatched');
551 # Make sure we aren't invoking the get-magic more than once
553 { # A helper class to count the number of accesses.
554 package FetchCounter;
557 bless {value => undef, count => 0}, $class;
560 my ($self, $val) = @_;
562 $self->{value} = $val;
566 # Avoid pre/post increment here
567 $self->{count} = 1 + $self->{count};
576 my $f = tie my $v, "FetchCounter";
578 { my $test_name = "Multiple FETCHes in given, due to aliasing";
588 is($ok, 1, "precheck: $test_name");
589 is($f->count(), 4, $test_name);
592 { my $test_name = "Only one FETCH (numeric when)";
595 is($f->count(), 0, "Sanity check: $test_name");
604 is($ok, 1, "precheck: $test_name");
605 is($f->count(), 1, $test_name);
608 { my $test_name = "Only one FETCH (string when)";
611 is($f->count(), 0, "Sanity check: $test_name");
620 is($ok, 1, "precheck: $test_name");
621 is($f->count(), 1, $test_name);
624 { my $test_name = "Only one FETCH (undef)";
627 is($f->count(), 0, "Sanity check: $test_name");
628 no warnings "uninitialized";
634 when(undef) {$ok = 0}
636 is($ok, 1, "precheck: $test_name");
637 is($f->count(), 1, $test_name);
645 is($first, 0, "Loop: second");
647 like($@, qr/^Can't "break" in a loop topicalizer/,
648 q{Can't "break" in a loop topicalizer});
651 is($first, 1, "Loop: first");
653 # Implicit break is okay
662 is($first, 0, "Explicit \$_: second");
664 like($@, qr/^Can't "break" in a loop topicalizer/,
665 q{Can't "break" in a loop topicalizer});
668 is($first, 1, "Explicit \$_: first");
670 # Implicit break is okay
679 sub foo {$called_foo = 1; "@_" eq "foo"}
681 sub bar {$called_bar = 1; "@_" eq "bar"}
682 my ($matched_foo, $matched_bar) = (0, 0);
684 when(\&bar) {$matched_bar = 1}
685 when(\&foo) {$matched_foo = 1}
687 is($called_foo, 1, "foo() was called");
688 is($called_bar, 1, "bar() was called");
689 is($matched_bar, 0, "bar didn't match");
690 is($matched_foo, 1, "foo did match");
698 my ($ok1, $ok2) = (0,0);
701 { $ok1 = 1; continue }
703 { $ok2 = 1; continue }
705 is($ok1, 1, "Calling sub directly (true)");
706 is($ok2, 1, "Calling sub indirectly (true)");
710 { $ok1 = 2; continue }
712 { $ok2 = 2; continue }
714 is($ok1, 1, "Calling sub directly (false)");
715 is($ok2, 1, "Calling sub indirectly (false)");
719 skip_if_miniperl("no dynamic loading on miniperl, no Scalar::Util", 14);
721 { package OverloadTest;
723 use overload '""' => sub{"string value of obj"};
724 use overload 'eq' => sub{"$_[0]" eq "$_[1]"};
726 use overload "~~" => sub {
727 my ($self, $other, $reversed) = @_;
729 $self->{left} = $other;
730 $self->{right} = $self;
731 $self->{reversed} = 1;
733 $self->{left} = $self;
734 $self->{right} = $other;
735 $self->{reversed} = 0;
738 return $self->{retval};
742 my ($pkg, $retval) = @_;
751 my $test = "Overloaded obj in given (true)";
752 my $obj = OverloadTest->new(1);
755 when ("other arg") {$matched = 1}
756 default {$matched = 0}
759 is($obj->{called}, 1, "$test: called");
760 ok($matched, "$test: matched");
764 my $test = "Overloaded obj in given (false)";
765 my $obj = OverloadTest->new(0);
768 when ("other arg") {$matched = 1}
771 is($obj->{called}, 1, "$test: called");
772 ok(!$matched, "$test: not matched");
776 my $test = "Overloaded obj in when (true)";
777 my $obj = OverloadTest->new(1);
780 when ($obj) {$matched = 1}
781 default {$matched = 0}
784 is($obj->{called}, 1, "$test: called");
785 ok($matched, "$test: matched");
786 is($obj->{left}, "topic", "$test: left");
787 is($obj->{right}, "string value of obj", "$test: right");
788 ok($obj->{reversed}, "$test: reversed");
792 my $test = "Overloaded obj in when (false)";
793 my $obj = OverloadTest->new(0);
796 when ($obj) {$matched = 1}
797 default {$matched = 0}
800 is($obj->{called}, 1, "$test: called");
801 ok(!$matched, "$test: not matched");
802 is($obj->{left}, "topic", "$test: left");
803 is($obj->{right}, "string value of obj", "$test: right");
804 ok($obj->{reversed}, "$test: reversed");
814 is($ok, 1, "postfix undef");
820 $ok += 2 when 9.1685;
821 $ok += 4 when $_ > 4;
822 $ok += 8 when $_ < 2.5;
824 is($ok, 8, "postfix numeric");
829 $ok = 1, continue when $_ eq "apple";
831 $ok = 0 when "banana";
833 is($ok, 3, "postfix string");
838 do { $ok = 1; continue } when /pea/;
844 is($ok, 7, "postfix regex");
846 # be_true is defined at the beginning of the file
849 given(my $x = "foo") {
851 is($x, "foo", "scope inside ... when my \$x = ...");
853 } when be_true(my $x = "bar");
854 is($x, "bar", "scope after ... when my \$x = ...");
860 my $x = 2, continue when be_true();
861 is($x, undef, "scope after my \$x = ... when ...");
865 # Tests for last and next in when clauses
876 is($letter, "b", "last in when");
879 LETTER1: for ("a".."e") {
882 when ("b") { last LETTER1 }
886 is($letter, "b", "last LABEL in when");
891 when (/b|d/) { next }
896 is($letter, "a,c,e,", "next in when");
899 LETTER2: for ("a".."e") {
901 when (/b|d/) { next LETTER2 }
906 is($letter, "a,c,e,", "next LABEL in when");
908 # Test goto with given/when
913 GIVEN1: given ($flag) {
917 is($flag, 0, "goto GIVEN1");
922 when (0) { $flag = 1; }
927 is($flag, 1, "goto inside given");
932 when (0) { $flag = 1; goto GIVEN3; $flag = 2; }
936 is($flag, 1, "goto inside given and when");
941 when (0) { $flag = 1; goto GIVEN4; $flag = 2; }
945 is($flag, 1, "goto inside for and when");
951 when (0) { $flag = 1; goto GIVEN5; $flag = 2; }
955 is($flag, 1, "goto inside given and when to the given stmt");
958 # test with unreified @_ in smart match [perl #71078]
959 sub unreified_check { ok([@_] ~~ \@_) } # should always match
960 unreified_check(1,2,"lala");
961 unreified_check(1,2,undef);
962 unreified_check(undef);
963 unreified_check(undef,"");
965 # Test do { given } as a rvalue
970 my @things = (11 .. 26); # 16 elements
971 my @exp = (5, 16, 9);
974 my $scalar = do { given ($_) {
975 when (0) { $lexical }
976 when (2) { 'void'; 8, 9 }
979 is($scalar, shift(@exp), "rvalue given - simple scalar [$_]");
988 my $scalar = do { given ($_) {
993 is($scalar, shift(@exp), "rvalue given - postfix scalar [$_]");
1000 my $scalar = do { given ($_) {
1006 is($scalar, shift(@exp), "rvalue given - default scalar [$_]");
1011 my @things = (11 .. 13);
1012 my @exp = ('3 4 5', '11 12 13', '8 9');
1014 my @list = do { given ($_) {
1016 when (2) { my $fake = 'void'; 8, 9 }
1019 is("@list", shift(@exp), "rvalue given - simple list [$_]");
1025 my @exp = ('3 4 5', '6 7', '12');
1027 my @list = do { given ($_) {
1032 is("@list", shift(@exp), "rvalue given - postfix list [$_]");
1037 my @things = (11 .. 20); # 10 elements
1038 my @exp = ('m o o', '8 10', '8 10');
1040 my @list = do { given ($_) {
1041 when (0) { "moo" =~ /(.)/g }
1042 default { 8, scalar(@things) }
1045 is("@list", shift(@exp), "rvalue given - default list [$_]");
1050 my @exp = ('6 7', '', '6 7');
1052 my @list = do { given ($_) {
1053 continue when $_ <= 1;
1058 is("@list", shift(@exp), "rvalue given - default list [$_]");
1062 # Context propagation
1063 my $smart_hash = sub {
1064 do { given ($_[0]) {
1066 when ([ 1 .. 3 ]) { 1 .. 3 }
1067 when (4) { my $fake; do { 4, 5 } }
1073 $scalar = $smart_hash->();
1074 is($scalar, 'undef', "rvalue given - scalar context propagation [undef]");
1076 $scalar = $smart_hash->(4);
1077 is($scalar, 5, "rvalue given - scalar context propagation [4]");
1079 $scalar = $smart_hash->(999);
1080 is($scalar, undef, "rvalue given - scalar context propagation [999]");
1084 @list = $smart_hash->();
1085 is("@list", 'undef', "rvalue given - list context propagation [undef]");
1087 @list = $smart_hash->(2);
1088 is("@list", '1 2 3', "rvalue given - list context propagation [2]");
1090 @list = $smart_hash->(4);
1091 is("@list", '4 5', "rvalue given - list context propagation [4]");
1093 @list = $smart_hash->(999);
1094 is("@list", '', "rvalue given - list context propagation [999]");
1098 my @list = 10 .. 15;
1107 when (@list[0..2]) {
1112 is("@in_list", "10 15", "when(array)");
1113 is("@in_slice", "10", "when(array slice)");
1117 my %list = map { $_ => $_ } "a" .. "f";
1120 for ("a", "e", "i") {
1126 when (@list{"a".."c"}) {
1131 is("@in_list", "a e", "when(hash)");
1132 is("@in_slice", "a", "when(hash slice)");
1135 { # RT#84526 - Handle magical TARG
1136 my $x = my $y = "aaa";
1139 is(pos, undef, "handle magical TARG");
1145 # Test that returned values are correctly propagated through several context
1146 # levels (see RT #93548).
1153 our ($when_loc, $given_loc, $ext_loc);
1161 our $given_glob = 5;
1162 local $given_loc = 6;
1166 when (1) { my $when_lex = 1 }
1167 when (2) { our $when_glob = 2 }
1168 when (3) { local $when_loc = 3 }
1170 when (4) { $given_lex }
1171 when (5) { $given_glob }
1172 when (6) { $given_loc }
1174 when (7) { $ext_lex }
1175 when (8) { $ext_glob }
1176 when (9) { $ext_loc }
1182 my @descriptions = qw<
1198 for my $id (0 .. 9) {
1199 my $desc = $descriptions[$id];
1201 my $res = $tester->($id);
1202 is $res, $id, "plain call - $desc";
1205 my $id_plus_1 = $id + 1;
1206 given ($id_plus_1) {
1215 $tester->($id_plus_1);
1220 is $res, $id, "across continue and default - $desc";
1224 # Check that values returned from given/when are destroyed at the right time.
1241 my @descriptions = qw<
1248 for my $id (0 .. 3) {
1249 my $desc = $descriptions[$id];
1258 when (0) { Fmurrr->new($destroyed, 0) }
1259 when (1) { my $y = Fmurrr->new($destroyed, 1); break }
1260 when (2) { $x = Fmurrr->new($destroyed, 2); continue }
1262 default { Fmurrr->new($destroyed, 3) }
1265 $res_id = $res->{id};
1267 $res_id = $id if $id == 1; # break doesn't return anything
1269 is $res_id, $id, "given/when returns the right object - $desc";
1270 is $destroyed, 1, "given/when does not leak - $desc";
1274 # break() must reset the stack
1285 is "@res", "1", "break resets the stack";
1289 # must ensure $_ is initialised and cleared at start/end of given block
1295 sub DESTROY { $d++ };
1298 no warnings 'experimental::lexical_topic';
1301 ::is($_->[0], 7, "is [7]");
1303 ::is($_, 5, "is 5");
1304 ::is($d, 1, "DESTROY called once");
1311 # Okay, that'll do for now. The intricacies of the smartmatch
1312 # semantics are tested in t/op/smartmatch.t. Taintedness of
1313 # returned values is checked in t/op/taint.t.