~ubuntu-branches/ubuntu/intrepid/perl-doc-html/intrepid

« back to all changes in this revision

Viewing changes to perlstyle.html

  • Committer: Bazaar Package Importer
  • Author(s): Roberto C. Sanchez
  • Date: 2008-05-17 20:14:19 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080517201419-qgbuogq2ckkdisyi
Tags: 5.10.0-2
Supersede botched upload of 5.10.0-1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
      <h2>Links:</h2>
55
55
      <ul>
56
56
        <li><a href="http://search.cpan.org">CPAN</a></li>
 
57
        <li><a href="http://www.perl.org">Perl.org</a></li>
57
58
        <li><a href="http://www.perl.com">Perl.com</a></li>
58
 
        <li><a href="http://www.perl.org">Perl.org</a></li>
 
59
        <li><a href="http://perlbuzz.com">Perl Buzz</a></li>
 
60
        <li><a href="http://www.perlfoundation.org/perl5/index.cgi">Perl 5 Wiki</a></li>
 
61
        <li><a href="http://jobs.perl.org">Perl Jobs</a></li>
59
62
        <li><a href="http://www.pm.org">Perl Mongers</a></li>
60
63
        <li><a href="http://www.perlmonks.org">Perl Monks</a></li>
61
64
        <li><a href="http://planet.perl.org">Planet Perl</a></li>
65
68
      <ul>
66
69
        <li>Site maintained by<br><a href="http://perl.jonallen.info">Jon Allen</a>
67
70
            (<a href="http://perl.jonallen.info">JJ</a>)</li>
68
 
        <li class="spaced">Last updated on<br>23 April 2006</li>
 
71
        <li class="spaced">Last updated on<br>23 December 2007</li>
69
72
        <li class="spaced">See the <a href="http://perl.jonallen.info/projects/perldoc">project page</a> for
70
73
        more details</li>
71
74
      </ul>
76
79
    <div id="centerContent">
77
80
      <div id="contentHeader">
78
81
        <div id="contentHeaderLeft"><a href="#" onClick="showLeft()">Show navigation</a></div>
79
 
        <div id="contentHeaderCentre">-- Perl 5.8.8 documentation --</div>
 
82
        <div id="contentHeaderCentre">-- Perl 5.10.0 documentation --</div>
80
83
        <div id="contentHeaderRight"><a href="#" onClick="showRight()">Show toolbar</a></div>
81
84
      </div>
82
85
      <div id="breadCrumbs"><a href="index.html">Home</a> &gt; <a href="index-tutorials.html">Tutorials</a> &gt; perlstyle</div>
89
92
make your programs easier to read, understand, and maintain.</p>
90
93
<p>The most important thing is to run your programs under the <b>-w</b>
91
94
flag at all times.  You may turn it off explicitly for particular
92
 
portions of code via the <code class="inline"><a class="l_k" href="functions/no.html">no</a> <a class="l_w" href="warnings.html">warnings</a></code>
 
95
portions of code via the <code class="inline"><a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span></code>
93
96
 pragma or the <code class="inline"><span class="i">$^W</span></code>
94
97
 variable
95
 
if you must.  You should also always run under <code class="inline"><a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="strict.html">strict</a></code>
 
98
if you must.  You should also always run under <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span></code>
96
99
 or know the
97
 
reason why not.  The <code class="inline"><a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="sigtrap.html">sigtrap</a></code>
98
 
 and even <code class="inline"><a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="diagnostics.html">diagnostics</a></code>
 
100
reason why not.  The <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">sigtrap</span></code>
 
101
 and even <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">diagnostics</span></code>
99
102
 pragmas
100
103
may also prove useful.</p>
101
104
<p>Regarding aesthetics of code lay out, about the only thing Larry
163
166
you <i>SHOULD</i> do it that way.  Perl is designed to give you several
164
167
ways to do anything, so consider picking the most readable one.  For
165
168
instance</p>
166
 
