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

Re^2: given/when one case prefixes another

by moritz (Cardinal)
on Apr 26, 2011 at 09:03 UTC ( [id://901309]=note: print w/replies, xml ) Need Help??


in reply to Re: given/when one case prefixes another
in thread given/when one case prefixes another

It seems like perfectly fine code to me.

I guess you are wondering if the code directly in "given" (without a "when" clause) is "proper". The answer is yes. That's one feature that distinguishes given/when from the switch statement in C.

But do consider that such code could become hard to read if the whole given/when doesn't fit onto one or two screen pages anymore - you shouldn't be doing very much in the when clauses (calling functions or methods should be fine).

  • Comment on Re^2: given/when one case prefixes another

Replies are listed 'Best First'.
Re^3: given/when one case prefixes another
by AnomalousMonk (Archbishop) on Apr 26, 2011 at 15:46 UTC

    I suppose I was uneasy about the possibility of the  when (/[ab]/) { ... } tail clause 'getting lost', dependent as it is on a  continue statement in other, possibly distant, clauses, and on the relative positions of those clauses (also a dependency of Neighbour's approach). Taking apl's suggestion, I am more comfortable with something like the following, which encapsulates the tail of each 'tailed' clause within the clause itself:

    >perl -wMstrict -lE "for my $s qw(a b c d) { given ($s) { my $tail_a_and_b = sub { say qq{ after a or b, was '$_' } }; when ('a') { say 'a'; $tail_a_and_b->(); } when ('b') { say 'b'; $tail_a_and_b->(); } when ('d') { say 'd'; } default { say qq{other: '$_'}; } } } " a after a or b, was 'a' b after a or b, was 'b' other: 'c' d

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-26 04:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found