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.";

In reply to Re: Style, style, style by sauoq
in thread Style, style, style by Juerd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.