1 .\" Copyright (c) 2000 Josef Karthauser <joe@FreeBSD.org>
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED. IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 .\" $FreeBSD: src/share/man/man7/style.perl.7,v 1.12.2.3 2001/08/17 13:08:49 ru Exp $
32 .Nd "FreeBSD Perl source file style guide"
34 This file specifies the preferred style for perl scripts in the
39 # Style guide for Perl. Based on the kernel style guide.
43 # VERY important single-line comments look like this.
46 # Most single-line comments look like this.
48 # Multi-line comments look like this. Make them real sentences.
49 # Fill them so they look like real paragraphs.
52 All scripts should follow the copyright block at the start of the
53 script with a comment block that describes what the script does.
60 # This script processes an old kernel config file, which it gets on
61 # stdin, and outputs a new style hints file to stdout.
64 All scripts should use the
66 module and run without warnings.
71 # Copyright, description of what the script does, etc
77 Where possible run the script with taint mode switched on.
84 The main program should be placed in a block labeled MAIN:.
86 makes it easier to identify the entry point in a large perl script,
87 and provides a scope for variables which are used in the main
88 program but nowhere else.
91 print(foo("/usr/bin/man", "7", "style.perl"));
96 All subroutines should be defined using argument prototypes as defined in
105 All variables should be defined before use; this is enforced if operating
109 Scope local variables should be defined using
117 declaration should only be used when it is required, and not by
119 Lots of perl4 scripts use
123 definition didn't exist prior to perl5.
125 In most cases globals should be defined at the top of the code
130 use vars qw($globalscalar @globalarray %globalhash);
133 In some cases it may be appropriate to use
135 statements at the top of the script as an alternative to using
139 All variables should be commented.
142 my $cmd = shift; # Command to run
143 my @args = @_; # Arguments to $cmd
147 Local variables should be separated from function arguments by a
151 my $cmd = shift; # Command to run
152 my @args = @_; # Arguments to command
156 my $output; # Output from command
160 Whenever possible code should be run through the code checker
168 and produce no warnings.
170 Indentation is an 8 character tab.
171 Second level indents are four spaces.
174 z = a + really + long + statement + that + needs +
175 two lines + gets + indented + four + spaces +
176 on + the + second + and + subsequent + lines.
180 Do not add whitespace at the end of a line, and only use tabs
181 followed by spaces to form the indentation.
182 Do not use more spaces
183 than a tab will produce and do not use spaces in front of tabs.
185 Opening braces should be at the end of the controlling line.
187 and elsif belong on the same line as the closing brace for the
188 previous if or elsif block:
191 my $cmd = shift; # Command to run
192 my @args = @_; # Arguments to command
196 my $output; # Output from command
198 unless (defined($pid = open(PIPE, "-|"))) {
199 die("open(): $!\\n");
200 } elsif ($pid == 0) {
202 die("exec(): $!\\n");
210 die("$cmd caught a signal " . ($? & 0x7f) . "\\n");
212 die("$cmd returned exit code " . ($? >> 8) . "\\n");
218 Where possible scripts should use standard modules instead of
219 rewriting the code inline.
220 It may be appropriate in some cases to
221 import a CPAN module into the base system to facilitate this.
232 .Ic if Pq Cm \&! No ...\&
233 where it improves readability.
235 Where it doesn't conflict with this guide read
237 and adopt Larry Wall's style recommendations.
243 This man page is largely based on the