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

Comparing common-sense/sense.pm.PL (file contents):
Revision 1.4 by root, Thu Apr 15 23:46:22 2010 UTC vs.
Revision 1.13 by root, Tue Jul 30 23:27:09 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines