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.3 by root, Sat Apr 3 15:31:16 2010 UTC vs.
Revision 1.10 by root, Sun Jun 10 19:33:12 2012 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 substr 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 substr 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
257 296
258=cut 297=cut
259 298
260package common::sense; 299package common::sense;
261 300
262our $VERSION = '3.1'; 301our $VERSION = '3.5';
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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines