http://www.perlmonks.org?node_id=196075


in reply to Style, style, style

Some of your questions compare inequivalent statements, mostly in regard of global variables.

p>while (<>) { ... } or while (my $line = <>) { ... }?
They do different things. I tend to take advantage of $_ and avoid named temporaries as much as I can, so I oftener code the former.

-w or use warnings;?
They do different things. $^W is global, warnings.pm is lexically scoped. The backward compatibility issue matters. Situational, for me.

sub CONSTANT () { ... } or use constant CONSTANT => ...;?
I use constant ... when I'm comfortable with the all-caps convention. For something whose name doesn't follow the convention, or that will be explicitly called like a sub, I define the sub.

my ($foo, $bar) = @_; or my $foo = shift; my $bar = shift;?
The two are very different. The former leaves @_ unaltered. I shift when I want to remove items so that @_ can be treated as an array of similar things, as often happens in OO Perl.

for (@array) { ... } or foreach (@array) { ... }?
I use for with the pronoun, foreach if named. Blind acceptance of the perldoc recommendations and examples.

print 'foo'; or print('foo');?
A list is a list. I prefer the former. I tend to use parens as little as I can. That way, when I see them I know I've defeated operator precedence.

'simple string' or "simple string"?
Single quotes. Why invoke interpolation for nothing?

glob '*' or <*>?
I've come to prefer glob. No tricky rules about how the argument is interpreted, no confusion with reading from an open handle. I do like the compactness of diamond notation.

readline *FOO or <FOO>?
See previous question, I prefer <FOO> for its compactness.

for (keys %foo) { $_ and $foo{$_} } or while (my ($key, $value) = each %foo) { $key and $value }?
Like the while question above, I like the pronoun when I can use it. The latter has the advantage when I need to protect $_, or $value is often used in the block.

After Compline,
Zaxo