3
mod_perl Coding Style Guide
7
This document explains the coding style used in the core mod_perl
8
development and which should be followed by all core developers.
10
=head1 Coding Style Guide
12
We try hard to code mod_perl using an identical style. Because
13
everyone in the team should be able to read and understand the code as
14
quickly and easily as possible. Some will have to adjust their habits
15
for the benefit of all.
21
mod_perl's C code follows the Apache style guide:
22
http://dev.apache.org/styleguide.html
26
C code inside XS modules also follows the Apache style guide.
30
mod_perl's Perl code also follows the Apache style guide, in terms of
31
indentation, braces, etc. Style issues not covered by Apache style of
32
guide should be looked up in the I<perlstyle> manpage.
36
Here are the rough guidelines with more stress on the Perl coding
41
=item Indentation and Tabs
43
Do use 4 characters indentation.
47
Here is how to setup your editor to do the right thing:
51
=item * x?emacs: cperl-mode
56
'(cperl-indent-level 4)
57
'(cperl-continued-statement-offset 4)
58
'(cperl-tab-always-indent t)
59
'(indent-tabs-mode nil)
66
set expandtab " replaces any tab keypress with the appropriate number of spaces
67
set tabstop=4 " sets tabs to 4 spaces
74
Do use a style similar to K&R style, not the same. The following
75
example is the best guide:
80
my($self, $cond, $baz, $taz) = @_;
86
$self->foo("one", 2, "...");
96
my ($self,$bar,$baz,$taz)=@_;
100
} else { $self->foo ("one",2,"..."); }
104
=item Lists and Arrays
106
Whenever you create a list or an array, always add a comma after the
107
last item. The reason for doing this is that it's highly probable that
108
new items will be appended to the end of the list in the future. If
109
the comma is missing and this isn't noticed, there will be an error.
128
=item Last Statement in the Block
130
The same goes for C<;> in the last statement of the block. Almost
131
always add it even if it's not required, so when you add a new
132
statement you don't have to remember to add C<;> on a previous line.
154
=head1 Function and Variable Prefixes Convention
160
The prefix for mod_perl C API functions.
164
The prefix for mod_perl C macros.
168
The prefix for mod_perl XS utility functions.
172
The prefix for mod_perl I<generated> XS utility functions.
176
The prefix for mod_perl XSUBs with an XS() prototype.
186
=head1 Coding Guidelines
188
The following are the Perl coding guidelines:
191
=head2 Global Variables
195
=item avoid globals in general
197
=item avoid $&, $', $`
199
See C<Devel::SawAmpersand>'s I<README> that explains the evilness.
200
Under mod_perl everybody suffers when one is seen anywhere since the
201
interpreter is never shutdown.
210
=item Exporting/Importing
212
Avoid too much exporting/importing (glob aliases eat up memory)
214
When you do wish to import from a module try to use an explicit list or
215
tag whenever possible, e.g.:
217
use POSIX qw(strftime);
219
When you do not wish to import from a module, always use an empty list
220
to avoid any import, e.g.:
224
(explain how to use Apache::Status to find imported/exported
234
=item indirect vs direct method calls
236
Avoid indirect method calls, e.g.
255
=item Avoid inheriting from certain modules
258
To avoid inheriting B<AutoLoader::AUTOLOAD>
262
*import = \&Exporter::import;
266
@MyClass::ISA = qw(Exporter);
280
stay away from C<main::> to avoid namespace clashes
284
=head2 Use of $_ in loops
286
Avoid using C<$_> in loops unless it's a short loop and you don't call
287
any subs from within the loop. If the loop started as short and then
288
started to grow make sure to remove the use of C<$_>:
292
for my $idx (1..100) {
293
....more than few lines...
301
....more than a few statements...
306
Because foo() might change C<$_> if foo()'s author didn't localize C<$_>.
311
.... a few statements with no subs called
312
# do something with $_
322
Maintainer is the person(s) you should contact with updates,
323
corrections and patches.
325
Stas Bekman E<lt>stas *at* stason.orgE<gt>
333
Doug MacEachernE<lt>dougm (at) covalent.netE<gt>
337
Stas Bekman E<lt>stas (at) stason.orgE<gt>
341
Only the major authors are listed above. For contributors see the