How does comma separated operations work in perl?
I am currently studying Perl programming and am running into statements li开发者_运维问答ke this:
return bless { }, $type;
I know what return bless { };
would do, and I also know what return $type;
would do, but how does separating them by a comma affect the statement, and does it work the same way for all unary operators?
bless is not an unary operator, so what happens is that $type
gets passed to bless
(where it is used as the name of the class to bless the hashref into).
The only thing special about return is that the expression on its right-hand might be evaluated in list, scalar, or void context depending on the context the subroutine was called in.
The comma operator is not interpreted any differently in a return
statement than it would be anywhere else (except that you can't tell whether it's in list or scalar context by looking at the return
statement).
Predefined Perl functions can be called with or without parentheses. Most of people coming from other languages confuse these functions for keywords/operators.
bless, undef, push, pop, shift, unshift, print, split, join, etc. are all functions.
Hence, these two are equivalent:
return bless { }, $type;
return bless({ }, $type);
But these two are not:
print 2 * 3 + 2; # prints 8
print(2 * 3) + 2; # prints 6 (with a warning if warnings pragma is on)
精彩评论