perlquestion
dpuu
Everyone known why <code>print (1+1)*2;</code> doesn't work as newbies might expect. But there's a simple rule to understand it: if it looks like a funtion call then it is a function call.
<p>
But I came across another example today:
<code>
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
</code>
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":
<code>
print sort uniq(0,0,1,2);
</code>
But nope, this still prints "0,0,1,2". To make it work I need to resort to:
<code>
print sort grep {1} uniq 0,0,1,2;
</code>
or some similar intusive builtin.
<p>
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?
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-183551">
--Dave<br>Opinions my own; statements of fact may be in error.
</div></div>