<pre class="verbatim">    <a class="l_k" href="functions/open.html">open</a><span class="s">(</span>FOO<span class="cm">,</span><span class="i">$foo</span><span class="s">)</span> || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Can&#39;t open $foo: $!&quot;</span><span class="sc">;</span></pre>
 
169
<pre class="verbatim">    <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span><span class="i">$foo</span><span class="s">)</span> || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Can&#39;t open $foo: $!&quot;</span><span class="sc">;</span></pre>
167
170
<p>is better than</p>
168
 
<pre class="verbatim">    <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Can&#39;t open $foo: $!&quot;</span> unless <a class="l_k" href="functions/open.html">open</a><span class="s">(</span>FOO<span class="cm">,</span><span class="i">$foo</span><span class="s">)</span><span class="sc">;</span></pre>
 
171
<pre class="verbatim">    <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Can&#39;t open $foo: $!&quot;</span> unless <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span><span class="cm">,</span><span class="i">$foo</span><span class="s">)</span><span class="sc">;</span></pre>
169
172
<p>because the second way hides the main point of the statement in a
170
173
modifier.  On the other hand</p>
171
174
<pre class="verbatim">    <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Starting analysis\n&quot;</span> if <span class="i">$verbose</span><span class="sc">;</span></pre>
192
195
the middle.  Just "outdent" it a little to make it more visible:</p>
193
196
<pre class="verbatim">    <span class="j">LINE:</span>
194
197
        for <span class="s">(</span><span class="sc">;</span><span class="sc">;</span><span class="s">)</span> <span class="s">{</span>
195
 
            statements<span class="sc">;</span>
 
198
            <span class="w">statements</span><span class="sc">;</span>
196
199
          <a class="l_k" href="functions/last.html">last</a> <span class="j">LINE</span> if <span class="i">$foo</span><span class="sc">;</span>
197
200
            <a class="l_k" href="functions/next.html">next</a> <span class="j">LINE</span> if <span class="q">/^#/</span><span class="sc">;</span>
198
 
            statements<span class="sc">;</span>
 
201
            <span class="w">statements</span><span class="sc">;</span>
199
202
        <span class="s">}</span></pre>
200
203
</li>
201
204
<li>
216
219
you know what version or patchlevel a particular feature was
217
220
implemented, you can test <code class="inline"><span class="i">$]</span></code>
218
221
 (<code class="inline"><span class="i">$PERL_VERSION</span></code>
219
 
 in <code class="inline"><a class="l_w" href="English.html">English</a></code>) to see if it
220
 
will be there.  The <code class="inline"><a class="l_w" href="Config.html">Config</a></code> module will also let you interrogate values
 
222
 in <code class="inline"><span class="w">English</span></code>
 
223
) to see if it
 
224
will be there.  The <code class="inline"><span class="w">Config</span></code>
 
225
 module will also let you interrogate values
221
226
determined by the <b>Configure</b> program when Perl was installed.</p>
222
227
</li>
223
228
<li>
232
237
 than <code class="inline"><span class="i">$VarNamesLikeThis</span></code>
233
238
, especially for
234
239
non-native speakers of English. It's also a simple rule that works
235
 
consistently with <code class="inline">VAR_NAMES_LIKE_THIS</code>
 
240
consistently with <code class="inline"><span class="w">VAR_NAMES_LIKE_THIS</span></code>
236
241
.</p>
237
242
<p>Package names are sometimes an exception to this rule.  Perl informally
238
 
reserves lowercase module names for "pragma" modules like <code class="inline"><a class="l_w" href="integer.html">integer</a></code> and
239
 
<code class="inline"><a class="l_w" href="strict.html">strict</a></code>.  Other modules should begin with a capital letter and use mixed
 
243
reserves lowercase module names for "pragma" modules like <code class="inline"><span class="w">integer</span></code>
 
244
 and
 
245
<code class="inline"><span class="w">strict</span></code>
 
