Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Understanding the benefit of Given/When ...

by ikegami (Pope)
on Mar 04, 2010 at 15:14 UTC ( #826720=note: print w/ replies, xml ) Need Help??


in reply to Understanding the benefit of Given/When ...

The when has an automatically built in smart match ~~. Nice ...but how can I take profit from this smart match if the parameter passed into given can only be a scalar?

Maybe, but you can check that scalar against a variety of things, not all of them scalars.

use 5.010; use strict; use warnings; my @x = qw( abc def ); given ("abc") { when (@x) { print "when\n"; } default { print "default\n"; } }

I have no idea what you are asking.


Comment on Re: Understanding the benefit of Given/When ...
Download Code
Smart enough for Smart Match??? (was "Understanding ...Given/When" )
by LanX (Canon) on Mar 04, 2010 at 15:53 UTC
    > I have no idea what you are asking.

    Maybe I'm not smart enough for Smart Match ;)

    It seems that the operands in Smart Match are swapped to what I expected after reading the docs

    Is perlsyn#Switch statements clear???

    Most of the power comes from implicit smart matching: when($foo) is exactly equivalent to when($_ ~~ $foo)
    ($_ was given)

    and

    # $a $b Type of Match Implied Matching Code # ====== ===== ===================== ============= ... # Array Regex array grep grep /$b/, @$a
    see perlsyn#Smart matching in detail

    so when writing

    given (@a) { when (/abc/) {} }

    I expect this to be tested:  @a ~~ /$b/ in the meaning of  grep /$b/, @$a

    Two possibilities:

    a) the docs need a rewrite!

    b) my brain needs a rewrite! ;)

    Cheers Rolf ...still confused...

    UPDATE: Sorry personally I think that ~~ is far too overloaden with functionality to be easily understood!!!

      I don't know why the following doesn't work:
      use 5.010; use strict; use warnings; my @a='abc'; given (@a) { when (/abc/ ) { print "abc\n" } when (1 ) { print "#\n" } when (['abc']) { print "copy\n" } when (\@a ) { print "self\n" } } # copy
      Why is it skipping Array Regex to go to Array Array?
        Have a look at the last code in the OP (I updated it 10 minutes after posting)

        given(@array) and given(\@array) both produce the same ref in $_, but the smartmatch doesn't work as documented.

        While I can understand why to limit given to simple scalars I think the docs should either be updated to reflect this behaviour or it's a bug.

        UPDATE: you should add continues to se which clauses are true

        my @a=('abc',1); given (@a) { when (/abc/ ) { print "abc\n" ;continue} when (1 ) { print "#\n" ;continue} when (['abc',1]) { print "copy\n" ;continue} when (\@a ) { print "self\n" } }

        OUTPUT

        Argument "abc" isn't numeric in smart match at /home/lanx/tmp/ike_give +n.pl line 9. # copy self

        Cheers Rolf

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2015-07-03 22:12 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 (57 votes), past polls