dpuu has asked for the wisdom of the Perl Monks concerning the following question:
But I came across another example today:
It appears that the sort function is seeing "uniq" as the code block that defines sort critera. OK, I thought, lets make it "look like a function call":sub uniq { local $_; my %seen; return grep { !$seen{$_}++ } @_; } print uniq 0,0,1,2; # good: prints 0,1,2 print sort uniq 0,0,1,2; # bad: prints 0,0,1,2
But nope, this still prints "0,0,1,2". To make it work I need to resort to:print sort uniq(0,0,1,2);
or some similar intusive builtin.print sort grep {1} uniq 0,0,1,2;
This example probably just scratches the surface of some parser logic that I haven't correctly groked. Why doesn't the "if it looks like a function" rule work in this case? If a builtin like "grep" (or "map") isn't treated as a sort-criteria code block, then how do I define my own subroutines that similarly are not sort-critera?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: When is a function call not a function call
by Roy Johnson (Monsignor) on Mar 29, 2006 at 21:55 UTC | |
|
Re: When is a function call not a function call
by duff (Parson) on Mar 30, 2006 at 04:15 UTC | |
by dpuu (Chaplain) on Mar 30, 2006 at 04:52 UTC | |
by duff (Parson) on Mar 30, 2006 at 05:55 UTC |