Update: Several rules now modified to reflect the feedback from wise monks (credited, hopefully, correctly, below the respective rule).
Larry's philosophy that different things should look different is one of the most beautiful and powerful strengths of Perl. We are immediately guided by the $, @, and % as to the nature of the variable (cr: TimToady).

However, for my purpose, it can go even further. With that in mind, I came up with the following 9 10 rules while coding a reasonably complicated project.

Coding strategies are personal by nature, but much can be learned from those who are wiser. Hence, I put forth my rules to get comments from the more learned monks than I --

# 0. Just-in-time variable declaration and initialization.
(cr: merlyn)
# 1. Variables in consistent case (all lower or all upper) # 2. Each dictionary word in the variable name separated # by underscore, unless accepted as one word by usage as # in 'username' or 'logout' # 3. Package variables in all caps, used for things like # config values use vars qw( $FOO $BAR $BAZ $FOO_BAR );
# 4. Lexicals in all lower case
my ($foo, $bar, $baz, @foo_bar);
# 4. Global lexicals in all lower case, and # prefixed with some text to mark them as such. # For example... my ($glob_foo, $glob_bar, $glob_baz, @glob_foo_bar);
# 5. Variables local to a subroutine prefixed by some 
#    consistent prefix such as 'this_' or 'local_'. This way 
#    there never will be any confusion as to whether a 
#    given variable sprang into life in the subroutine
#    of if it exists in the outside world and might possibly 
#    get clobbered locally.
sub foo {
	my ($this_bar) = @_;
	for (@$this_bar) {
		print "yay" if ($_ eq $FOO);
	}
	return $this_bar;
}
# 5. Local lexicals on all lowercase sub foo { my ($bar) = @_; for (@$bar) { print "yay" if ($_ eq $FOO); } return $bar; }
#4 and #5 above flipped (cr: Forsaken)
# 5.5. Use prefixes to scope other logically different # vars. For example, 'cgi_' to prefix vars holding # params delivered via the cgi my $cgi_foo = $cgi->param('foo'); my $cgi_bar = $cgi->param('bar'); # 6. Refs prefixed with the appropriate identifier. I wish # Perl would extend the "different things look different" # to enable easy identification of the kind of data # structure a ref refers to. my $aref_bar = foo(\@foo_bar); # 7. Rules 1-5 apply to rule 6 as well... so, an array ref # inside a sub would be $this_aref_bar, etc.
# 8. Subroutines named in camelCase with the first letter 
#    lower case
sub normalizeThisMonkeyBoy {
}
# 8. modified: I like camelCase, however, the real purpose # here is to visually distinguish a sub from a var. Choose # your method # 9. Subroutines not exported from a package prefixed with # an underscore package Foo; @EXPORT = qw( startProgram watchOut goHome ); sub startProgram { _wakeUp(); } sub watchOut { _keepEyeOpen(); } sub goHome { _nightyNight(); } sub _wakeUp {} sub _keepEyeOpen {} sub _nightyNight {} # 9.5. Never export anything by default. @EXPORT_OK
(cr: TilRMan)
# 10. Always pass named vars to subs doThis('with' => $that, 'and' = $something_else);
--

when small people start casting long shadows, it is time to go to bed

Janitored by holli - moved to Meditations


In reply to coding rules by punkish

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.