note
sauoq
<blockquote><code>while (<>) { ... } or while (my $line = <>) { ... }</code>?</blockquote>
<p>It depends... in general, the former for short scripts the latter for larger projects.</p>
<blockquote><code>-w or use warnings;</code>?</blockquote>
<p>It depends... usually -w because I've got to make things work on 5.005 (and .004 for that matter) much of the time. Again, if it is a larger project and I can control the version of perl, I prefer <em>use warnings</em> especially since they'll provide better granularity if needed.</p>
<blockquote><code>sub CONSTANT () { ... } or use constant CONSTANT => ...;</code>?</blockquote>
<p><code>use constant CONSTANT => ...;</code><font size=-1>but I confess to secretly wishing that we all still had to use references to literals... ;-)</font></p>
<blockquote><code>my ($foo, $bar) = @_; or my $foo = shift; my $bar = shift;</code>?</blockquote>
<p>It depends... I use the list assignment by default but there are exceptions. I usually shift $self as [dws] does. Sometimes I don't need all the args, in which case I may shift only when I need the next one. <em>Rarely</em>, but on occasion, I even find a reason to use <code>$_[0]</code> and friends directly.</p>
<blockquote><code>for (@array) { ... } or foreach (@array) { ... }</code>?</blockquote>
<p>I use <code>for</code> consistently. Whatever is inside the parens can speak for itself. I used to use <code>foreach</code> but I've had one time too often when I've changed <code>(;;)</code> to <code>(@a)</code> and forgot to change the keyword. I've done the reverse too and its kind of embarrassing when someone finds a <code>foreach(;;)</code> in your code. Sure it works but there's no real excuse for it. I found it was easier to just use <code>for</code> all of the time.</p>
<blockquote><code>print 'foo'; or print('foo');? </code>?</blockquote>
<p>No parens unless necessary. <code>print( ($a + $b) * $c, "\n" );</code> is a case where it might be necessary. I use parens with printf() though.</p>
<blockquote><code>'simple string'; or "simple string"</code>?</blockquote>
<p>Single quotes unless it's in a print in which case I usually use doubles because, when I don't, I always end up needing to change them. If I need double quotes elsewhere, I tend to prefer qq().</p>
<blockquote><code>glob '*' or <*></code>?</blockquote>
<p>Neither recently. I have used <code><*></code> but I'd rather not remember those days. I'm with [dws] on this one too. <code>opendir()</code> et al is "The Right Way™".</p>
<blockquote><code>readline *FOO or <FOO></code>?</blockquote>
<p>It depends... Ha! Just kidding. I always use: <code><FOO></code> ... Perl should look like Perl.</p>
<blockquote><code>for (keys %foo) { $_ and $foo{$_} } or while (my ($key, $value) = each %foo) { $key and $value }</code>?</blockquote>
<p>It depends... I don't use <code>each</code> very often but there have been and will be exceptions.</p>
<pre>
-sauoq
"My two cents aren't worth a dime.";
</pre>
195957
195957