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

Replies are listed 'Best First'.
Re: Re: Style, style, style
by Juerd (Abbot) on Sep 08, 2002 at 19:18 UTC

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

    I know. Style and equality have not much to do with eachother. While it is true that all of the pairs display two approaches to the same problem, "use strict; or no strict;?" could easily have been one of the questions.

    Take for example the list assignment vs shifts question. They are not equivalent. Of course they are not - I hope everyone reading this thread realises what the differences are. But it *is* a question of style when @_ is not used elsewhere.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.