This hunk of code will accept -m0 -m=0 and -m 0 as equivalents. I'm working on my figlet in perl implementation and decided I should accept standard figlet style switches which are -m0 and -m 0. I was previously using -s and hence only accepted -m=0. I felt this warranted backwards compatibility.

UPDATE: Just thought I'd add that while it's not so obvious this does not support switch clustering, though upon inspection the reason is obvious.

my %opts; #Arguments that take no options $opts{$_} = undef for qw(A D E L R X c demo help l r x); #Arguments that must take options # (may is easily doable with a lookahead in the do but # then you cannot accept negative numbers as values # without them being adjoined to the switch) # do{ if( $ARGV[$i+1] =~ /^-/ ){ 1; }else{ $i--; shift;} } $opts{$_} = "0 but true" for qw(I d f m w); for(my $i=0; $i <= scalar @ARGV; $i++){ #End arguments at -- shift && last if $ARGV[$i] eq '--'; #UPDATE: The sort gives priority to longer switches foreach my $key ( sort { length($b)-length($a) } keys %opts){ if( $ARGV[$i] =~ /^-$key=?(.*)/ ){ #Get rid of the switch, keep $i in sync shift; $i--; #Set the value (if adjoined or shift else 1) $opts{$key} = defined($1) && $1 ne '' ? $1 : defined($opts{$key}) ? do{$i--; shift} : 1; #Short circuit last; } } } #Clear unseen switches that take values to not pass along false positi +ves $_ eq '0 but true' && ($_ = undef) for values %opts;

In reply to Triple syntax getopt by belg4mit

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.