<?xml version="1.0" encoding="windows-1252"?>
<node id="77230" title="Golf Do-s and Don't-s" created="2001-05-02 03:51:46" updated="2005-08-13 14:01:08">
<type id="120">
perlmeditation</type>
<author id="1936">
japhy</author>
<data>
<field name="doctext">
With the onslaught of golf meditations recently (of which I shall soon be posting one -- fear not!), I'm posting a set of reminders that you may be wont to take heed of, ere you post again!

&lt;ul&gt;

&lt;li&gt; if you use a regex, be sure &lt;tt&gt;.&lt;/tt&gt; can match a newline, by using the &lt;tt&gt;/s&lt;/tt&gt; modifier to the regex

&lt;li&gt; &lt;tt&gt;pop&lt;/tt&gt; is two characters less than &lt;tt&gt;shift&lt;/tt&gt;

&lt;li&gt; assign to &lt;tt&gt;@_&lt;/tt&gt; and &lt;tt&gt;$_&lt;/tt&gt; if you can -- it saves a &lt;tt&gt;my&lt;/tt&gt; declaration (if you're stingy), and allows operations like regexes and &lt;tt&gt;pop&lt;/tt&gt; to work without an argument

&lt;li&gt; screw &lt;tt&gt;$1&lt;/tt&gt; and the like, and use &lt;tt&gt;$&amp;&lt;/tt&gt; and its friends instead

&lt;li&gt; &lt;tt&gt;map()&lt;/tt&gt; is probably a useful tool to master for golf

&lt;li&gt; leave out semicolons wherever possible

&lt;li&gt; don't explicitly &lt;tt&gt;return&lt;/tt&gt; unless you must

&lt;li&gt; doing &lt;tt&gt;.$/&lt;/tt&gt; is shorter than &lt;tt&gt;."\n"&lt;/tt&gt; -- same for &lt;tt&gt;.$"&lt;/tt&gt; vs. &lt;tt&gt;." "&lt;/tt&gt;

&lt;li&gt; &lt;tt&gt;{...;redo}&lt;/tt&gt; is shorter than &lt;tt&gt;while(1){...}&lt;/tt&gt;

&lt;li&gt; &lt;tt&gt;{...;$x&amp;&amp;redo}&lt;/tt&gt; is shorter than &lt;tt&gt;while($x){...}&lt;/tt&gt;

&lt;li&gt; use &lt;tt&gt;||&lt;/tt&gt; and &lt;tt&gt;&amp;&amp;&lt;/tt&gt;

&lt;/ul&gt;

&lt;br&gt;&lt;br&gt;
&lt;tt&gt;&lt;font color="#0000ff"&gt;japhy&lt;/font&gt;&lt;/tt&gt; -- 
&lt;a href="/index.pl?node=japhy&amp;lastnode_id=1072"&gt;Perl and Regex Hacker&lt;/a&gt;</field>
</data>
</node>
