- while (<>) { ... } or while (my $line = <>) { ... }?
Almost always the former. Occasionally the latter, if
using $_ won't make the loop body any simpler and I'm not
dealing with lines as text strings (for instance,
while (my $addr = <>) for a file full of IP
addresses), but that's a rare thing.
- -w or use warnings;?
-w. Most of my scripts target 5.005_03.
- sub CONSTANT () { ... } or use constant CONSTANT => ...;?
I don't usually enforce constness. I'm much more
likely to say $main::CONSTANT = 'foo'; and
just not touch any identifiers in all caps.
- my ($foo, $bar) = @_; or my $foo = shift; my $bar = shift;?
Always assign from @_. Less verbose, easier to change,
and the list looks more like a formal parameter list to my
C-trained eyes.
- for (@array) { ... } or foreach (@array) { ... }?
for. It's less than half the length of
foreach. Huffman encoding, etc, etc.
- print 'foo'; or print('foo');?
Without brackets, unless that induces precedence
lossage.
- 'simple string' or "simple string"?
I'm trying to avoid double-quoted strings unless
there's actually some interpolation going on, but I've
already formed the habit of double-quoting all strings.
We'll see which one wins.
- glob '*' or <*>?
Explicit glob. Angle brackets read from
filehandles.
- readline *FOO or <FOO>?
<FOO>. Again, angle brackets read from
filehandles.
- for (keys %foo) { $_ and $foo{$_} } or while (my ($key, $value) = each %foo) { $key and $value }?
Usually for (keys %foo), but more out of
habit than anything else.
In general, I try for the tersest code that doesn't
require a comment.
--
F
o
x
t
r
o
t
U
n
i
f
o
r
m
Found a typo in this node? /msg me
The hell with paco, vote for Erudil!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|