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.8 by root, Tue Jan 18 18:15:21 2011 UTC vs.
Revision 1.19 by root, Thu Apr 2 07:53:41 2020 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines