Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: perldelta unclear on 'given's fate

by tobyink (Canon)
on Jan 04, 2014 at 20:38 UTC ( #1069315=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1069315]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2020-10-24 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (242 votes). Check out past polls.

    Notices?