1 | #! perl |
1 | #! perl-000 |
2 | |
2 | |
3 | open STDOUT, ">$ARGV[0]~" |
3 | open STDOUT, ">$ARGV[0]~" |
4 | or die "$ARGV[0]~: $!"; |
4 | or die "$ARGV[0]~: $!"; |
5 | |
5 | |
6 | our $WARN; |
6 | our $WARN; |
7 | our $H; |
7 | our $H; |
|
|
8 | our %H; |
8 | |
9 | |
9 | BEGIN { |
10 | BEGIN { |
10 | $H = $^H; |
11 | $H = $^H; |
11 | $WARN = ${^WARNING_BITS}; |
12 | $WARN = ${^WARNING_BITS}; |
12 | } |
13 | } |
13 | |
14 | |
14 | use utf8; |
15 | use utf8; |
15 | use strict qw(subs vars); |
16 | use strict qw(subs vars); |
16 | |
17 | |
|
|
18 | BEGIN { |
|
|
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 | |
17 | no warnings; |
32 | no warnings; |
18 | use warnings qw(FATAL closed threads internal debugging pack malloc portable prototype |
33 | use 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); |
21 | no warnings qw(exec newline unopened); |
36 | no warnings qw(exec newline unopened); |
22 | |
37 | |
23 | BEGIN { |
38 | BEGIN { |
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 | |
28 | while (<DATA>) { |
44 | while (<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 | |
47 | common::sense - save a tree AND a kitten, use common::sense! |
67 | common::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 | |
67 | This module implements some sane defaults for Perl programs, as defined by |
94 | This module implements some sane defaults for Perl programs, as defined by |
68 | two typical (or not so typical - use your common sense) specimens of Perl |
95 | two typical (or not so typical - use your common sense) specimens of Perl |
69 | coders. In fact, after working out details on which warnings and strict |
96 | coders. In fact, after working out details on which warnings and strict |
70 | modes to enable and make fatal, we found that we (and our code written so |
97 | modes to enable and make fatal, we found that we (and our code written so |
… | |
… | |
95 | program and there are no advantages to not reporting this, so malloc |
122 | program and there are no advantages to not reporting this, so malloc |
96 | warnings are fatal by default. |
123 | warnings are fatal by default. |
97 | |
124 | |
98 | =back |
125 | =back |
99 | |
126 | |
|
|
127 | Unfortunately, there is no fine-grained warning control in perl, so often |
|
|
128 | whole groups of useful warnings had to be excluded because of a single |
|
|
129 | useless warning (for example, perl puts an arbitrary limit on the length |
|
|
130 | of text you can match with some regexes before emitting a warning, making |
|
|
131 | the whole C<regexp> category useless). |
|
|
132 | |
100 | What follows is a more thorough discussion of what this module does, |
133 | What follows is a more thorough discussion of what this module does, |
101 | and why it does it, and what the advantages (and disadvantages) of this |
134 | and why it does it, and what the advantages (and disadvantages) of this |
102 | approach are. |
135 | approach are. |
103 | |
136 | |
104 | =head1 RATIONALE |
137 | =head1 RATIONALE |
… | |
… | |
148 | for (@{ $var->[0] }) { ... |
181 | for (@{ $var->[0] }) { ... |
149 | |
182 | |
150 | If that isn't hypocrisy! And all that from a mere program! |
183 | If 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 | |
155 | We found it annoying that we always have to enable extra features. If |
188 | We found it annoying that we always have to enable extra features. If |
156 | something breaks because it didn't anticipate future changes, so be |
189 | something breaks because it didn't anticipate future changes, so be |
157 | it. 5.10 broke almost all our XS modules and nobody cared either (or at |
190 | it. 5.10 broke almost all our XS modules and nobody cared either (or at |
158 | least I know of nobody who really complained about gratuitous changes - |
191 | least I know of nobody who really complained about gratuitous changes - |
… | |
… | |
168 | older versions of perl - common::sense of course works there as well. |
201 | older versions of perl - common::sense of course works there as well. |
169 | There is also an important other mode where having additional features by |
202 | There is also an important other mode where having additional features by |
170 | default is useful: commandline hacks and internal use scripts: See "much |
203 | default is useful: commandline hacks and internal use scripts: See "much |
171 | reduced typing", below. |
204 | reduced typing", below. |
172 | |
205 | |
|
|
206 | There is one notable exception: C<unicode_eval> is not enabled by |
|
|
207 | default. In our opinion, C<use feature> had one main effect - newer perl |
|
|
208 | versions don't value backwards compatibility and the ability to write |
|
|
209 | modules for multiple perl versions much, after all, you can use feature. |
|
|
210 | |
|
|
211 | C<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 | |
176 | Ah, the dreaded warnings. Even worse, the horribly dreaded C<-w> |
215 | Ah, the dreaded warnings. Even worse, the horribly dreaded C<-w> |
177 | switch: Even though we don't care if other people use warnings (and |
216 | switch: Even though we don't care if other people use warnings (and |
… | |
… | |
192 | favourable way, calling it outright "wrong"), but standard utilities, such |
231 | favourable way, calling it outright "wrong"), but standard utilities, such |
193 | as L<prove>, or MakeMaker when running C<make test>, still enable them |
232 | as L<prove>, or MakeMaker when running C<make test>, still enable them |
194 | blindly. |
233 | blindly. |
195 | |
234 | |
196 | For version 2 of common::sense, we finally sat down a few hours and went |
235 | For version 2 of common::sense, we finally sat down a few hours and went |
197 | through I<every single warning message>, identifiying - according to |
236 | through I<every single warning message>, identifying - according to |
198 | common sense - all the useful ones. |
237 | common sense - all the useful ones. |
199 | |
238 | |
200 | This resulted in the rather impressive list in the SYNOPSIS. When we |
239 | This resulted in the rather impressive list in the SYNOPSIS. When we |
201 | weren't sure, we didn't include the warning, so the list might grow in |
240 | weren't sure, we didn't include the warning, so the list might grow in |
202 | the future (we might have made a mistake, too, so the list might shrink |
241 | the future (we might have made a mistake, too, so the list might shrink |
… | |
… | |
211 | warnings>, which is good if you are into strict things (we are not, |
250 | warnings>, which is good if you are into strict things (we are not, |
212 | actually, but these things tend to be subjective). |
251 | actually, but these things tend to be subjective). |
213 | |
252 | |
214 | After deciding on the list, we ran the module against all of our code that |
253 | After deciding on the list, we ran the module against all of our code that |
215 | uses C<common::sense> (that is almost all of our code), and found only one |
254 | uses C<common::sense> (that is almost all of our code), and found only one |
216 | occurence where one of them caused a problem: one of elmex's (unreleased) |
255 | occurrence where one of them caused a problem: one of elmex's (unreleased) |
217 | modules contained: |
256 | modules 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 | |
221 | We quickly agreed that indeed the code should be changed, even though it |
260 | We 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 | |
227 | Especially with version 2.0 of common::sense, the amount of boilerplate |
266 | Especially with version 2.0 of common::sense, the amount of boilerplate |
228 | code you need to add to gte I<this> policy is daunting. Nobody would write |
267 | code you need to add to get I<this> policy is daunting. Nobody would write |
229 | this out in throwaway scripts, commandline hacks or in quick internal-use |
268 | this out in throwaway scripts, commandline hacks or in quick internal-use |
230 | scripts. |
269 | scripts. |
231 | |
270 | |
232 | By using common::sense you get a defined set of policies (ours, but maybe |
271 | By using common::sense you get a defined set of policies (ours, but maybe |
233 | yours, too, if you accept them), and they are easy to apply to your |
272 | yours, too, if you accept them), and they are easy to apply to your |
… | |
… | |
250 | |
289 | |
251 | The money/time/effort/electricity invested in these gigabytes (probably |
290 | The money/time/effort/electricity invested in these gigabytes (probably |
252 | petabytes globally!) of wasted memory could easily save 42 trees, and a |
291 | petabytes globally!) of wasted memory could easily save 42 trees, and a |
253 | kitten! |
292 | kitten! |
254 | |
293 | |
255 | Unfortunately, until everybods applies more common sense, there will still |
294 | Unfortunately, until everybody applies more common sense, there will still |
256 | often be modules that pull in the monster pragmas. But one can hope... |
295 | often be modules that pull in the monster pragmas. But one can hope... |
257 | |
296 | |
258 | =cut |
297 | =cut |
259 | |
298 | |
260 | package common::sense; |
299 | package common::sense; |
261 | |
300 | |
262 | our $VERSION = '3.2'; |
301 | our $VERSION = '3.6'; |
263 | |
302 | |
264 | # overload should be included |
303 | # overload should be included |
265 | |
304 | |
266 | sub import { |
305 | sub import { |
|
|
306 | local $^W; # work around perl 5.16 spewing out warnings for next statement |
267 | IMPORT |
307 | IMPORT |
268 | # use feature |
|
|
269 | $^H{feature_switch} = |
|
|
270 | $^H{feature_say} = |
|
|
271 | $^H{feature_state} = 1; |
|
|
272 | } |
308 | } |
273 | |
309 | |
274 | 1; |
310 | 1; |
275 | |
311 | |
276 | =back |
312 | =back |
… | |
… | |
379 | ew73 |
415 | ew73 |
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 | |
|
|
421 | chip (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 | |
387 | Or frequently-come-up confusions. |
429 | Or frequently-come-up confusions. |
388 | |
430 | |
… | |
… | |
426 | usefulness of some warnings over others. This module is aimed at |
468 | usefulness of some warnings over others. This module is aimed at |
427 | experienced Perl programmers, not people migrating from other languages |
469 | experienced Perl programmers, not people migrating from other languages |
428 | who might be surprised about stuff such as C<undef>. On the other hand, |
470 | who might be surprised about stuff such as C<undef>. On the other hand, |
429 | this does not exclude the usefulness of this module for total newbies, due |
471 | this does not exclude the usefulness of this module for total newbies, due |
430 | to its strictness in enforcing policy, while at the same time not limiting |
472 | to its strictness in enforcing policy, while at the same time not limiting |
431 | the expresive power of perl. |
473 | the expressive power of perl. |
432 | |
474 | |
433 | This module is considerably I<more> strict than the canonical C<use |
475 | This module is considerably I<more> strict than the canonical C<use |
434 | strict; use warnings>, as it makes all its warnings fatal in nature, so |
476 | strict; use warnings>, as it makes all its warnings fatal in nature, so |
435 | you can not get away with as many things as with the canonical approach. |
477 | you can not get away with as many things as with the canonical approach. |
436 | |
478 | |
437 | This was not implemented in version 1.0 because of the daunting number |
479 | This was not implemented in version 1.0 because of the daunting number |
438 | of warning categories and the difficulty in getting exactly the set of |
480 | of warning categories and the difficulty in getting exactly the set of |
439 | warnings you wish (i.e. look at the SYNOPSIS in how complicated it is to |
481 | warnings you wish (i.e. look at the SYNOPSIS in how complicated it is to |
440 | get a specific set of warnings - it is not reasonable to put this into |
482 | get a specific set of warnings - it is not reasonable to put this into |
441 | every module, the maintenance effort would be enourmous). |
483 | every 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 |
444 | savings do not apply? |
486 | savings do not apply? |
445 | |
487 | |
446 | I suddenly feel sad... |
488 | I suddenly feel sad... |