ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/common-sense/sense.pm.PL
Revision: 1.9
Committed: Sat Mar 24 12:46:11 2012 UTC (12 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_5
Changes since 1.8: +2 -1 lines
Log Message:
3.5

File Contents

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