ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/common-sense/sense.pm
(Generate patch)

Comparing common-sense/sense.pm (file contents):
Revision 1.17 by root, Tue Sep 8 16:04:52 2009 UTC vs.
Revision 1.22 by root, Wed Nov 4 11:04:25 2009 UTC

4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use common::sense; 7 use common::sense;
8 8
9 # roughly the same as, with much lower memory usage: 9 # supposed to be the same, with much lower memory usage, as:
10 # 10 #
11 # use strict qw(vars subs); 11 # use strict qw(vars subs);
12 # use feature qw(say state switch); 12 # use feature qw(say state switch);
13 # no warnings; 13 # no warnings;
14 # use warnings qw(FATAL closed threads internal debugging pack substr malloc 14 # use warnings qw(FATAL closed threads internal debugging pack substr malloc
19 19
20=head1 DESCRIPTION 20=head1 DESCRIPTION
21 21
22This module implements some sane defaults for Perl programs, as defined by 22This module implements some sane defaults for Perl programs, as defined by
23two typical (or not so typical - use your common sense) specimens of Perl 23two typical (or not so typical - use your common sense) specimens of Perl
24coders. 24coders. In fact, after working out details on which warnings and strict
25modes to enable and make fatal, we found that we (and our code written so
26far, and others) fully agree on every option, even though we never used
27warnings before, so it seems this module indeed reflects a "common" sense
28among some long-time Perl coders.
29
30The basic philosophy behind the choices made in common::sense can be
31summarised as: "enforcing strict policies to catch as many bugs as
32possible, while at the same time, not limiting the expressive power
33available to the programmer".
34
35Two typical examples of this philosophy are uninitialised and malloc
36warnings:
37
38C<undef> is a well-defined feature of perl, and enabling warnings for
39using it rarely catches any bugs, but considerably limits you in what you
40can do, so uninitialised warnings are disabled.
41
42Freeing something twice on the C level is a serious bug, usually causing
43memory corruption. It often leads to side effects much later in the
44program and there are no advantages to not reporting this, so malloc
45warnings are fatal by default.
46
47What follows is a more thorough discussion of what this module does,
48and why it does it, and what the advantages (and disadvantages) of this
49approach are.
25 50
26=over 4 51=over 4
27 52
28=item use strict qw(subs vars) 53=item use strict qw(subs vars)
29 54
41 66
42This is annoying, and doesn't shield against obvious mistakes such as 67This is annoying, and doesn't shield against obvious mistakes such as
43using C<"">, so one would even have to write (at least for the time 68using C<"">, so one would even have to write (at least for the time
44being): 69being):
45 70
46 @{ defined $var->[0] ? $var->[0] : [] } 71 @{ defined $var->[0] ? $var->[0] : [] }
47 72
48... which nobody with a bit of common sense would consider 73... which nobody with a bit of common sense would consider
49writing. 74writing: clear code is clearly something else.
50 75
51Curiously enough, sometimes perl is not so strict, as this works even with 76Curiously enough, sometimes perl is not so strict, as this works even with
52C<use strict> in scope: 77C<use strict> in scope:
53 78
54 for (@{ $var->[0] }) { ... 79 for (@{ $var->[0] }) { ...
68Perl, regardless of use feature or not, so a new major perl release means 93Perl, regardless of use feature or not, so a new major perl release means
69changes to many modules - new keywords are just the tip of the iceberg. 94changes to many modules - new keywords are just the tip of the iceberg.
70 95
71If your code isn't alive, it's dead, Jim - be an active maintainer. 96If your code isn't alive, it's dead, Jim - be an active maintainer.
72 97
98But nobody forces you to use those extra features in modules meant for
99older versions of perl - common::sense of course works there as well.
100There is also an important other mode where having additional features by
101default is useful: commandline hacks and internal use scripts: See "much
102reduced typing", below.
103
73 104
74=item no warnings, but a lot of new errors 105=item no warnings, but a lot of new errors
75 106
76Ah, the dreaded warnings. Even worse, the horribly dreaded C<-w> 107Ah, the dreaded warnings. Even worse, the horribly dreaded C<-w>
77switch: Even though we don't care if other people use warnings (and 108switch: Even though we don't care if other people use warnings (and
120 151
121We quickly agreed that indeed the code should be changed, even though it 152We quickly agreed that indeed the code should be changed, even though it
122happened to do the right thing when the warning was switched off. 153happened to do the right thing when the warning was switched off.
123 154
124 155
156=item much reduced typing
157
158Especially with version 2.0 of common::sense, the amount of boilerplate
159code you need to add to gte I<this> policy is daunting. Nobody would write
160this out in throwaway scripts, commandline hacks or in quick internal-use
161scripts.
162
163By using common::sense you get a defined set of policies (ours, but maybe
164yours, too, if you accept them), and they are easy to apply to your
165scripts: typing C<use common::sense;> is even shorter than C<use warnings;
166use strict; use feature ...>.
167
168And you can immediately use the features of your installed perl, which
169is more difficult in code you release, but not usually an issue for
170internal-use code (downgrades of your production perl should be rare,
171right?).
172
173
125=item mucho reduced memory usage 174=item mucho reduced memory usage
126 175
127Just using all those pragmas mentioned in the SYNOPSIS together wastes 176Just using all those pragmas mentioned in the SYNOPSIS together wastes
128<blink>I<< B<776> kilobytes >></blink> of precious memory in my perl, for 177<blink>I<< B<776> kilobytes >></blink> of precious memory in my perl, for
129I<every single perl process using our code>, which on our machines, is a 178I<every single perl process using our code>, which on our machines, is a
139 188
140=cut 189=cut
141 190
142package common::sense; 191package common::sense;
143 192
144our $VERSION = '2.0'; 193our $VERSION = '2.02';
145 194
146# paste this into pelr to find bitmask 195# paste this into perl to find bitmask
147 196
148# no warnings; 197# no warnings;
149# use warnings qw(FATAL closed threads internal debugging pack substr malloc unopened portable prototype 198# use warnings qw(FATAL closed threads internal debugging pack substr malloc unopened portable prototype
150# inplace io pipe unpack regexp deprecated exiting glob digit printf 199# inplace io pipe unpack regexp deprecated exiting glob digit printf
151# utf8 layer reserved parenthesis taint closure semicolon); 200# utf8 layer reserved parenthesis taint closure semicolon);
154 203
155# overload should be included 204# overload should be included
156 205
157sub import { 206sub import {
158 # verified with perl 5.8.0, 5.10.0 207 # verified with perl 5.8.0, 5.10.0
159 ${^WARNING_BITS} = "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03"; 208 ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03";
160 209
161 # use strict vars subs 210 # use strict vars subs
162 $^H |= 0x00000600; 211 $^H |= 0x00000600;
163 212
164 # use feature 213 # use feature
251 300
252apeiron (meta-comment about us commenting^Wquoting his comment) 301apeiron (meta-comment about us commenting^Wquoting his comment)
253 302
254 How about quoting this: get a clue, you fucktarded amoeba. 303 How about quoting this: get a clue, you fucktarded amoeba.
255 304
305quanth
306
307 common sense is beautiful, json::xs is fast, Anyevent, EV are fast and
308 furious. I love mlehmannware ;)
309
310=head1 FREQUQNTLY ASKED QUESTIONS
311
312Or frequently-come-up confusions.
313
314=over 4
315
316=item Is this module meant to be serious?
317
318Yes, we would have put it under the C<Acme::> namespace otherwise.
319
320=item But the manpage is written in a funny/stupid/... way?
321
322This was meant to make it clear that our common sense is a subjective
323thing and other people can use their own notions, taking the steam out
324of anybody who might be offended (as some people are always offended no
325matter what you do).
326
327This was a failure.
328
329But we hope the manpage still is somewhat entertaining even though it
330explains boring rationale.
331
332=item Why do you impose your conventions on my code?
333
334For some reason people keep thinking that C<common::sense> imposes
335process-wide limits, even though the SYNOPSIS makes it clear that it works
336like other similar modules - only on the scope that uses them.
337
338So, no, we don't - nobody is forced to use this module, and using a module
339that relies on common::sense does not impose anything on you.
340
341=item Why do you think only your notion of common::sense is valid?
342
343Well, we don't, and have clearly written this in the documentation to
344every single release. We were just faster than anybody else w.r.t. to
345grabbing the namespace.
346
347=item But everybody knows that you have to use strict and use warnings,
348why do you disable them?
349
350Well, we don't do this either - we selectively disagree with the
351usefulness of some warnings over others. This module is aimed at
352experienced Perl programmers, not people migrating from other languages
353who might be surprised about stuff such as C<undef>. On the other hand,
354this does not exclude the usefulness of this module for total newbies, due
355to its strictness in enforcing policy, while at the same time not limiting
356the expresive power of perl.
357
358This module is considerably I<more> strict than the canonical C<use
359strict; use warnings>, as it makes all its warnings fatal in nature, so
360you can not get away with as many things as with the canonical approach.
361
362This was not implemented in version 1.0 because of the daunting number
363of warning categories and the difficulty in getting exactly the set of
364warnings you wish (i.e. look at the SYNOPSIS in how complicated it is to
365get a specific set of warnings - it is not reasonable to put this into
366every module, the maintainance effort would be enourmous).
367
368=item But many modules C<use strict> or C<use warnings>, so the memory
369savings do not apply?
370
371I am suddenly so sad.
372
373But yes, that's true. Fortunately C<common::sense> still uses only a
374miniscule amount of RAM.
375
376=item But it adds another dependency to your modules!
377
378It's a fact, yeah. But it's trivial to install, most popular modules have
379many more dependencies and we consider dependencies a good thing - it
380leads to better APIs, more thought about interworking of modules and so
381on.
382
383=item Why do you use JSON and not YAML for your META.yml?
384
385This is not true - YAML supports a large subset of JSON, and this subset
386is what META.yml is written in, so it would be correct to say "the
387META.yml is written in a common subset of YAML and JSON".
388
389The META.yml follows the YAML, JSON and META.yml specifications, and is
390correctly parsed by CPAN, so if you have trouble with it, the problem is
391likely on your side.
392
393=item But! But!
394
395Yeah, we know.
396
397=back
398
256=head1 AUTHOR 399=head1 AUTHOR
257 400
258 Marc Lehmann <schmorp@schmorp.de> 401 Marc Lehmann <schmorp@schmorp.de>
259 http://home.schmorp.de/ 402 http://home.schmorp.de/
260 403

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines