Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Strange given/when deparsing

by KSURi (Monk)
on Jun 09, 2009 at 10:58 UTC ( #769844=perlquestion: print w/ replies, xml ) Need Help??
KSURi has asked for the wisdom of the Perl Monks concerning the following question:

Hello, monks!

There is a simple script:
use Modern::Perl; my $x = 2.3; given($x) { when(int) { say 'match' } # int($_) ~~ $x default { say 'no match' } }
It prints 'no match' as expected.

There is also another script with a little difference:
use Modern::Perl; my $x = 2.3; given($x) { when($_) { say 'match' } # $_ ~~ $x default { say 'no match' } }
Prints 'match' as expected.

The strangeness is that B::Deparse produces the same output for both scripts:
use Modern::Perl; use warnings; use strict 'refs'; BEGIN { $^H{'feature_say'} = q(1); $^H{'feature_state'} = q(1); $^H{'feature_switch'} = q(1); } my $x = 2.3; given ($x) { when ($_) { say 'match'; } default { say 'no match'; } }
Why is this so? Is this a bug of B::Deparse?

Comment on Strange given/when deparsing
Select or Download Code
Replies are listed 'Best First'.
Re: Strange given/when deparsing
by ikegami (Pope) on Jun 09, 2009 at 15:53 UTC

    Is this a bug of B::Deparse?

    Yes, although it *might* have already been fixed in forthcoming 5.10.1.

    You can see the difference with the more precise (but verbose) B::Concise:

    g <2> smartmatch sK*/2 ->h d <0> padsv[$_:46,51] s ->e f <1> int[t4] sK/1 ->g e <0> padsv[$_:46,51] s ->f
    f <2> smartmatch sK*/2 ->g d <0> padsv[$_:46,51] s ->e e <0> padsv[$_:46,51] s ->f

    when(int) { say 'match' } # int($_) ~~ $x

    You have the order backwards. It's $x ~~ int($_). (Well, $_ ~~ int($_) really.)

      Isn't ~~ commutative?
        It might have been. It might be. It won't be.

        Update: Is the following clearer?

        It might have been commutative in the past. It might be commutative now. It won't be commutative in the future.

Re: Strange when/given deparsing
by akho (Hermit) on Jun 09, 2009 at 11:06 UTC
    Very probably. Only perl can parse Perl, after all; and given/when is quite new.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (18)
As of 2015-07-07 17:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (92 votes), past polls