Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Smart enough for Smart Match??? (was "Understanding ...Given/When" )

by ikegami (Patriarch)
on Mar 04, 2010 at 17:45 UTC ( [id://826761]=note: print w/replies, xml ) Need Help??


in reply to Smart enough for Smart Match??? (was "Understanding ...Given/When" )
in thread Understanding the benefit of Given/When ...

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?

Replies are listed 'Best First'.
Re^2: Smart enough for Smart Match??? (was "Understanding ...Given/When" )
by LanX (Saint) on Mar 04, 2010 at 17:53 UTC
    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

      given(@array) and given(\@array) both produce the same ref in $_

      That's fine. ARRAY means array or array ref.

      I didn't get a chance to read the thread JavaFan started, but I gotta go for a bit now.

        extending your code shows that there is a bug when matching ARRAY ~~ REGEX, do you agree?

        use 5.010; use strict; use warnings; my @a=('abc',1); given (@a) { when (/abc/ ) { print '/abc/'."\n" ;continue} when ("abc" ) { print "abc\n" ;continue} #when (1 ) { print "#\n" ;continue} when (['abc',1]) { print "copy\n" ;continue} when (\@a ) { print "self\n" } } print "as it should be: ",@a~~/abc/;

        OUTPUT

        abc copy self as it should be 1

        Cheers Rolf

        PS: I have to go, too ...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-03-28 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found