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

comment on

( [id://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:

    CODE:

    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/;
    OUTPUT:
    === 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

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



    • Are you posting in the right place? Check out Where do I post X? to know for sure.
    • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
      <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
    • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
    • Want more info? How to link or How to display code and escape characters are good places to start.
  • Log In?
    Username:
    Password:

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others admiring the Monastery: (9)
    As of 2024-04-23 18:29 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found