Reputation: 25223
I read now http://www.perlmonks.org/?node_id=861966 But I have a question about internals
What is cause that prototype sub atan2($$); not cause the parser to grab only two arguments:
So
@a = (atan2 1, 2, 3);
does not become
@a = (atan2(1, 2), 3);
What is a cause to not allow perl to do that?
Upvotes: 0
Views: 117
Reputation: 386541
It would be extremely misleading to the reader, and forgetting the parens would fail silently (in your example, at least). I can't think of any reason you'd want that behaviour.
Upvotes: 5
Reputation: 25223
Reading documentation more accuracy. I have found this: "a list operator tends to gobble up all the arguments that follow it" and only after gobbling parser checks count of that arguments to much prototype. It seems the prototyping is like to constraint a number of arguments a function can take.
So we do not allow parser to grab two, three, four argument because this will break main rule:
If we break that rule, it will confuse reader. What does this mean:
say atan2 1, 2, 3, 4;
say atan2( 1, 2 ), 3, 4;
say atan2( 1, 2 , 3), 4;
say atan2( 1, 2 , 3, 4 );
thus force programmer to read source code.
.
What about unary?
DOC: unary operators are treated as functions with one argument.
They are exception because of unary functions are simple and easily noticeable. It is hard to think wrong when you see unary. So it is OK, to grab one argument, but allowing grab two, three etc arguments make source ambiguous
Upvotes: 0
Reputation: 4371
Perl passes lists of parameters to functions, and 1, 2, 3
is a list. An exception to this rule is a monadic function (e.g. sub func($) { ... }
) which is handled in a special way to allow you to construct lists like this in a very natural way:
sub double($) { $_[0] * 2 }
sub triple($) { $_[0] * 3 }
my @a = (1, double 2, triple 3, 4);
Apart from the special cases, the parser handles all function calls the same way, with the entire list following the function name being compared to the prototype, if any. When the type(s) and/or length do not match, the compiler throws a syntax error.
Upvotes: 1