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.12 by root, Thu Jun 27 11:43:43 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;
7our $H; 7our $H;
8our %H;
8 9
9BEGIN { 10BEGIN {
10 $H = $^H; 11 $H = $^H;
11 $WARN = ${^WARNING_BITS}; 12 $WARN = ${^WARNING_BITS};
12} 13}
13 14
14use utf8; 15use utf8;
15use strict qw(subs vars); 16use strict qw(subs vars);
16 17
18BEGIN {
19 if ($] >= 5.010) {
20 require feature;
21 feature->import (qw(say state switch));
22 }
23 if ($] >= 5.012) {
24 feature->import (qw(unicode_strings));
25 }
26 if ($] >= 5.016) {
27 feature->import (qw(current_sub fc evalbytes));
28 feature->unimport (qw(array_base));
29 }
30}
31
17no warnings; 32no warnings;
18use warnings qw(FATAL closed threads internal debugging pack malloc portable prototype 33use warnings qw(FATAL closed threads internal debugging pack malloc portable prototype
19 inplace io pipe unpack regexp deprecated exiting glob digit printf 34 inplace io pipe unpack deprecated glob digit printf
20 layer reserved taint closure semicolon); 35 layer reserved taint closure semicolon);
21no warnings qw(exec newline unopened); 36no warnings qw(exec newline unopened);
22 37
23BEGIN { 38BEGIN {
24 $H = $^H & ~$H; 39 $H = $^H & ~$H;
25 $WARN = ${^WARNING_BITS} & ~$WARN; 40 $WARN = ${^WARNING_BITS} & ~$WARN;
41 %H = %^H;
26} 42}
27 43
28while (<DATA>) { 44while (<DATA>) {
29 if (/^IMPORT/) { 45 if (/^IMPORT/) {
30 print " # use warnings\n"; 46 print " # use warnings\n";
31 printf " \${^WARNING_BITS} ^= \${^WARNING_BITS} ^ \"%s\";\n", 47 printf " \${^WARNING_BITS} ^= \${^WARNING_BITS} ^ \"%s\";\n",
32 join "", map "\\x$_", unpack "(H2)*", $WARN; 48 join "", map "\\x$_", unpack "(H2)*", $WARN;
33 print " # use strict, use utf8;\n"; 49 print " # use strict, use utf8; use feature;\n";
34 printf " \$^H |= 0x%x;\n", $H; 50 printf " \$^H |= 0x%x;\n", $H;
51
52 if (my @features = grep /^feature_/, keys %H) {
53 print " \@^H{qw(@features)} = (1) x ", (scalar @features), ";\n";
54 }
35 } else { 55 } else {
36 print; 56 print;
37 } 57 }
38} 58}
39 59
46 66
47common::sense - save a tree AND a kitten, use common::sense! 67common::sense - save a tree AND a kitten, use common::sense!
48 68
49=head1 SYNOPSIS 69=head1 SYNOPSIS
50 70
51 use common::sense; 71 use common::sense;
52 72
53 # supposed to be the same, with much lower memory usage, as: 73 # Supposed to be mostly the same, with much lower memory usage, as:
54 # 74
55 # use utf8; 75 # use utf8;
56 # use strict qw(vars subs); 76 # use strict qw(vars subs);
57 # use feature qw(say state switch); 77 # use feature qw(say state switch);
78 # use feature qw(unicode_strings unicode_eval current_sub fc evalbytes);
79 # no feature qw(array_base);
58 # no warnings; 80 # no warnings;
59 # use warnings qw(FATAL closed threads internal debugging pack malloc 81 # use warnings qw(FATAL closed threads internal debugging pack
60 # portable prototype inplace io pipe unpack regexp 82 # portable prototype inplace io pipe unpack malloc
61 # deprecated exiting glob digit printf layer 83 # deprecated glob digit printf layer
62 # reserved taint closure semicolon); 84 # reserved taint closure semicolon);
63 # no warnings qw(exec newline unopened); 85 # no warnings qw(exec newline unopened);
64 86
65=head1 DESCRIPTION 87=head1 DESCRIPTION
88
89 “Nothing is more fairly distributed than common sense: no one thinks
90 he needs more of it than he already has.”
91
92 – René Descartes
66 93
67This module implements some sane defaults for Perl programs, as defined by 94This module implements some sane defaults for Perl programs, as defined by
68two typical (or not so typical - use your common sense) specimens of Perl 95two typical (or not so typical - use your common sense) specimens of Perl
69coders. In fact, after working out details on which warnings and strict 96coders. 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 97modes to enable and make fatal, we found that we (and our code written so
95program and there are no advantages to not reporting this, so malloc 122program and there are no advantages to not reporting this, so malloc
96warnings are fatal by default. 123warnings are fatal by default.
97 124
98=back 125=back
99 126
127Unfortunately, there is no fine-grained warning control in perl, so often
128whole groups of useful warnings had to be excluded because of a single
129useless warning (for example, perl puts an arbitrary limit on the length
130of text you can match with some regexes before emitting a warning, making
131the whole C<regexp> category useless).
132
100What follows is a more thorough discussion of what this module does, 133What follows is a more thorough discussion of what this module does,
101and why it does it, and what the advantages (and disadvantages) of this 134and why it does it, and what the advantages (and disadvantages) of this
102approach are. 135approach are.
103 136
104=head1 RATIONALE 137=head1 RATIONALE
148 for (@{ $var->[0] }) { ... 181 for (@{ $var->[0] }) { ...
149 182
150If that isn't hypocrisy! And all that from a mere program! 183If that isn't hypocrisy! And all that from a mere program!
151 184
152 185
153=item use feature qw(say state given) 186=item use feature qw(say state given ...)
154 187
155We found it annoying that we always have to enable extra features. If 188We found it annoying that we always have to enable extra features. If
156something breaks because it didn't anticipate future changes, so be 189something 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 190it. 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 - 191least I know of nobody who really complained about gratuitous changes -
168older versions of perl - common::sense of course works there as well. 201older versions of perl - common::sense of course works there as well.
169There is also an important other mode where having additional features by 202There is also an important other mode where having additional features by
170default is useful: commandline hacks and internal use scripts: See "much 203default is useful: commandline hacks and internal use scripts: See "much
171reduced typing", below. 204reduced typing", below.
172 205
206There is one notable exception: C<unicode_eval> is not enabled by
207default. In our opinion, C<use feature> had one main effect - newer perl
208versions don't value backwards compatibility and the ability to write
209modules for multiple perl versions much, after all, you can use feature.
210
211C<unicode_eval> doesn't add a new feature, it breaks an existing function.
173 212
174=item no warnings, but a lot of new errors 213=item no warnings, but a lot of new errors
175 214
176Ah, the dreaded warnings. Even worse, the horribly dreaded C<-w> 215Ah, the dreaded warnings. Even worse, the horribly dreaded C<-w>
177switch: Even though we don't care if other people use warnings (and 216switch: Even though we don't care if other people use warnings (and
192favourable way, calling it outright "wrong"), but standard utilities, such 231favourable way, calling it outright "wrong"), but standard utilities, such
193as L<prove>, or MakeMaker when running C<make test>, still enable them 232as L<prove>, or MakeMaker when running C<make test>, still enable them
194blindly. 233blindly.
195 234
196For version 2 of common::sense, we finally sat down a few hours and went 235For version 2 of common::sense, we finally sat down a few hours and went
197through I<every single warning message>, identifiying - according to 236through I<every single warning message>, identifying - according to
198common sense - all the useful ones. 237common sense - all the useful ones.
199 238
200This resulted in the rather impressive list in the SYNOPSIS. When we 239This 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 240weren'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 241the future (we might have made a mistake, too, so the list might shrink
211warnings>, which is good if you are into strict things (we are not, 250warnings>, which is good if you are into strict things (we are not,
212actually, but these things tend to be subjective). 251actually, but these things tend to be subjective).
213 252
214After deciding on the list, we ran the module against all of our code that 253After 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 254uses 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) 255occurrence where one of them caused a problem: one of elmex's (unreleased)
217modules contained: 256modules contained:
218 257
219 $fmt =~ s/([^\s\[]*)\[( [^\]]* )\]/\x0$1\x1$2\x0/xgo; 258 $fmt =~ s/([^\s\[]*)\[( [^\]]* )\]/\x0$1\x1$2\x0/xgo;
220 259
221We quickly agreed that indeed the code should be changed, even though it 260We quickly agreed that indeed the code should be changed, even though it
223 262
224 263
225=item much reduced typing 264=item much reduced typing
226 265
227Especially with version 2.0 of common::sense, the amount of boilerplate 266Especially 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 267code you need to add to get I<this> policy is daunting. Nobody would write
229this out in throwaway scripts, commandline hacks or in quick internal-use 268this out in throwaway scripts, commandline hacks or in quick internal-use
230scripts. 269scripts.
231 270
232By using common::sense you get a defined set of policies (ours, but maybe 271By 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 272yours, too, if you accept them), and they are easy to apply to your
250 289
251The money/time/effort/electricity invested in these gigabytes (probably 290The money/time/effort/electricity invested in these gigabytes (probably
252petabytes globally!) of wasted memory could easily save 42 trees, and a 291petabytes globally!) of wasted memory could easily save 42 trees, and a
253kitten! 292kitten!
254 293
255Unfortunately, until everybods applies more common sense, there will still 294Unfortunately, until everybody applies more common sense, there will still
256often be modules that pull in the monster pragmas. But one can hope... 295often be modules that pull in the monster pragmas. But one can hope...
257 296
258=cut 297=cut
259 298
260package common::sense; 299package common::sense;
261 300
262our $VERSION = '3.2'; 301our $VERSION = '3.6';
263 302
264# overload should be included 303# overload should be included
265 304
266sub import { 305sub import {
306 local $^W; # work around perl 5.16 spewing out warnings for next statement
267IMPORT 307IMPORT
268 # use feature
269 $^H{feature_switch} =
270 $^H{feature_say} =
271 $^H{feature_state} = 1;
272} 308}
273 309
2741; 3101;
275 311
276=back 312=back
379ew73 415ew73
380 416
381 "... I never got past the SYNOPSIS before calling it shit." 417 "... I never got past the SYNOPSIS before calling it shit."
382 [...] 418 [...]
383 How come no one ever quotes me. :(" 419 How come no one ever quotes me. :("
420
421chip (not willing to explain his cryptic questions about links in Changes files)
422
423 "I'm willing to ask the question I've asked. I'm not willing to go
424 through the whole dance you apparently have choreographed. Either
425 answer the completely obvious question, or tell me to fuck off again."
384 426
385=head1 FREQUENTLY ASKED QUESTIONS 427=head1 FREQUENTLY ASKED QUESTIONS
386 428
387Or frequently-come-up confusions. 429Or frequently-come-up confusions.
388 430
426usefulness of some warnings over others. This module is aimed at 468usefulness of some warnings over others. This module is aimed at
427experienced Perl programmers, not people migrating from other languages 469experienced Perl programmers, not people migrating from other languages
428who might be surprised about stuff such as C<undef>. On the other hand, 470who 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 471this 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 472to its strictness in enforcing policy, while at the same time not limiting
431the expresive power of perl. 473the expressive power of perl.
432 474
433This module is considerably I<more> strict than the canonical C<use 475This module is considerably I<more> strict than the canonical C<use
434strict; use warnings>, as it makes all its warnings fatal in nature, so 476strict; 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. 477you can not get away with as many things as with the canonical approach.
436 478
437This was not implemented in version 1.0 because of the daunting number 479This was not implemented in version 1.0 because of the daunting number
438of warning categories and the difficulty in getting exactly the set of 480of 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 481warnings 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 482get a specific set of warnings - it is not reasonable to put this into
441every module, the maintenance effort would be enourmous). 483every module, the maintenance effort would be enormous).
442 484
443=item But many modules C<use strict> or C<use warnings>, so the memory 485=item But many modules C<use strict> or C<use warnings>, so the memory
444savings do not apply? 486savings do not apply?
445 487
446I suddenly feel sad... 488I suddenly feel sad...

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines