The perl grep operator can be used to determine if some condition holds
for some elements of a list. In scalar context, it returns the number of times the condition was true. This means it's semantically a bad fit for finding a single element in a list because it always processes the
complete list. But it can be used as test expression for a statement
modifier ... so far, so bad.
Upon spending a time thinking through this, it dawned to be that eval {
} is syntactically an expression, hence
------
@a = qw(a b c d e);
while ($l = <STDIN>) {
chomp($l);
print("found $l\n") if eval { $_ eq $l and return 1 for @a };
}
-------
is valid Perl (which works, obviously).
Στις 2/4/2021 9:37 μ.μ., ο/η Rainer Weikusat έγραψε:
The perl grep operator can be used to determine if some condition holds
for some elements of a list. In scalar context, it returns the number of
times the condition was true. This means it's semantically a bad fit for
finding a single element in a list because it always processes the
complete list. But it can be used as test expression for a statement
modifier ... so far, so bad.
Upon spending a time thinking through this, it dawned to be that eval {
} is syntactically an expression, hence
------
@a = qw(a b c d e);
while ($l = <STDIN>) {
chomp($l);
print("found $l\n") if eval { $_ eq $l and return 1 for @a };
}
-------
is valid Perl (which works, obviously).
use the clever equal ~~
is valid Perl (which works, obviously).
Rainer Weikusat <[email protected]> wrote:
[...]
is valid Perl (which works, obviously).
Thanks for this bit of strangeness, it caused me to look up the Perl
"secret operators" page:
https://metacpan.org/pod/distribution/perlsecret/lib/perlsecret.pod
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (3 / 13) |
| Uptime: | 28:23:56 |
| Calls: | 12,107 |
| Calls today: | 7 |
| Files: | 15,006 |
| Messages: | 6,518,231 |