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


in reply to Style, style, style

1. while (<>) { ... } or while (my $line = <>) { ... }? I use both, depending on whim and my assemement of readability. 2. -w or use warnings;? -w, but only because of muscle memory. 3. sub CONSTANT () { ... } or use constant CONSTANT => ...;? use constant CONSTANT => ...; 4. my ($foo, $bar) = @_; or my $foo = shift; my $bar = shift;? I always shift $self. Whether or not to shift the other args or use array assignment kind of depends. Whim again, I'm afraid. 5. for (@array) { ... } or foreach (@array) { ... }? foreach. My undergrad degree was in Math. 6. print 'foo'; or print('foo');? No parens unless needed for precedence. 7. 'simple string'; or "simple string"? Double quotes. 8. glob '*' or <*>? opendir(); readdir(); closedir(); 9. readline *FOO or <FOO>? <FOO> 10. for (keys %foo) { $_ and $foo{$_} } or while (my ($key, $value) = each %foo) { $key and $value }? Depends on whether or not %foo is tied to a DBM. I use the 'each' form on DBMs, having been burned once by pulling all keys into memory on a big DBM.

Replies are listed 'Best First'.
Re: Re: Style, style, style
by Anonymous Monk on Sep 08, 2002 at 03:51 UTC
    4. my ($foo, $bar) = @_; or my $foo = shift; my $bar = shift;?

    What about my ($foo, $bar) = (shift, shift);?
      What about my ($foo, $bar) = (shift, shift);?
      Yuck.

        I regularly see one of these in a co-worker's code:

        sub blah { my ( $foo, $bar, $baz ) = ( shift, shift ); }

        Perhaps not too bad in a short list of shifts. But terrible when invoked as:

        shazam ( 0, 6, 7, $index, $arrayref, $hashref ); ... sub shazam { my ( $foo, $bar, $baz, $bup, $quz, $pig, $cow ) = (shift, shift, shi +ft, shift, shift, shift); }
        Quick, was $cow passed in via @_ or simply instantiated?

        update: added another egregious example, shazam

        blyman
        setenv EXINIT 'set noai ts=2'

      maybe in Obfuscation, not in production code.

      - cybear