Question

I am currently studying Perl programming and am running into statements like 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?

Was it helpful?

Solution

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).

OTHER TIPS

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top