Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Style, style, style

by sauoq (Abbot)
on Sep 08, 2002 at 04:17 UTC ( #195979=note: print w/replies, xml ) Need Help??


in reply to Style, style, style

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

It depends... in general, the former for short scripts the latter for larger projects.

-w or use warnings;?

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 use warnings especially since they'll provide better granularity if needed.

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

use constant CONSTANT => ...;but I confess to secretly wishing that we all still had to use references to literals... ;-)

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

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. Rarely, but on occasion, I even find a reason to use $_[0] and friends directly.

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

I use for consistently. Whatever is inside the parens can speak for itself. I used to use foreach but I've had one time too often when I've changed (;;) to (@a) and forgot to change the keyword. I've done the reverse too and its kind of embarrassing when someone finds a foreach(;;) in your code. Sure it works but there's no real excuse for it. I found it was easier to just use for all of the time.

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

No parens unless necessary. print( ($a + $b) * $c, "\n" ); is a case where it might be necessary. I use parens with printf() though.

'simple string'; or "simple string"?

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().

glob '*' or <*>?

Neither recently. I have used <*> but I'd rather not remember those days. I'm with dws on this one too. opendir() et al is "The Right Way™".

readline *FOO or <FOO>?

It depends... Ha! Just kidding. I always use: <FOO> ... Perl should look like Perl.

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

It depends... I don't use each very often but there have been and will be exceptions.

-sauoq
"My two cents aren't worth a dime.";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://195979]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2021-06-22 13:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (104 votes). Check out past polls.

    Notices?