xref: /openbsd-src/gnu/usr.bin/perl/t/cmd/switch.t (revision 0dc2eace317a852e5f4da130db2d009adf26967e)
14a4f25f9Sdownsj#!./perl
24a4f25f9Sdownsj
34a4f25f9Sdownsjprint "1..18\n";
44a4f25f9Sdownsj
54a4f25f9Sdownsjsub foo1 {
64a4f25f9Sdownsj    $_ = shift(@_);
74a4f25f9Sdownsj    $a = 0;
84a4f25f9Sdownsj    until ($a++) {
94a4f25f9Sdownsj	next if $_ eq 1;
104a4f25f9Sdownsj	next if $_ eq 2;
114a4f25f9Sdownsj	next if $_ eq 3;
124a4f25f9Sdownsj	next if $_ eq 4;
134a4f25f9Sdownsj	return 20;
144a4f25f9Sdownsj    }
154a4f25f9Sdownsj    continue {
164a4f25f9Sdownsj	return $_;
174a4f25f9Sdownsj    }
184a4f25f9Sdownsj}
194a4f25f9Sdownsj
20*0dc2eaceSmillertprint foo1(0) == 20 ? "ok 1\n" : "not ok 1\n";
21*0dc2eaceSmillertprint foo1(1) == 1 ? "ok 2\n" : "not ok 2\n";
22*0dc2eaceSmillertprint foo1(2) == 2 ? "ok 3\n" : "not ok 3\n";
23*0dc2eaceSmillertprint foo1(3) == 3 ? "ok 4\n" : "not ok 4\n";
24*0dc2eaceSmillertprint foo1(4) == 4 ? "ok 5\n" : "not ok 5\n";
25*0dc2eaceSmillertprint foo1(5) == 20 ? "ok 6\n" : "not ok 6\n";
264a4f25f9Sdownsj
274a4f25f9Sdownsjsub foo2 {
284a4f25f9Sdownsj    $_ = shift(@_);
294a4f25f9Sdownsj    {
304a4f25f9Sdownsj	last if $_ == 1;
314a4f25f9Sdownsj	last if $_ == 2;
324a4f25f9Sdownsj	last if $_ == 3;
334a4f25f9Sdownsj	last if $_ == 4;
344a4f25f9Sdownsj    }
354a4f25f9Sdownsj    continue {
364a4f25f9Sdownsj	return 20;
374a4f25f9Sdownsj    }
384a4f25f9Sdownsj    return $_;
394a4f25f9Sdownsj}
404a4f25f9Sdownsj
41*0dc2eaceSmillertprint foo2(0) == 20 ? "ok 7\n" : "not ok 7\n";
42*0dc2eaceSmillertprint foo2(1) == 1 ? "ok 8\n" : "not ok 8\n";
43*0dc2eaceSmillertprint foo2(2) == 2 ? "ok 9\n" : "not ok 9\n";
44*0dc2eaceSmillertprint foo2(3) == 3 ? "ok 10\n" : "not ok 10\n";
45*0dc2eaceSmillertprint foo2(4) == 4 ? "ok 11\n" : "not ok 11\n";
46*0dc2eaceSmillertprint foo2(5) == 20 ? "ok 12\n" : "not ok 12\n";
474a4f25f9Sdownsj
484a4f25f9Sdownsjsub foo3 {
494a4f25f9Sdownsj    $_ = shift(@_);
504a4f25f9Sdownsj    if (/^1/) {
514a4f25f9Sdownsj	return 1;
524a4f25f9Sdownsj    }
534a4f25f9Sdownsj    elsif (/^2/) {
544a4f25f9Sdownsj	return 2;
554a4f25f9Sdownsj    }
564a4f25f9Sdownsj    elsif (/^3/) {
574a4f25f9Sdownsj	return 3;
584a4f25f9Sdownsj    }
594a4f25f9Sdownsj    elsif (/^4/) {
604a4f25f9Sdownsj	return 4;
614a4f25f9Sdownsj    }
624a4f25f9Sdownsj    else {
634a4f25f9Sdownsj	return 20;
644a4f25f9Sdownsj    }
654a4f25f9Sdownsj    return 40;
664a4f25f9Sdownsj}
674a4f25f9Sdownsj
68*0dc2eaceSmillertprint foo3(0) == 20 ? "ok 13\n" : "not ok 13\n";
69*0dc2eaceSmillertprint foo3(1) == 1 ? "ok 14\n" : "not ok 14\n";
70*0dc2eaceSmillertprint foo3(2) == 2 ? "ok 15\n" : "not ok 15\n";
71*0dc2eaceSmillertprint foo3(3) == 3 ? "ok 16\n" : "not ok 16\n";
72*0dc2eaceSmillertprint foo3(4) == 4 ? "ok 17\n" : "not ok 17\n";
73*0dc2eaceSmillertprint foo3(5) == 20 ? "ok 18\n" : "not ok 18\n";
74