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.10 by root, Tue Aug 18 00:47:16 2009 UTC vs.
Revision 1.23 by root, Wed Nov 4 17:32:56 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
15 # unopened portable prototype inplace io pipe unpack regexp
16 # deprecated exiting glob digit printf utf8 layer
17 # reserved parenthesis taint closure semicolon);
18 # no warnings qw(exec newline);
14 19
15=head1 DESCRIPTION 20=head1 DESCRIPTION
16 21
17This module implements some sane defaults for Perl programs, as defined by 22This module implements some sane defaults for Perl programs, as defined by
18two typical (or not so typical - use your common sense) specimens of 23two typical (or not so typical - use your common sense) specimens of Perl
19Perl coders. 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.
20 50
21=over 4 51=over 4
22 52
23=item no warnings
24
25The dreaded warnings. Even worse, the horribly dreaded C<-w> switch. Even
26though we don't care if other people use warnings (and certainly there are
27useful ones), a lot of warnings simply go against the spirit of Perl, most
28prominently, the warnings related to C<undef>. There is nothing wrong with
29C<undef>: it has well-defined semantics, it is useful, and spitting out
30warnings you never asked for is just evil.
31
32So every module needs C<no warnings> to avoid somebody accidentally using
33C<-w> and forcing his bad standards on our code. No will do.
34
35Funnily enough, L<perllexwarn> explicitly mentions C<-w> (and not in a
36favourable way), but standard utilities, such as L<prove>, or MakeMaker
37when running C<make test> enable them blindly.
38
39=item use strict qw(subs vars) 53=item use strict qw(subs vars)
40 54
41Using C<use strict> is definitely common sense, but C<use strict 55Using C<use strict> is definitely common sense, but C<use strict
42'refs'> definitely overshoots it's usefulness. After almost two 56'refs'> definitely overshoots its usefulness. After almost two
43decades of Perl hacking, we decided that it does more harm than being 57decades of Perl hacking, we decided that it does more harm than being
44useful. Specifically, constructs like these: 58useful. Specifically, constructs like these:
45 59
46 @{ $var->[0] } 60 @{ $var->[0] }
47 61
49scope, and C<$var> can legally be C<undef>: 63scope, and C<$var> can legally be C<undef>:
50 64
51 @{ $var->[0] || [] } 65 @{ $var->[0] || [] }
52 66
53This is annoying, and doesn't shield against obvious mistakes such as 67This is annoying, and doesn't shield against obvious mistakes such as
54using C<"">, so one would even have to write: 68using C<"">, so one would even have to write (at least for the time
69being):
55 70
56 @{ defined $var->[0] ? $var->[0] : [] } 71 @{ defined $var->[0] ? $var->[0] : [] }
57 72
58... which nobody with a bit of common sense would consider 73... which nobody with a bit of common sense would consider
74writing: clear code is clearly something else.
75
59writing. Curiously enough, sometimes, perl is not so strict, as this works 76Curiously enough, sometimes perl is not so strict, as this works even with
60even with C<use strict> in scope: 77C<use strict> in scope:
61 78
62 for (@{ $var->[0] }) { ... 79 for (@{ $var->[0] }) { ...
63 80
64If that isnt hipocrasy! And all that from a mere program! 81If that isn't hypocrisy! And all that from a mere program!
82
65 83
66=item use feature qw(say state given) 84=item use feature qw(say state given)
67 85
68We found it annoying that we always have to enable extra features. If 86We found it annoying that we always have to enable extra features. If
69something breaks because it didn't anticipate future changes, so be 87something breaks because it didn't anticipate future changes, so be
70it. 5.10 broke almost all our XS modules and nobody cared either - and few 88it. 5.10 broke almost all our XS modules and nobody cared either (or at
89least I know of nobody who really complained about gratuitous changes -
90as opposed to bugs).
91
71modules that are no longer maintained work with newer versions of Perl, 92Few modules that are not actively maintained work with newer versions of
72regardless of use feature. 93Perl, regardless of use feature or not, so a new major perl release means
94changes to many modules - new keywords are just the tip of the iceberg.
73 95
74If your code isn't alive, it's dead, jim. 96If your code isn't alive, it's dead, Jim - be an active maintainer.
75 97
76=item much less memory 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.
77 103
78Just using all those pragmas together wastes <blink>I<< B<776> kilobytes 104
79>></blink> of precious memory in my perl, for I<every single perl process 105=item no warnings, but a lot of new errors
80using our code>, which on our machines, is a lot. In comparison, this 106
81module only uses I<< B<four> >> kilobytes (I even had to write it out so 107Ah, the dreaded warnings. Even worse, the horribly dreaded C<-w>
108switch: Even though we don't care if other people use warnings (and
109certainly there are useful ones), a lot of warnings simply go against the
110spirit of Perl.
111
112Most prominently, the warnings related to C<undef>. There is nothing wrong
113with C<undef>: it has well-defined semantics, it is useful, and spitting
114out warnings you never asked for is just evil.
115
116The result was that every one of our modules did C<no warnings> in the
117past, to avoid somebody accidentally using and forcing his bad standards
118on our code. Of course, this switched off all warnings, even the useful
119ones. Not a good situation. Really, the C<-w> switch should only enable
120warnings for the main program only.
121
122Funnily enough, L<perllexwarn> explicitly mentions C<-w> (and not in a
123favourable way, calling it outright "wrong"), but standard utilities, such
124as L<prove>, or MakeMaker when running C<make test>, still enable them
125blindly.
126
127For version 2 of common::sense, we finally sat down a few hours and went
128through I<every single warning message>, identifiying - according to
129common sense - all the useful ones.
130
131This resulted in the rather impressive list in the SYNOPSIS. When we
132weren't sure, we didn't include the warning, so the list might grow in
133the future (we might have made a mistake, too, so the list might shrink
134as well).
135
136Note the presence of C<FATAL> in the list: we do not think that the
137conditions caught by these warnings are worthy of a warning, we I<insist>
138that they are worthy of I<stopping> your program, I<instantly>. They are
139I<bugs>!
140
141Therefore we consider C<common::sense> to be much stricter than C<use
142warnings>, which is good if you are into strict things (we are not,
143actually, but these things tend to be subjective).
144
145After deciding on the list, we ran the module against all of our code that
146uses C<common::sense> (that is almost all of our code), and found only one
147occurence where one of them caused a problem: one of elmex's (unreleased)
148modules contained:
149
150 $fmt =~ s/([^\s\[]*)\[( [^\]]* )\]/\x0$1\x1$2\x0/xgo;
151
152We quickly agreed that indeed the code should be changed, even though it
153happened to do the right thing when the warning was switched off.
154
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
174=item mucho reduced memory usage
175
176Just using all those pragmas mentioned in the SYNOPSIS together wastes
177<blink>I<< B<776> kilobytes >></blink> of precious memory in my perl, for
178I<every single perl process using our code>, which on our machines, is a
179lot. In comparison, this module only uses I<< B<four> >> kilobytes (I even
82it looks like more) of memory on the same platform. 180had to write it out so it looks like more) of memory on the same platform.
83 181
84The money/time/effort/electricity invested in these gigabytes (probably 182The money/time/effort/electricity invested in these gigabytes (probably
85petabytes globally!) of wasted memory could easily save 42 trees, and a 183petabytes globally!) of wasted memory could easily save 42 trees, and a
86kitten! 184kitten!
87 185
186Unfortunately, until everybods applies more common sense, there will still
187often be modules that pull in the monster pragmas. But one can hope...
188
88=cut 189=cut
89 190
90package common::sense; 191package common::sense;
91 192
92our $VERSION = '0.04'; 193our $VERSION = '2.02';
194
195# paste this into perl to find bitmask
196
197# no warnings;
198# use warnings qw(FATAL closed threads internal debugging pack substr malloc unopened portable prototype
199# inplace io pipe unpack regexp deprecated exiting glob digit printf
200# utf8 layer reserved parenthesis taint closure semicolon);
201# no warnings qw(exec newline);
202# BEGIN { warn join "", map "\\x$_", unpack "(H2)*", ${^WARNING_BITS}; exit 0 };
203
204# overload should be included
93 205
94sub import { 206sub import {
95 # no warnings 207 # verified with perl 5.8.0, 5.10.0
96 ${^WARNING_BITS} ^= ${^WARNING_BITS}; 208 ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03";
97 209
98 # use strict vars subs 210 # use strict vars subs
99 $^H |= 0x00000600; 211 $^H |= 0x00000600;
100 212
101 # use feature 213 # use feature
117=head1 STABILITY AND FUTURE VERSIONS 229=head1 STABILITY AND FUTURE VERSIONS
118 230
119Future versions might change just about everything in this module. We 231Future versions might change just about everything in this module. We
120might test our modules and upload new ones working with newer versions of 232might test our modules and upload new ones working with newer versions of
121this module, and leave you standing in the rain because we didn't tell 233this module, and leave you standing in the rain because we didn't tell
122you. 234you. In fact, we did so when switching from 1.0 to 2.0, which enabled gobs
235of warnings, and made them FATAL on top.
123 236
124Most likely, we will pick a few useful warnings, instead of just disabling
125all of them. And maybe we will load some nifty modules that try to emulate 237Maybe we will load some nifty modules that try to emulate C<say> or so
126C<say> or so with perls older than 5.10 (this module, of course, should 238with perls older than 5.10 (this module, of course, should work with older
127work with older perl versions - supporting 5.8 for example is just common 239perl versions - supporting 5.8 for example is just common sense at this
128sense at this time. Maybe not in the future, but of course you can trust 240time. Maybe not in the future, but of course you can trust our common
129our common sense). 241sense to be consistent with, uhm, our opinion).
130 242
131
132=head1 WHAT OTHER PEOPLE HAVE TO SAY ABOUT THIS MODULE 243=head1 WHAT OTHER PEOPLE HAD TO SAY ABOUT THIS MODULE
244
245apeiron
246
247 "... wow"
248 "I hope common::sense is a joke."
249
250crab
251
252 "i wonder how it would be if joerg schilling wrote perl modules."
253
254Adam Kennedy
255
256 "Very interesting, efficient, and potentially something I'd use all the time."
257 [...]
258 "So no common::sense for me, alas."
259
260H.Merijn Brand
261
262 "Just one more reason to drop JSON::XS from my distribution list"
133 263
134Pista Palo 264Pista Palo
135 265
136 "Something in short supply these days..." 266 "Something in short supply these days..."
137 267
151 281
152 "the arrogance of the guy" 282 "the arrogance of the guy"
153 "I swear he tacked somenoe else's name onto the module 283 "I swear he tacked somenoe else's name onto the module
154 just so he could use the royal 'we' in the documentation" 284 just so he could use the royal 'we' in the documentation"
155 285
286Anonymous Monk
287
288 "You just gotta love this thing, its got META.json!!!"
289
156dngor 290dngor
157 291
158 "Heh. '"<elmex at ta-sa.org>"' The quotes are semantic 292 "Heh. '"<elmex at ta-sa.org>"' The quotes are semantic
159 distancing from that e-mail address." 293 distancing from that e-mail address."
160 294
166 300
167acme 301acme
168 302
169 "THERE IS NO 'no common::sense'!!!! !!!! !!" 303 "THERE IS NO 'no common::sense'!!!! !!!! !!"
170 304
171crab 305apeiron (meta-comment about us commenting^Wquoting his comment)
172 306
173 "i wonder how it would be if joerg schilling wrote perl modules." 307 "How about quoting this: get a clue, you fucktarded amoeba."
174 308
175H.Merijn Brand 309quanth
176 310
177 "Just one more reason to drop JSON::XS from my distribution list" 311 "common sense is beautiful, json::xs is fast, Anyevent, EV are fast and
312 furious. I love mlehmannware ;)"
178 313
179apeiron 314=head1 FREQUQNTLY ASKED QUESTIONS
180 315
181 "... wow" 316Or frequently-come-up confusions.
182 "I hope common::sense is a joke." 317
318=over 4
319
320=item Is this module meant to be serious?
321
322Yes, we would have put it under the C<Acme::> namespace otherwise.
323
324=item But the manpage is written in a funny/stupid/... way?
325
326This was meant to make it clear that our common sense is a subjective
327thing and other people can use their own notions, taking the steam out
328of anybody who might be offended (as some people are always offended no
329matter what you do).
330
331This was a failure.
332
333But we hope the manpage still is somewhat entertaining even though it
334explains boring rationale.
335
336=item Why do you impose your conventions on my code?
337
338For some reason people keep thinking that C<common::sense> imposes
339process-wide limits, even though the SYNOPSIS makes it clear that it works
340like other similar modules - only on the scope that uses them.
341
342So, no, we don't - nobody is forced to use this module, and using a module
343that relies on common::sense does not impose anything on you.
344
345=item Why do you think only your notion of common::sense is valid?
346
347Well, we don't, and have clearly written this in the documentation to
348every single release. We were just faster than anybody else w.r.t. to
349grabbing the namespace.
350
351=item But everybody knows that you have to use strict and use warnings,
352why do you disable them?
353
354Well, we don't do this either - we selectively disagree with the
355usefulness of some warnings over others. This module is aimed at
356experienced Perl programmers, not people migrating from other languages
357who might be surprised about stuff such as C<undef>. On the other hand,
358this does not exclude the usefulness of this module for total newbies, due
359to its strictness in enforcing policy, while at the same time not limiting
360the expresive power of perl.
361
362This module is considerably I<more> strict than the canonical C<use
363strict; use warnings>, as it makes all its warnings fatal in nature, so
364you can not get away with as many things as with the canonical approach.
365
366This was not implemented in version 1.0 because of the daunting number
367of warning categories and the difficulty in getting exactly the set of
368warnings you wish (i.e. look at the SYNOPSIS in how complicated it is to
369get a specific set of warnings - it is not reasonable to put this into
370every module, the maintainance effort would be enourmous).
371
372=item But many modules C<use strict> or C<use warnings>, so the memory
373savings do not apply?
374
375I am suddenly so sad.
376
377But yes, that's true. Fortunately C<common::sense> still uses only a
378miniscule amount of RAM.
379
380=item But it adds another dependency to your modules!
381
382It's a fact, yeah. But it's trivial to install, most popular modules have
383many more dependencies and we consider dependencies a good thing - it
384leads to better APIs, more thought about interworking of modules and so
385on.
386
387=item Why do you use JSON and not YAML for your META.yml?
388
389This is not true - YAML supports a large subset of JSON, and this subset
390is what META.yml is written in, so it would be correct to say "the
391META.yml is written in a common subset of YAML and JSON".
392
393The META.yml follows the YAML, JSON and META.yml specifications, and is
394correctly parsed by CPAN, so if you have trouble with it, the problem is
395likely on your side.
396
397=item But! But!
398
399Yeah, we know.
400
401=back
183 402
184=head1 AUTHOR 403=head1 AUTHOR
185 404
186 Marc Lehmann <schmorp@schmorp.de> 405 Marc Lehmann <schmorp@schmorp.de>
187 http://home.schmorp.de/ 406 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines