Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Style, style, style

by FoxtrotUniform (Prior)
on Sep 08, 2002 at 20:48 UTC ( #196093=note: print w/replies, xml ) Need Help??

in reply to Style, style, style

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

    Almost always the former. Occasionally the latter, if using $_ won't make the loop body any simpler and I'm not dealing with lines as text strings (for instance, while (my $addr = <>) for a file full of IP addresses), but that's a rare thing.

  • -w or use warnings;?

    -w. Most of my scripts target 5.005_03.

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

    I don't usually enforce constness. I'm much more likely to say $main::CONSTANT = 'foo'; and just not touch any identifiers in all caps.

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

    Always assign from @_. Less verbose, easier to change, and the list looks more like a formal parameter list to my C-trained eyes.

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

    for. It's less than half the length of foreach. Huffman encoding, etc, etc.

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

    Without brackets, unless that induces precedence lossage.

  • 'simple string' or "simple string"?

    I'm trying to avoid double-quoted strings unless there's actually some interpolation going on, but I've already formed the habit of double-quoting all strings. We'll see which one wins.

  • glob '*' or <*>?

    Explicit glob. Angle brackets read from filehandles.

  • readline *FOO or <FOO>?

    <FOO>. Again, angle brackets read from filehandles.

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

    Usually for (keys %foo), but more out of habit than anything else.

In general, I try for the tersest code that doesn't require a comment.

F o x t r o t U n i f o r m
Found a typo in this node? /msg me
The hell with paco, vote for Erudil!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2021-06-22 15:08 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (106 votes). Check out past polls.