To keep this problem from happening again, you might want to look at Quantum::Superpositions, specifically the any() and all() functions.
Eh, no, you don't. Quantum::Superpositions is extremely slow.
There's no justification for using Quantum::Superpositions,
just to save typing a pair of parenthesis. Here's a benchmark:
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw /cmpthese/;
use Quantum::Superpositions;
our @data1 = "aa" .. "zz";
our @data2 = "AA" .. "ZZ";
my %queries = (
middle => [$data1 [@data1 / 2], $data2 [@data2 / 2]],
first => [$data1 [0], $data2 [0]],
not => ["feeble", "fnord"],
);
foreach my $type (qw /middle first not/) {
warn "Running '$type' test.\n";
our ($q1, $q2) = @{$queries {$type}};
our ($a_g, $a_q);
cmpthese -1 => {
grep => '$a_g = grep ({$q1 eq $_} @data1) &&
grep ({$q2 eq $_} @data2)',
QS => '$a_q = $q1 eq any (@data1) && $q2 eq any (@data2)'
+,
};
die "Unequal" if $a_g xor $a_q;
print "\n";
}
__END__
Running 'middle' test.
Rate QS grep
QS 24.5/s -- -99%
grep 2376/s 9588% --
Running 'first' test.
Rate QS grep
QS 24.1/s -- -99%
grep 2400/s 9869% --
Running 'not' test.
Rate QS grep
QS 48.6/s -- -99%
grep 5744/s 11719% --
Abigail | [reply] [d/l] |
Q::S is not a small module. While any and all can be implemented with a lot less code, the 'Quantum' part of is what makes Q::S so large. IMHO, using it almost anywhere except as a toy is going to be massive overkill.
----
: () { :|:& };:
Note: All code is untested, unless otherwise stated
| [reply] [d/l] [select] |
Another solution to your direct problem is to put the "grep { } @arr" in parentheses.
Indeed... personally I've never been one to favor the "new" style of only using parens when only absolutely necessary. Precendence rules are tricky (i.e. the && vs. and). By using parens I can at least explicitly tell the compiler what I want (and it helps visually as well, IMHO). I find
open(IN, "foo") || die "...";
much easier to read than
open IN, "foo" or die "...";
Michael
| [reply] [d/l] [select] |