{"id":281,"date":"2005-05-31T20:53:00","date_gmt":"2005-05-31T20:53:00","guid":{"rendered":"http:\/\/jclark.org\/weblog\/Programming\/Perl\/regextest.html"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-30T04:00:00","slug":"regextest","status":"publish","type":"post","link":"https:\/\/jclark.org\/weblog\/2005\/05\/31\/regextest\/","title":{"rendered":"Stupid Perl Debugger Tricks"},"content":{"rendered":"<p>I&#8217;ve been playing with Python alot lately in my spare time, but I still use mostly Perl at work.  One of the handy things about Python is the interactive mode; I like it so much I even [cloned it for Perl][1] some time ago.  Even without my Perlthon script, you can get a quick approximation in perl using the perl debugger and a command-line script:<\/p>\n<pre><code>perl -de1<\/code><\/pre>\n<p>(That&#8217;s a one, not an el.)  The above will invoke perl with the debugger (<code>-d<\/code>), debugging a very simple script (<code>-e1<\/code>, which is to say <code>-e &#039;1;&#039;<\/code>).  Once the debugger starts, you can just type perl statements, and can use `x <\/p>\n` to inspect values.  \n\nWhichever way you play with interactive Perl, testing [regular expressions][2] can be a pain.  It&#8217;s not too bad under Python, given Python&#8217;s use of `match` objects:\n\n    import re\n    re.search(&#8216;regex&#8217;, &#8216;string&#8217;).group(0)\n\nThe second line runs the regex against the string, and prints the entire match.  If your regex doesn&#8217;t match at all, you get an error, but that&#8217;s fine (and self explanatory).  If your regex doesn&#8217;t perform as expected, repeated attempts make it easy to triangulate.  If you have Python&#8217;s readline module installed, you can just hit UpArrow after the test, tweak the regex, lather, rinse, repeat.  I wanted the same flexibility with interactive Perl; it turns out to be trivial:\n\n    # perl debugger version\n    x (&#8216;string&#8217; =~ \/regex\/, $&amp;)[-1]\n\n    # perlthon version\n    (&#8216;string&#8217; =~ \/regex\/, $&amp;)[-1];\n\nThe regex match operation will return a list of matched groups, which can be handy at times.  For testing a complicated regex, I often just want to see the whole match to be sure I&#8217;m getting what I expect.  The array notation accomplishes this nicely.\n\n[1]: http:\/\/jclark.org\/weblog\/Programming\/Perl\/Perlthon\/ &#8220;Perlthon Home Page&#8221;\n[2]: http:\/\/en.wikipedia.org\/wiki\/Regular_expression     ","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been playing with Python alot lately in my spare time, but I still use mostly Perl at work. One of the handy things about Python is the interactive mode; I like it so much I even [cloned it for Perl][1] some time ago. Even without my Perlthon script, you can get a quick approximation [&hellip;]<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-281","post","type-post","status-publish","format-standard","hentry","category-perl"],"_links":{"self":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/posts\/281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/comments?post=281"}],"version-history":[{"count":0,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/posts\/281\/revisions"}],"wp:attachment":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/media?parent=281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/categories?post=281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/tags?post=281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}