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


in reply to Style, style, style

Any opinions expressed herein are from someone with about a month's exposure to Perl working with it during what little free time I have. Saying that, here goes from my still "new to Perl" point of view... :)

while (<>) { ... } or while (my $line = <>) { ... }?

I generally use the former right now for ease of typing although I'm beginning to have some notions seeping into my head about lexical scoping of $_ which I think I need to learn more about!

-w or use warnings;?

I started with -w for my first few programs just because I'd read it on here, then I changed to use warnings; after some interesting discussions on here and due to the fact the Perl code I'm writing is always designed to be run on a version of Perl that supports it.

sub CONSTANT () { ... } or use constant CONSTANT => ...;?

This would have to be the latter as I didn't really know about the former until now :) Time to research the differences if any! (Hopefully there will be lots of wise words in this thread)

my ($foo, $bar) = @_; or my $foo = shift; my $bar = shift;?

Originally I'd have chosen the first option but since I've been trying to play around with packages and OO programming in Perl I've come to understand that shift isn't as scarey as it first looked so now I'd choose the latter.

for (@array) { ... } or foreach (@array) { ... }?

Easy one! The first because it has fewer keystrokes, I'm lazy, and right now I'm the only one who will ever see my Perl so there is nothing to be gained (AFAIK) in using the extra four letters. (Enlightenment is welcomed if there *is* a difference at all)

print 'foo'; or print('foo');?

Generally without the parentheses unless I have some scoping issue. Speaking of scoping issues that *really* hurt me the first time it happened but now I'm more wary!

'simple string' or "simple string"?

Always single quotes unless I know there is some variable in the string that I want interpolated, just seems safer that way to me right now.

glob '*' or &lt;*&gt;?

At the moment I'd pick glob '*' because my limited experience of glob is currently just from reading the Llama book so I like to be reminded that it's there and I still don't "get" what it does 100%

readline *FOO or &lt;FOO&gt;?

I'd choose the latter here simply because the former is a notation I hadn't seen much of before.

for (keys %foo) { $_ and $foo{$_} } or while (my ($key, $value) = each %foo) { $key and $value }?

Again I'd choose the former but there is still some tingling feeling running up my spine that I'm not entirely conversant with the scoping of $_ and what I can and can't do with it! :)

Nice thread,
Neil