CONDITION and ACTION;

E.g the code I've used for ages for flexible switch parsing, as the original simple arg handling module went against my tastes:

# maybe replace @ARGV below with my(@A)=@ARGV or similar, as we destro +y the array while($_=$ARGV[0]){ /^(-V|-?-verbose)$/o and do{shift; $verbose=1; ...; next}; /^(-v|-?-negate)$/o and do{shift; $negate=1; ...; next}; ... /^--?$/o and do{shift; ...; last}; # early + loop exit last; }
cu & HTH, Peter -- hints may be untested unless stated otherwise; use with caution & understanding.

Update 1:

Considering Bloodnok's variant below:

If your Perl's sufficiently recent, use my($_) instead of local to protect your caller while also protecting from anything called functions might do.

Also note that I'm using both next and last (loop exit condition) above. If you want a stronger distinction between looping and switching while retaining early loop exiting, you need either a second target label before the while() or set an explicit loop-exit flag variable for testing after the switch block. Simply saying last SWITCH in case of -- when restructuring does introduce a bug and changes semantics of -- to a NOP, with additional problems further down the line for non-option args.

Footnotes to my example above:

Non-famous last words: I simply prefer the above solution over given/when and if-cascades for readability and perceived elegance. But given the ascii art readjustment and byte counts, it's not faster to maintain nor that much shorter.


In reply to Re: Succinct switch statement by jakobi
in thread Succinct switch statement by Anonymous Monk

Title:
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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.