Everyone known why
print (1+1)*2; 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.
But I came across another example today:
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
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":
print sort uniq(0,0,1,2);
But nope, this still prints "0,0,1,2". To make it work I need to resort to:
print sort grep {1} uniq 0,0,1,2;
or some similar intusive builtin.
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?
--Dave
Opinions my own; statements of fact may be in error.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.