ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/common-sense/sense.pm
Revision: 1.29
Committed: Mon Dec 21 09:11:36 2009 UTC (14 years, 5 months ago) by root
Branch: MAIN
Changes since 1.28: +12 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3     common::sense - save a tree AND a kitten, use common::sense!
4    
5     =head1 SYNOPSIS
6    
7     use common::sense;
8    
9 root 1.19 # supposed to be the same, with much lower memory usage, as:
10 root 1.1 #
11 root 1.25 # use utf8;
12 root 1.1 # use strict qw(vars subs);
13     # use feature qw(say state switch);
14     # no warnings;
15 root 1.15 # use warnings qw(FATAL closed threads internal debugging pack substr malloc
16 root 1.24 # portable prototype inplace io pipe unpack regexp
17 root 1.25 # deprecated exiting glob digit printf layer
18 root 1.15 # reserved parenthesis taint closure semicolon);
19 root 1.24 # no warnings qw(exec newline unopened);
20 root 1.1
21     =head1 DESCRIPTION
22    
23     This module implements some sane defaults for Perl programs, as defined by
24 root 1.16 two typical (or not so typical - use your common sense) specimens of Perl
25 root 1.21 coders. In fact, after working out details on which warnings and strict
26     modes to enable and make fatal, we found that we (and our code written so
27     far, and others) fully agree on every option, even though we never used
28     warnings before, so it seems this module indeed reflects a "common" sense
29     among some long-time Perl coders.
30 root 1.1
31 root 1.22 The basic philosophy behind the choices made in common::sense can be
32     summarised as: "enforcing strict policies to catch as many bugs as
33     possible, while at the same time, not limiting the expressive power
34     available to the programmer".
35    
36 root 1.29 Two typical examples of how this philosophy is applied in practise is the
37     handling of uninitialised and malloc warnings:
38    
39     =over 4
40    
41     =item I<uninitialised>
42 root 1.22
43     C<undef> is a well-defined feature of perl, and enabling warnings for
44     using it rarely catches any bugs, but considerably limits you in what you
45     can do, so uninitialised warnings are disabled.
46    
47 root 1.29 =item I<malloc>
48    
49 root 1.22 Freeing something twice on the C level is a serious bug, usually causing
50     memory corruption. It often leads to side effects much later in the
51     program and there are no advantages to not reporting this, so malloc
52     warnings are fatal by default.
53    
54 root 1.29 =back
55    
56 root 1.22 What follows is a more thorough discussion of what this module does,
57     and why it does it, and what the advantages (and disadvantages) of this
58     approach are.
59    
60 root 1.29 =head1 RATIONALE
61    
62 root 1.1 =over 4
63    
64 root 1.25 =item use utf8
65    
66     While it's not common sense to write your programs in UTF-8, it's quickly
67 root 1.26 becoming the most common encoding, is the designated future default
68     encoding for perl sources, and the most convenient encoding available
69     (you can do really nice quoting tricks...). Experience has shown that our
70     programs were either all pure ascii or utf-8, both of which will stay the
71     same.
72 root 1.25
73     There are few drawbacks to enabling UTF-8 source code by default (mainly
74     some speed hits due to bugs in older versions of perl), so this module
75     enables UTF-8 source code encoding by default.
76    
77 root 1.1 =item use strict qw(subs vars)
78    
79     Using C<use strict> is definitely common sense, but C<use strict
80 root 1.11 'refs'> definitely overshoots its usefulness. After almost two
81 root 1.1 decades of Perl hacking, we decided that it does more harm than being
82     useful. Specifically, constructs like these:
83    
84     @{ $var->[0] }
85    
86 root 1.4 Must be written like this (or similarly), when C<use strict 'refs'> is in
87     scope, and C<$var> can legally be C<undef>:
88 root 1.1
89     @{ $var->[0] || [] }
90    
91     This is annoying, and doesn't shield against obvious mistakes such as
92 root 1.11 using C<"">, so one would even have to write (at least for the time
93     being):
94 root 1.1
95 root 1.18 @{ defined $var->[0] ? $var->[0] : [] }
96 root 1.1
97     ... which nobody with a bit of common sense would consider
98 root 1.18 writing: clear code is clearly something else.
99 root 1.11
100     Curiously enough, sometimes perl is not so strict, as this works even with
101     C<use strict> in scope:
102 root 1.1
103     for (@{ $var->[0] }) { ...
104    
105 root 1.15 If that isn't hypocrisy! And all that from a mere program!
106    
107 root 1.1
108     =item use feature qw(say state given)
109    
110     We found it annoying that we always have to enable extra features. If
111     something breaks because it didn't anticipate future changes, so be
112 root 1.11 it. 5.10 broke almost all our XS modules and nobody cared either (or at
113 root 1.15 least I know of nobody who really complained about gratuitous changes -
114     as opposed to bugs).
115 root 1.11
116     Few modules that are not actively maintained work with newer versions of
117     Perl, regardless of use feature or not, so a new major perl release means
118     changes to many modules - new keywords are just the tip of the iceberg.
119    
120 root 1.15 If your code isn't alive, it's dead, Jim - be an active maintainer.
121    
122 root 1.22 But nobody forces you to use those extra features in modules meant for
123     older versions of perl - common::sense of course works there as well.
124     There is also an important other mode where having additional features by
125     default is useful: commandline hacks and internal use scripts: See "much
126     reduced typing", below.
127    
128 root 1.15
129 root 1.16 =item no warnings, but a lot of new errors
130 root 1.15
131     Ah, the dreaded warnings. Even worse, the horribly dreaded C<-w>
132     switch: Even though we don't care if other people use warnings (and
133     certainly there are useful ones), a lot of warnings simply go against the
134     spirit of Perl.
135    
136     Most prominently, the warnings related to C<undef>. There is nothing wrong
137     with C<undef>: it has well-defined semantics, it is useful, and spitting
138     out warnings you never asked for is just evil.
139    
140 root 1.16 The result was that every one of our modules did C<no warnings> in the
141     past, to avoid somebody accidentally using and forcing his bad standards
142     on our code. Of course, this switched off all warnings, even the useful
143     ones. Not a good situation. Really, the C<-w> switch should only enable
144     warnings for the main program only.
145 root 1.15
146     Funnily enough, L<perllexwarn> explicitly mentions C<-w> (and not in a
147     favourable way, calling it outright "wrong"), but standard utilities, such
148     as L<prove>, or MakeMaker when running C<make test>, still enable them
149     blindly.
150    
151 root 1.16 For version 2 of common::sense, we finally sat down a few hours and went
152     through I<every single warning message>, identifiying - according to
153     common sense - all the useful ones.
154 root 1.15
155     This resulted in the rather impressive list in the SYNOPSIS. When we
156     weren't sure, we didn't include the warning, so the list might grow in
157     the future (we might have made a mistake, too, so the list might shrink
158 root 1.16 as well).
159 root 1.15
160     Note the presence of C<FATAL> in the list: we do not think that the
161     conditions caught by these warnings are worthy of a warning, we I<insist>
162 root 1.16 that they are worthy of I<stopping> your program, I<instantly>. They are
163     I<bugs>!
164 root 1.15
165     Therefore we consider C<common::sense> to be much stricter than C<use
166     warnings>, which is good if you are into strict things (we are not,
167     actually, but these things tend to be subjective).
168    
169     After deciding on the list, we ran the module against all of our code that
170     uses C<common::sense> (that is almost all of our code), and found only one
171     occurence where one of them caused a problem: one of elmex's (unreleased)
172     modules contained:
173    
174     $fmt =~ s/([^\s\[]*)\[( [^\]]* )\]/\x0$1\x1$2\x0/xgo;
175    
176     We quickly agreed that indeed the code should be changed, even though it
177     happened to do the right thing when the warning was switched off.
178    
179 root 1.11
180 root 1.22 =item much reduced typing
181    
182     Especially with version 2.0 of common::sense, the amount of boilerplate
183     code you need to add to gte I<this> policy is daunting. Nobody would write
184     this out in throwaway scripts, commandline hacks or in quick internal-use
185     scripts.
186    
187     By using common::sense you get a defined set of policies (ours, but maybe
188     yours, too, if you accept them), and they are easy to apply to your
189     scripts: typing C<use common::sense;> is even shorter than C<use warnings;
190     use strict; use feature ...>.
191    
192     And you can immediately use the features of your installed perl, which
193     is more difficult in code you release, but not usually an issue for
194     internal-use code (downgrades of your production perl should be rare,
195     right?).
196    
197    
198 root 1.11 =item mucho reduced memory usage
199    
200     Just using all those pragmas mentioned in the SYNOPSIS together wastes
201     <blink>I<< B<776> kilobytes >></blink> of precious memory in my perl, for
202     I<every single perl process using our code>, which on our machines, is a
203     lot. In comparison, this module only uses I<< B<four> >> kilobytes (I even
204     had to write it out so it looks like more) of memory on the same platform.
205 root 1.1
206     The money/time/effort/electricity invested in these gigabytes (probably
207     petabytes globally!) of wasted memory could easily save 42 trees, and a
208     kitten!
209    
210 root 1.11 Unfortunately, until everybods applies more common sense, there will still
211     often be modules that pull in the monster pragmas. But one can hope...
212    
213 root 1.1 =cut
214    
215 root 1.2 package common::sense;
216 root 1.1
217 root 1.25 our $VERSION = '3.0';
218 root 1.15
219 root 1.19 # paste this into perl to find bitmask
220 root 1.1
221 root 1.13 # no warnings;
222 root 1.24 # use warnings qw(FATAL closed threads internal debugging pack substr malloc portable prototype
223 root 1.16 # inplace io pipe unpack regexp deprecated exiting glob digit printf
224 root 1.25 # layer reserved parenthesis taint closure semicolon);
225 root 1.24 # no warnings qw(exec newline unopened);
226 root 1.14 # BEGIN { warn join "", map "\\x$_", unpack "(H2)*", ${^WARNING_BITS}; exit 0 };
227 root 1.13
228     # overload should be included
229    
230 root 1.2 sub import {
231 root 1.13 # verified with perl 5.8.0, 5.10.0
232 root 1.25 ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\xfc\x3f\x33\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x00";
233 root 1.3
234 root 1.25 # use utf8, strict vars subs
235     $^H |= 0x00800600;
236 root 1.3
237     # use feature
238     $^H{feature_switch} =
239     $^H{feature_say} =
240     $^H{feature_state} = 1;
241 root 1.1 }
242    
243     1;
244    
245     =back
246    
247 root 1.5 =head1 THERE IS NO 'no common::sense'!!!! !!!! !!
248 root 1.4
249     This module doesn't offer an unimport. First of all, it wastes even more
250     memory, second, and more importantly, who with even a bit of common sense
251     would want no common sense?
252    
253 root 1.5 =head1 STABILITY AND FUTURE VERSIONS
254    
255     Future versions might change just about everything in this module. We
256     might test our modules and upload new ones working with newer versions of
257     this module, and leave you standing in the rain because we didn't tell
258 root 1.15 you. In fact, we did so when switching from 1.0 to 2.0, which enabled gobs
259     of warnings, and made them FATAL on top.
260 root 1.5
261 root 1.15 Maybe we will load some nifty modules that try to emulate C<say> or so
262     with perls older than 5.10 (this module, of course, should work with older
263     perl versions - supporting 5.8 for example is just common sense at this
264     time. Maybe not in the future, but of course you can trust our common
265     sense to be consistent with, uhm, our opinion).
266 root 1.11
267     =head1 WHAT OTHER PEOPLE HAD TO SAY ABOUT THIS MODULE
268    
269     apeiron
270    
271     "... wow"
272     "I hope common::sense is a joke."
273    
274     crab
275 root 1.5
276 root 1.11 "i wonder how it would be if joerg schilling wrote perl modules."
277 root 1.7
278 root 1.17 Adam Kennedy
279    
280     "Very interesting, efficient, and potentially something I'd use all the time."
281     [...]
282     "So no common::sense for me, alas."
283    
284 root 1.11 H.Merijn Brand
285    
286     "Just one more reason to drop JSON::XS from my distribution list"
287 root 1.7
288     Pista Palo
289    
290     "Something in short supply these days..."
291    
292     Steffen Schwigon
293    
294     "This module is quite for sure *not* just a repetition of all the other
295     'use strict, use warnings'-approaches, and it's also not the opposite.
296     [...] And for its chosen middle-way it's also not the worst name ever.
297     And everything is documented."
298    
299     BKB
300    
301     "[Deleted - thanks to Steffen Schwigon for pointing out this review was
302     in error.]"
303    
304     Somni
305    
306     "the arrogance of the guy"
307     "I swear he tacked somenoe else's name onto the module
308     just so he could use the royal 'we' in the documentation"
309    
310 root 1.23 Anonymous Monk
311    
312     "You just gotta love this thing, its got META.json!!!"
313    
314 root 1.7 dngor
315    
316     "Heh. '"<elmex at ta-sa.org>"' The quotes are semantic
317     distancing from that e-mail address."
318    
319     Jerad Pierce
320    
321     "Awful name (not a proper pragma), and the SYNOPSIS doesn't tell you
322     anything either. Nor is it clear what features have to do with "common
323     sense" or discipline."
324    
325     acme
326    
327     "THERE IS NO 'no common::sense'!!!! !!!! !!"
328    
329 root 1.15 apeiron (meta-comment about us commenting^Wquoting his comment)
330 root 1.12
331 root 1.23 "How about quoting this: get a clue, you fucktarded amoeba."
332 root 1.12
333 root 1.20 quanth
334    
335 root 1.23 "common sense is beautiful, json::xs is fast, Anyevent, EV are fast and
336     furious. I love mlehmannware ;)"
337 root 1.20
338 root 1.28 apeiron
339    
340     "... it's mlehmann's view of what common sense is. His view of common
341     sense is certainly uncommon, insofar as anyone with a clue disagrees
342     with him."
343    
344 root 1.27 apeiron (another meta-comment)
345    
346     "apeiron wonders if his little informant is here to steal more quotes"
347    
348     ew73
349    
350     "... I never got past the SYNOPSIS before calling it shit."
351    
352 root 1.19 =head1 FREQUQNTLY ASKED QUESTIONS
353    
354     Or frequently-come-up confusions.
355    
356     =over 4
357    
358     =item Is this module meant to be serious?
359    
360     Yes, we would have put it under the C<Acme::> namespace otherwise.
361    
362     =item But the manpage is written in a funny/stupid/... way?
363    
364     This was meant to make it clear that our common sense is a subjective
365     thing and other people can use their own notions, taking the steam out
366     of anybody who might be offended (as some people are always offended no
367     matter what you do).
368    
369     This was a failure.
370    
371     But we hope the manpage still is somewhat entertaining even though it
372     explains boring rationale.
373    
374     =item Why do you impose your conventions on my code?
375    
376     For some reason people keep thinking that C<common::sense> imposes
377     process-wide limits, even though the SYNOPSIS makes it clear that it works
378     like other similar modules - only on the scope that uses them.
379    
380     So, no, we don't - nobody is forced to use this module, and using a module
381     that relies on common::sense does not impose anything on you.
382    
383     =item Why do you think only your notion of common::sense is valid?
384    
385     Well, we don't, and have clearly written this in the documentation to
386     every single release. We were just faster than anybody else w.r.t. to
387     grabbing the namespace.
388    
389     =item But everybody knows that you have to use strict and use warnings,
390     why do you disable them?
391    
392     Well, we don't do this either - we selectively disagree with the
393     usefulness of some warnings over others. This module is aimed at
394     experienced Perl programmers, not people migrating from other languages
395 root 1.22 who might be surprised about stuff such as C<undef>. On the other hand,
396     this does not exclude the usefulness of this module for total newbies, due
397     to its strictness in enforcing policy, while at the same time not limiting
398     the expresive power of perl.
399    
400     This module is considerably I<more> strict than the canonical C<use
401     strict; use warnings>, as it makes all its warnings fatal in nature, so
402     you can not get away with as many things as with the canonical approach.
403 root 1.19
404     This was not implemented in version 1.0 because of the daunting number
405     of warning categories and the difficulty in getting exactly the set of
406     warnings you wish (i.e. look at the SYNOPSIS in how complicated it is to
407     get a specific set of warnings - it is not reasonable to put this into
408     every module, the maintainance effort would be enourmous).
409    
410     =item But many modules C<use strict> or C<use warnings>, so the memory
411     savings do not apply?
412    
413     I am suddenly so sad.
414    
415     But yes, that's true. Fortunately C<common::sense> still uses only a
416     miniscule amount of RAM.
417    
418     =item But it adds another dependency to your modules!
419    
420     It's a fact, yeah. But it's trivial to install, most popular modules have
421     many more dependencies and we consider dependencies a good thing - it
422     leads to better APIs, more thought about interworking of modules and so
423     on.
424    
425 root 1.22 =item Why do you use JSON and not YAML for your META.yml?
426    
427     This is not true - YAML supports a large subset of JSON, and this subset
428     is what META.yml is written in, so it would be correct to say "the
429     META.yml is written in a common subset of YAML and JSON".
430    
431     The META.yml follows the YAML, JSON and META.yml specifications, and is
432     correctly parsed by CPAN, so if you have trouble with it, the problem is
433     likely on your side.
434    
435 root 1.19 =item But! But!
436    
437     Yeah, we know.
438    
439     =back
440    
441 root 1.1 =head1 AUTHOR
442    
443     Marc Lehmann <schmorp@schmorp.de>
444     http://home.schmorp.de/
445    
446 root 1.4 Robin Redeker, "<elmex at ta-sa.org>".
447    
448 root 1.1 =cut
449