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:
- For a cleaner syntax, you can use for my $_ (@ARGV) instead of the while if your loop nevers needs to access/eat multiple array members per iteration.
But don't attempt the parens you normally should use with local, my and our: for my($_) (@ARGV)... will fail.
- To fall through, you can either set $_ suitably or unshift one or more values.
- Note the structural similarity of the lines above to, say, the suggestions in the mass pattern matching thread (->qr//, closures) or dispatch table methods (e.g. removing next, saying last whileloop depending on method-rc, and assuming NOP as default method)
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. |