http://qs321.pair.com?node_id=1069315


in reply to Re^2: perldelta unclear on 'given's fate
in thread perldelta unclear on 'given's fate

It basically comes down to a question of why does when(two) seem to work as expected, but when(three) screw up everything below it?

when(EXPR) can mean two different things. It sometimes means if($_ ~~ EXPR), and it sometimes means if(EXPR). The rules by which the interpretation of when is decided are fairly convoluted, and have changed from Perl version to Perl version. In this case they're fairly easily explained.

three is a sub call, so when(three) is interpreted as if(three). The three sub returns 3, so this condition always evaluates to true. Thus the subsequent condition when(4) and the default clause will never get triggered.

two is a simple sub with a prototype of () though. Thus Perl treats it as a constant. It inlines the return value 2 when the when(two) is compiled. So Perl sees the condition as when(2) - that is, not a sub call at all - and interprets it as meaning when($_ ~~ 2).

use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name