1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
|
#!./perl
BEGIN {
chdir 't' if -d 't';
require "./test.pl";
set_up_inc('../lib');
}
use strict;
use warnings;
use utf8;
my @have;
@have = ();
# Simplest case is an explicit list:
for my ($q, $r) ('A', 'B', 'C', 'D') {
push @have, "$q;$r";
}
is("@have", 'A;B C;D', 'explicit list');
@have = ();
for my ($q, $r) (reverse 'A', 'B', 'C', 'D') {
push @have, "$q;$r";
}
is("@have", 'D;C B;A', 'explicit list reversed');
@have = ();
for my ($q, $r) ('A', 'B', 'C', 'D', 'E', 'F') {
push @have, "$q;$r";
}
is("@have", 'A;B C;D E;F', 'explicit list three iterations');
@have = ();
for my ($q, $r, $s) ('A', 'B', 'C', 'D', 'E', 'F') {
push @have, "$q;$r;$s";
}
is("@have", 'A;B;C D;E;F', 'explicit list triplets');
@have = ();
for my ($q, $r, $s,) ('A', 'B', 'C', 'D', 'E', 'F') {
push @have, "$q;$r;$s";
}
is("@have", 'A;B;C D;E;F', 'trailing comma n-fold');
@have = ();
for my ($q, $r, $s) ('A', 'B', 'C', 'D', 'E') {
push @have, join ';', map { $_ // 'undef' } $q, $r, $s;
}
is("@have", 'A;B;C D;E;undef', 'incomplete explicit list');
@have = ();
for my ($q, $r, $s) (reverse 'A', 'B', 'C', 'D', 'E') {
push @have, join ';', map { $_ // 'undef' } $q, $r, $s;
}
is("@have", 'E;D;C B;A;undef', 'incomplete explicit list reversed');
# This two are legal syntax and actually indistinguishable from for my $q () ...
@have = ();
for my ($q,) ('A', 'B', 'C', 'D', 'E', 'F') {
push @have, $q;
}
is("@have", 'A B C D E F', 'trailing comma one-at-a-time');
@have = ();
for my ($q) ('A', 'B', 'C', 'D', 'E', 'F') {
push @have, $q;
}
is("@have", 'A B C D E F', 'one-at-a-time');
# Arrays have an optimised case in pp_iter:
{
no strict 'vars';
@array = split ' ', 'Dogs have owners, cats have staff.';
my $count = scalar @array;
@have = ();
for my ($q, $r, $s) (@array) {
push @have, "$q;$r;$s";
}
is("@have", 'Dogs;have;owners, cats;have;staff.', 'package array');
is(scalar @array, $count, 'package array size unchanged');
@have = ();
for my ($q, $r, $s) (reverse @array) {
push @have, "$q;$r;$s";
}
is("@have", 'staff.;have;cats owners,;have;Dogs', 'package array reversed');
is(scalar @array, $count, 'package array reversed size unchanged');
@have = ();
for my ($q, $r, $s, $t) (@array) {
push @have, join ';', map { $_ // '!' } $q, $r, $s, $t;
}
is("@have", 'Dogs;have;owners,;cats have;staff.;!;!', 'incomplete package array');
@have = ();
for my ($q, $r, $s, $t) (reverse @array) {
push @have, join ';', map { $_ // '!' } $q, $r, $s, $t;
}
is("@have", 'staff.;have;cats;owners, have;Dogs;!;!', 'incomplete package array reversed');
is(scalar @array, $count, 'incomplete package array size unchanged');
# And for our last test, we trash @array
for my ($q, $r) (@array) {
($q, $r) = ($r, $q);
}
is("@array", 'have Dogs cats owners, staff. have', 'package array aliased');
is(scalar @array, $count, 'incomplete package array reversed size unchanged');
}
my @array = split ' ', 'God is real, unless declared integer.';
my $count = scalar @array;
@have = ();
for my ($q, $r, $s) (@array) {
push @have, "$q;$r;$s";
}
is("@have", 'God;is;real, unless;declared;integer.', 'lexical array');
is(scalar @array, $count, 'lexical array size unchanged');
@have = ();
for my ($q, $r, $s) (reverse @array) {
push @have, "$q;$r;$s";
}
is("@have", 'integer.;declared;unless real,;is;God', 'lexical array reversed');
is(scalar @array, $count, 'lexical array reversed size unchanged');
@have = ();
for my ($q, $r, $s, $t) (@array) {
push @have, join ';', map { $_ // '!' } $q, $r, $s, $t;
}
is("@have", 'God;is;real,;unless declared;integer.;!;!', 'incomplete lexical array');
is(scalar @array, $count, 'incomplete lexical array size unchanged');
@have = ();
for my ($q, $r, $s, $t) (reverse @array) {
push @have, join ';', map { $_ // '!' } $q, $r, $s, $t;
}
is("@have", 'integer.;declared;unless;real, is;God;!;!', 'incomplete lexical array reversed');
is(scalar @array, $count, 'incomplete lexical array reversed size unchanged');
for my ($q, $r) (@array) {
$q = uc $q;
$r = ucfirst $r;
}
is("@array", 'GOD Is REAL, Unless DECLARED Integer.', 'lexical array aliased');
# Integer ranges have an optimised case in pp_iter:
@have = ();
for my ($q, $r, $s) (0..5) {
push @have, "$q;$r;$s";
}
is("@have", '0;1;2 3;4;5', 'integer list');
@have = ();
for my ($q, $r, $s) (reverse 0..5) {
push @have, "$q;$r;$s";
}
is("@have", '5;4;3 2;1;0', 'integer list reversed');
@have = ();
for my ($q, $r, $s) (1..5) {
push @have, join ';', map { $_ // 'undef' } $q, $r, $s;
}
is("@have", '1;2;3 4;5;undef', 'incomplete integer list');
@have = ();
for my ($q, $r, $s) (reverse 1..5) {
push @have, join ';', map { $_ // 'Thunderbirds are go' } $q, $r, $s;
}
is("@have", '5;4;3 2;1;Thunderbirds are go', 'incomplete integer list reversed');
# String ranges have an optimised case in pp_iter:
@have = ();
for my ($q, $r, $s) ('A'..'F') {
push @have, "$q;$r;$s";
}
is("@have", 'A;B;C D;E;F', 'string list');
@have = ();
for my ($q, $r, $s) (reverse 'A'..'F') {
push @have, "$q;$r;$s";
}
is("@have", 'F;E;D C;B;A', 'string list reversed');
@have = ();
for my ($q, $r, $s) ('B'..'F') {
push @have, join ';', map { $_ // 'undef' } $q, $r, $s;
}
is("@have", 'B;C;D E;F;undef', 'incomplete string list');
@have = ();
for my ($q, $r, $s) (reverse 'B'..'F') {
push @have, join ';', map { $_ // 'undef' } $q, $r, $s;
}
is("@have", 'F;E;D C;B;undef', 'incomplete string list reversed');
# Hashes are expanded as regular lists, so there's nothing particularly
# special here:
{
no strict;
%hash = (
perl => 'rules',
beer => 'foamy',
);
@have = ();
for my ($key, $value) (%hash) {
push @have, "$key;$value";
}
my $got = "@have";
if ($got =~ /^perl/) {
is($got, 'perl;rules beer;foamy', 'package hash key/value iteration');
}
else {
is($got, 'beer;foamy perl;rules', 'package hash key/value iteration');
}
@have = ();
for my ($value, $key) (reverse %hash) {
push @have, "$key;$value";
}
$got = "@have";
if ($got =~ /^perl/) {
is($got, 'perl;rules beer;foamy', 'package hash key/value reverse iteration');
}
else {
is($got, 'beer;foamy perl;rules', 'package hash key/value reverse iteration');
}
# values are aliases. As ever. Keys are copies.
for my ($key, $value) (%hash) {
$key = ucfirst $key;
$value = uc $value;
}
$got = join ';', %hash;
if ($got =~ /^perl/i) {
is($got, 'perl;RULES;beer;FOAMY', 'package hash value iteration aliases');
}
else {
is($got, 'beer;FOAMY;perl;RULES', 'package hash value iteration aliases');
}
}
my %hash = (
beer => 'street',
gin => 'lane',
);
@have = ();
for my ($key, $value) (%hash) {
push @have, "$key;$value";
}
my $got = "@have";
if ($got =~ /^gin/) {
is($got, 'gin;lane beer;street', 'lexical hash key/value iteration');
}
else {
is($got, 'beer;street gin;lane', 'lexical hash key/value iteration');
}
@have = ();
for my ($value, $key) (reverse %hash) {
push @have, "$key;$value";
}
$got = "@have";
if ($got =~ /^gin/) {
is($got, 'gin;lane beer;street', 'lexical hash key/value reverse iteration');
}
else {
is($got, 'beer;street gin;lane', 'lexical hash key/value reverse iteration');
}
# values are aliases, keys are copies, so this is a daft thing to do:
for my ($key, $value) (%hash) {
($key, $value) = ($value, $key);
}
$got = join ';', %hash;
if ($got =~ /^gin/i) {
is($got, 'gin;gin;beer;beer', 'lexical hash value iteration aliases');
}
else {
is($got, 'beer;beer;gin;gin', 'lexical hash value iteration aliases');
}
my $code = 'for my ($q, $r) (6, 9) {}; 42';
$got = eval $code;
is($@, "", 'test code generated no error');
is($got, 42, 'test code ran');
$code =~ s/my/our/;
like($code, qr/for our \(/, 'for our code set up correctly');
$got = eval $code;
like($@, qr/^Missing \$ on loop variable /, 'for our code generated error');
is($got, undef, 'for our did not run');
$code =~ s/ our//;
like($code, qr/for \(/, 'for () () code set up correctly');
$got = eval "no strict 'vars'; $code";
like($@, qr/^syntax error /, 'for () () code generated error');
is($got, undef, 'for () () did not run');
# Yes, I looked these up:
my @Quercus = qw(robor petraea cerris);
# I should be able to sneak this past the children for some years...
my @Allium = qw(cepa sativum ampeloprasum);
for my ($left, $right) (@Quercus, @Allium) {
$left = uc $left;
$right = reverse $right;
}
is("@Quercus", 'ROBOR aeartep CERRIS', 'for () () aliases 1');
is("@Allium", 'apec SATIVUM musarpolepma', 'for () () aliases 2');
is(eval {
for my ($left, $right) (@Allium, undef, @Quercus) {
$left = reverse $left;
$right = lc($right // "");
}
54;
}, undef, 'aliased rvalue');
like($@, qr/^Modification of a read-only value attempted/,
'aliased rvalue threw the correct exception');
is("@Allium", 'cepa sativum ampeloprasum', 'for () () aliases 3');
is("@Quercus", 'ROBOR aeartep CERRIS', 'for () () aliases 4');
is(eval {
for my ($left, $right) (@Quercus) {
$left = lc $left;
$right = reverse($right // "");
}
54;
}, undef, 'padded with literal undef');
like($@, qr/^Modification of a read-only value attempted/,
'padded with literal undef threw the correct exception');
is("@Quercus", 'robor petraea cerris', 'side effects observed');
my @numbers = (3, 2, 1, 0);
my $redo;
my $next;
my $done;
my $continue;
for my ($left, $right) (@numbers) {
$left *= 3;
++$right;
redo
unless $redo++;
++$done;
next
unless $next++;
$left *= 5;
$right *= 7;
} continue {
$continue .= 'x';
}
is("@numbers", '27 4 15 7', 'expected result');
is($redo, 3, 'redo reached thrice');
is($next, 2, 'next reached twice');
is($continue, 'xx', 'continue reached twice');
{
no strict 'vars';
# Important that this is a package variable, so that we test that the parser
# ends the scope of the my at the ')' and generates the correct ops to read
# from the symbol table, not the pad.
@Lamini = qw(alpaca guanaco llama vicuña);
@have = ();
for my ($domestic, $wild) (@Lamini) {
push @have, "$domestic;$wild";
}
is("@have", 'alpaca;guanaco llama;vicuña', 'comma test 0');
@have = ();
for my ($domestic, $wild,) (@Lamini) {
push @have, "$domestic;$wild";
}
is("@have", 'alpaca;guanaco llama;vicuña', 'comma test 1');
@have = ();
for my ($domestic,, $wild) (@Lamini) {
push @have, "$domestic;$wild";
}
is("@have", 'alpaca;guanaco llama;vicuña', 'comma test 2');
@have = ();
for my ($domestic,, $wild,) (@Lamini) {
push @have, "$domestic;$wild";
}
is("@have", 'alpaca;guanaco llama;vicuña', 'comma test 3');
@have = ();
for my ($domestic,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, $wild) (@Lamini) {
push @have, "$domestic;$wild";
}
is("@have", 'alpaca;guanaco llama;vicuña', 'comma test 42');
}
# Spaces shouldn't trigger parsing errors:
{
my @correct = ('Pointy', 'Up', 'Flamey', 'Down');
@have = ();
for my ($one) (@correct) {
push @have, $one;
}
is("@have", "@correct", 'for my ($one)');
@have = ();
for my($one) (@correct) {
push @have, $one;
}
is("@have", "@correct", 'for my($one)');
@have = ();
# This is lots of lovely whitespace:
for my
($end, $orientation) (@correct) {
push @have, "$end end $orientation";
}
is("@have", "Pointy end Up Flamey end Down", 'for my ($one, $two)');
@have = ();
for my($end, $orientation) (@correct) {
push @have, "$end end $orientation";
}
is("@have", "Pointy end Up Flamey end Down", 'for my ($one, $two)');
}
done_testing();
|