I have the following script:
use strict; use List::MoreUtils qw/uniq/; use Data::Dumper; my @x = (3,2); my @y = (4,3); print "unique results \n"; print Dumper([uniq(@x,@y)]); print "sorted unique results\n"; print Dumper([sort uniq(@x,@y)]);
Output signal
unique results $VAR1 = [ 3, 2, 4 ]; sorted unique results $VAR1 = [ 2, 3, 3, 4 ];
So it seems that sorting does not work with uniq. I did not understand why.
I ran perl script with -MO = Deparse and got
use List::MoreUtils ('uniq'); use Data::Dumper; use strict 'refs'; my(@x) = (3, 2); my(@y) = (4, 3); print "unique results \n"; print Dumper([uniq(@x, @y)]); print "sorted unique results\n"; print Dumper([(sort uniq @x, @y)]);
My interpretation is that perl decided to remove the parentheses from uniq (@x, @y) and use uniq as a sort function.
Why did the pearl decide to do this?
How can I avoid these and similar traps?
Thanks David
source share