Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Syntactic Confectionery Delight

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Honorable Monks,

After this discussion -> Re^6: while(){}continue{}; Useful? (Consistent!), I'm trying to understand the extended functionality of the new Given/When compared to the old workaround with For/Next.

As far as I understand,

  • In given/when I have to write a continue (statement not block!) where I omit the next statement. I need to "invert" the fall through.
  • In given/when I lose the continue-block which is always executed.
  • 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 I missed something, please see the example code to understand what I mean:


    use feature "switch"; $,=$\="\t"; @test=qw/abc def abcdef nnn/; print "\n\n=== Given/When"; for (@test){ print "\nGIVEN($_):"; given($_) { when (/abc/) { print "abc";continue } when (/def/) { print "def" } when (/xyz/) { print "xyz" } default { print "default" } } } print "\n\n=== For"; for (@test){ for ($_){ print "\nFOR($_):"; if (/abc/) { print "abc"} if (/def/) { print "def" ;next} if (/xyz/) { print "xyz" ;next} print "default"; } } print "\n\n=== For/Continue"; # "simplifying" the former with post-ifs and && # using continue-block my @res; for (@test){ push (@res, "abc") if (/abc/); push (@res, "def") && next if (/def/); push (@res, "xyz") && next if (/xyz/); push (@res, "default"); } continue { print "\nFOR/CONT($_):",@res; @res=(); } print "\n\n === When smartmatch\n"; print "\n= GIVEN(\@test):\n"; given(@test) { print "whats tested is:",$_; when (/abc/) { print "abc";continue } when (/def/) { print "def" } when (/xyz/) { print "xyz" } default { print "default" } } print "\n= GIVEN(\\\@test):\n"; # does when act differently when a arr_reff is passed? given(\@test) { print "whats tested is:",$_; when (/abc/) { print "abc";continue } when (/def/) { print "def" } when (/xyz/) { print "xyz" } default { print "default" } } # whats the point of implicit smartmatch if only scalars can be testet +??? smartmatchingan array is different print "\nBUT: def ~~ \@test!" if @test ~~ /def/;
    === Given/When GIVEN(abc): abc default GIVEN(def): def GIVEN(abcdef): abc def GIVEN(nnn): default === For FOR(abc): abc default FOR(def): def FOR(abcdef): abc def FOR(nnn): default === For/Continue FOR/CONT(abc): abc default FOR/CONT(def): def FOR/CONT(abcdef): abc def FOR/CONT(nnn): default === When smartmatch = GIVEN(@test): whats tested is: ARRAY(0x8a08a38) default = GIVEN(\@test): whats tested is: ARRAY(0x8a08a38) default BUT: def ~~ @test!

    Cheers Rolf

    UPDATE: extended given(Array) and given(Array_ref)

    Since given accepts arrays and automatically passes the ref without when reacting accordingly, I suppose it's a BUG!

    In reply to Understanding the benefit of Given/When ... by LanX

    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (10)
    As of 2014-04-16 10:36 GMT
    Find Nodes?
      Voting Booth?

      April first is:

      Results (423 votes), past polls