Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by iguanodon (Priest)
on Jan 04, 2014 at 18:37 UTC ( #1069308=note: print w/replies, xml ) Need Help??


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

A whole extra layer of weird indeed. I couldn't guess so I ran it... weirder than I expected. Can you please explain what's going on here?
  • Comment on Re^2: perldelta unclear on 'given's fate

Replies are listed 'Best First'.
Re^3: perldelta unclear on 'given's fate
by tobyink (Canon) on Jan 04, 2014 at 20:38 UTC

    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://1069308]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2020-09-30 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (160 votes). Check out past polls.

    Notices?