{"id":2311,"date":"2013-06-10T20:12:07","date_gmt":"2013-06-11T03:12:07","guid":{"rendered":"http:\/\/www.sheer.us\/weblogs\/?p=2311"},"modified":"2013-06-10T20:12:49","modified_gmt":"2013-06-11T03:12:49","slug":"perl-arg-parser","status":"publish","type":"post","link":"https:\/\/www.sheer.us\/weblogs\/programming\/perl-arg-parser","title":{"rendered":"Perl arg parser"},"content":{"rendered":"<p><PRE><br \/>\nI use this a lot when writing a simple perl script that I want to take args like &#8211;flag and &#8211;database=this and &#8211;comment=&#8221;This is a comment with spaces&#8221;<\/p>\n<p>while($arg = shift) { <\/p>\n<p>        if(($s1,$s2) = $arg =~ \/&#8211;(.*)=(.*)\/) {<br \/>\n                $l1 = lc $s1;<br \/>\n                $s2 = $a if(($a) = $s2 =~ \/^\\&#8221;(.*)\\&#8221;$\/);<br \/>\n                $arg{$l1} = $s2;<br \/>\n        } elsif(($s1) = $arg =~ \/&#8211;(.*)\/) {<br \/>\n                $l1 = lc $s1;<br \/>\n                $arg{$l1} = 1;<br \/>\n        }<br \/>\n}<br \/>\n<\/PRE><\/p>\n<p>Stick it in the top of the script, and you can then just use <\/p>\n<p>if($arg{&#8216;flag&#8217;}) {<\/p>\n<p>}<\/p>\n<p>$comment = $arg{&#8216;comment&#8217;};<\/p>\n<p>and so on and so forth.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I use this a lot when writing a simple perl script that I want to take args like &#8211;flag and &#8211;database=this and &#8211;comment=&#8221;This is a comment with spaces&#8221; while($arg = shift) { if(($s1,$s2) = $arg =~ \/&#8211;(.*)=(.*)\/) { $l1 = lc $s1; $s2 = $a if(($a) = $s2 =~ \/^\\&#8221;(.*)\\&#8221;$\/); $arg{$l1} = $s2; } elsif(($s1) [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[],"_links":{"self":[{"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/posts\/2311"}],"collection":[{"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/comments?post=2311"}],"version-history":[{"count":2,"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/posts\/2311\/revisions"}],"predecessor-version":[{"id":2313,"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/posts\/2311\/revisions\/2313"}],"wp:attachment":[{"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/media?parent=2311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/categories?post=2311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/tags?post=2311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}