246
.  Other modules should begin with a capital letter and use mixed
240
247
case, but probably without underscores due to limitations in primitive
241
248
file systems' representations of module names as files that must fit into a
242
249
few sparse bytes.</p>
281
288
</li>
282
289
<li>
283
290
<p>Always check the return codes of system calls.  Good error messages should
284
 
go to <code class="inline">STDERR</code>
 
291
go to <code class="inline"><span class="w">STDERR</span></code>
285
292
, include which program caused the problem, what the failed
286
293
system call and arguments were, and (VERY IMPORTANT) should contain the
287
294
standard system error message for what went wrong.  Here's a simple but
288
295
sufficient example:</p>
289
 
<pre class="verbatim">    <a class="l_k" href="functions/opendir.html">opendir</a><span class="s">(</span>D<span class="cm">,</span> <span class="i">$dir</span><span class="s">)</span>         or <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;can&#39;t opendir $dir: $!&quot;</span><span class="sc">;</span></pre>
 
296
<pre class="verbatim">    <a class="l_k" href="functions/opendir.html">opendir</a><span class="s">(</span><span class="w">D</span><span class="cm">,</span> <span class="i">$dir</span><span class="s">)</span>  or <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;can&#39;t opendir $dir: $!&quot;</span><span class="sc">;</span></pre>
290
297
</li>
291
298
<li>
292
299
<p>Line up your transliterations when it makes sense:</p>
297
304
<p>Think about reusability.  Why waste brainpower on a one-shot when you
298
305
might want to do something like it again?  Consider generalizing your
299
306
code.  Consider writing a module or object class.  Consider making your
300
 
code run cleanly with <code class="inline"><a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="strict.html">strict</a></code>
301
 
 and <code class="inline"><a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="warnings.html">warnings</a></code>
 
307
code run cleanly with <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span></code>
 
308
 and <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span></code>
302
309
 (or <b>-w</b>) in
303
310
effect.  Consider giving away your code.  Consider changing your whole
304
311
world view.  Consider... oh, never mind.</p>
308
315
are commonly expected conventions:</p>
309
316
<ul>
310
317
<li>
311
 
<p>use <code class="inline">C&lt;&gt;</code>
 
318
<p>use <code class="inline"><span class="w">C</span>&lt;&gt;</code>
312
319
 for function, variable and module names (and more
313
320
generally anything that can be considered part of code, like filehandles
314
321
or specific values). Note that function names are considered more readable
316
323
.</p>
317
324
</li>
318
325
<li>
319
 
<p>use <code class="inline"><a class="l_w" href="B.html">B</a>&lt;&gt;</code>
 
326
<p>use <code class="inline"><span class="w">B</span>&lt;&gt;</code>
320
327
 for commands names like <b>cat</b> or <b>grep</b>.</p>
321
328
</li>
322
329
<li>
323
 
<p>use <code class="inline">F&lt;&gt;</code>
324
 
 or <code class="inline">C&lt;&gt;</code>
325
 
 for file names. <code class="inline">F&lt;&gt;</code>
 
330
<p>use <code class="inline"><span class="w">F</span>&lt;&gt;</code>
 
331
 or <code class="inline"><span class="w">C</span>&lt;&gt;</code>
 
332
 for file names. <code class="inline"><span class="w">F</span>&lt;&gt;</code>
326
333
 should
327
334
be the only Pod code for file names, but as most Pod formatters render it
328
335
as italic, Unix and Windows paths with their slashes and backslashes may
329
 
be less readable, and better rendered with <code class="inline">C&lt;&gt;</code>
 
336
be less readable, and better rendered with <code class="inline"><span class="w">C</span>&lt;&gt;</code>
330
337
.</p>
331
338
</li>
332
339
</ul>
355
362
          <!--<select name="r"><option value="1" selected>Go to top result<option value="0">Show results list</select>-->
356
363
        </form>
357
364
      </p>
 
365
      <script language="JavaScript" type="text/javascript" src="/perl-version.js"></script>
358
366
      <h2>Labels:</h2>
359
367
      <p>
360
368
        <a href="#" onClick="addLabel('perlstyle','perlstyle.html')">Add this page</a>