I would've expected all of these to print out 1, 2 and 3. But the last two don't. Why is that?
#!/usr/bin/perl -w
use strict;
sub mysub {
return (1,2,3);
}
print "--- foreach (mysub()) prints 1, 2 and 3\n";
foreach (mysub()) {
print $_, "\n";
}
print "--- foreach (mysub) prints 1, 2 and 3\n";
foreach (mysub) {
print $_, "\n";
}
print "--- foreach (sort(mysub())) prints 1, 2 and 3\n";
foreach (sort(mysub())) {
print $_, "\n";
}
print "--- foreach (sort mysub()) prints no elements (huh?)\n";
foreach (sort mysub()) {
print $_, "\n";
}
print "--- foreach (sort mysub) prints a single 'mysub' string (huh?)\n";
foreach (sort mysub) {
print $_, "\n";
}
It has this output:
--- foreach (mysub()) prints 1, 2 and 3
1
2
3
--- foreach (mysub) prints 1, 2 and 3
1
2
3
--- foreach (sort(mysub())) prints 1, 2 and 3
1
2
3
--- foreach (sort mysub()) prints no elements (huh?)
--- foreach (sort mysub) prints a single 'mysub' string (huh?)
mysub
Why not 1, 2 and 3 in every case? (Perl 5.18.2 on debian jessie)