… | |
… | |
9 | # roughly the same as, with much lower memory usage: |
9 | # roughly the same as, with much lower memory usage: |
10 | # |
10 | # |
11 | # use strict qw(vars subs); |
11 | # use strict qw(vars subs); |
12 | # use feature qw(say state switch); |
12 | # use feature qw(say state switch); |
13 | # no warnings; |
13 | # no warnings; |
|
|
14 | # use warnings qw(FATAL closed threads internal debugging pack substr malloc |
|
|
15 | # unopened portable prototype inplace io pipe unpack regexp |
|
|
16 | # deprecated exiting glob digit printf utf8 layer |
|
|
17 | # reserved parenthesis taint closure semicolon); |
|
|
18 | # no warnings qw(exec newline); |
14 | |
19 | |
15 | =head1 DESCRIPTION |
20 | =head1 DESCRIPTION |
16 | |
21 | |
17 | This module implements some sane defaults for Perl programs, as defined by |
22 | This module implements some sane defaults for Perl programs, as defined by |
18 | two typical (or not so typical - use your common sense) specimens of |
23 | two typical (or not so typical - use your common sense) specimens of Perl |
19 | Perl coders. |
24 | coders. |
20 | |
25 | |
21 | =over 4 |
26 | =over 4 |
22 | |
27 | |
23 | =item use strict qw(subs vars) |
28 | =item use strict qw(subs vars) |
24 | |
29 | |
25 | Using C<use strict> is definitely common sense, but C<use strict |
30 | Using C<use strict> is definitely common sense, but C<use strict |
26 | 'refs'> definitely overshoots it's usefulness. After almost two |
31 | 'refs'> definitely overshoots its usefulness. After almost two |
27 | decades of Perl hacking, we decided that it does more harm than being |
32 | decades of Perl hacking, we decided that it does more harm than being |
28 | useful. Specifically, constructs like these: |
33 | useful. Specifically, constructs like these: |
29 | |
34 | |
30 | @{ $var->[0] } |
35 | @{ $var->[0] } |
31 | |
36 | |
32 | Must be written like this, when C<use strict 'refs'> is in scope, and |
37 | Must be written like this (or similarly), when C<use strict 'refs'> is in |
33 | C<$var> can legally be C<undef>: |
38 | scope, and C<$var> can legally be C<undef>: |
34 | |
39 | |
35 | @{ $var->[0] || [] } |
40 | @{ $var->[0] || [] } |
36 | |
41 | |
37 | This is annoying, and doesn't shield against obvious mistakes such as |
42 | This is annoying, and doesn't shield against obvious mistakes such as |
38 | using C<"">, so one would even have to write: |
43 | using C<"">, so one would even have to write (at least for the time |
|
|
44 | being): |
39 | |
45 | |
40 | @{ defined $var->[0] ? $var->[0] : [] } |
46 | @{ defined $var->[0] ? $var->[0] : [] } |
41 | |
47 | |
42 | ... which nobody with a bit of common sense would consider |
48 | ... which nobody with a bit of common sense would consider |
|
|
49 | writing. |
|
|
50 | |
43 | writing. Curiously enough, sometimes, perl is not so strict, as this works |
51 | Curiously enough, sometimes perl is not so strict, as this works even with |
44 | even with C<use strict> in scope: |
52 | C<use strict> in scope: |
45 | |
53 | |
46 | for (@{ $var->[0] }) { ... |
54 | for (@{ $var->[0] }) { ... |
47 | |
55 | |
48 | If that isnt hipocrasy! And all that from a mere program! |
56 | If that isn't hypocrisy! And all that from a mere program! |
|
|
57 | |
49 | |
58 | |
50 | =item use feature qw(say state given) |
59 | =item use feature qw(say state given) |
51 | |
60 | |
52 | We found it annoying that we always have to enable extra features. If |
61 | We found it annoying that we always have to enable extra features. If |
53 | something breaks because it didn't anticipate future changes, so be |
62 | something breaks because it didn't anticipate future changes, so be |
54 | it. 5.10 broke almost all our XS modules and nobody cared either - and few |
63 | it. 5.10 broke almost all our XS modules and nobody cared either (or at |
|
|
64 | least I know of nobody who really complained about gratuitous changes - |
|
|
65 | as opposed to bugs). |
|
|
66 | |
55 | modules that are no longer maintained work with newer versions of Perl, |
67 | Few modules that are not actively maintained work with newer versions of |
56 | regardless of use feature. |
68 | Perl, regardless of use feature or not, so a new major perl release means |
|
|
69 | changes to many modules - new keywords are just the tip of the iceberg. |
57 | |
70 | |
58 | If your code isn'talive, it's dead, jim. |
71 | If your code isn't alive, it's dead, Jim - be an active maintainer. |
59 | |
72 | |
60 | =item no warnings |
|
|
61 | |
73 | |
|
|
74 | =item no warnings, but a lot of new errors |
|
|
75 | |
62 | The dreaded warnings. Even worse, the horribly dreaded C<-w> switch. Even |
76 | Ah, the dreaded warnings. Even worse, the horribly dreaded C<-w> |
63 | though we don't care if other people use warnings (and certainly there are |
77 | switch: Even though we don't care if other people use warnings (and |
64 | useful ones), a lot of warnings simply go against the spirit of Perl, most |
78 | certainly there are useful ones), a lot of warnings simply go against the |
|
|
79 | spirit of Perl. |
|
|
80 | |
65 | prominently, the warnings related to C<undef>. There is nothing wrong with |
81 | Most prominently, the warnings related to C<undef>. There is nothing wrong |
66 | C<undef>: it has well-defined semantics, it is useful, and spitting out |
82 | with C<undef>: it has well-defined semantics, it is useful, and spitting |
67 | warnings you never asked for is just evil. |
83 | out warnings you never asked for is just evil. |
68 | |
84 | |
69 | So every module needs C<no warnings> to avoid somebody accidentally using |
85 | The result was that every one of our modules did C<no warnings> in the |
70 | C<-w> and forcing his bad standards on our code. No will do. |
86 | past, to avoid somebody accidentally using and forcing his bad standards |
|
|
87 | on our code. Of course, this switched off all warnings, even the useful |
|
|
88 | ones. Not a good situation. Really, the C<-w> switch should only enable |
|
|
89 | warnings for the main program only. |
71 | |
90 | |
72 | (Also, why isn't this a C<use feature> switch? Adding warnings is |
91 | Funnily enough, L<perllexwarn> explicitly mentions C<-w> (and not in a |
73 | apparently considered O.K., even if it breaks your programs). |
92 | favourable way, calling it outright "wrong"), but standard utilities, such |
|
|
93 | as L<prove>, or MakeMaker when running C<make test>, still enable them |
|
|
94 | blindly. |
74 | |
95 | |
75 | =item much less memory |
96 | For version 2 of common::sense, we finally sat down a few hours and went |
|
|
97 | through I<every single warning message>, identifiying - according to |
|
|
98 | common sense - all the useful ones. |
76 | |
99 | |
77 | Just using all those pragmas together waste <blink>I<< B<776> kilobytes |
100 | This resulted in the rather impressive list in the SYNOPSIS. When we |
78 | >></blink> of precious memory in my perl, for I<every single perl process |
101 | weren't sure, we didn't include the warning, so the list might grow in |
79 | using our code>, which on our machines, is a lot. |
102 | the future (we might have made a mistake, too, so the list might shrink |
|
|
103 | as well). |
|
|
104 | |
|
|
105 | Note the presence of C<FATAL> in the list: we do not think that the |
|
|
106 | conditions caught by these warnings are worthy of a warning, we I<insist> |
|
|
107 | that they are worthy of I<stopping> your program, I<instantly>. They are |
|
|
108 | I<bugs>! |
|
|
109 | |
|
|
110 | Therefore we consider C<common::sense> to be much stricter than C<use |
|
|
111 | warnings>, which is good if you are into strict things (we are not, |
|
|
112 | actually, but these things tend to be subjective). |
|
|
113 | |
|
|
114 | After deciding on the list, we ran the module against all of our code that |
|
|
115 | uses C<common::sense> (that is almost all of our code), and found only one |
|
|
116 | occurence where one of them caused a problem: one of elmex's (unreleased) |
|
|
117 | modules contained: |
|
|
118 | |
|
|
119 | $fmt =~ s/([^\s\[]*)\[( [^\]]* )\]/\x0$1\x1$2\x0/xgo; |
|
|
120 | |
|
|
121 | We quickly agreed that indeed the code should be changed, even though it |
|
|
122 | happened to do the right thing when the warning was switched off. |
|
|
123 | |
|
|
124 | |
|
|
125 | =item mucho reduced memory usage |
|
|
126 | |
|
|
127 | Just using all those pragmas mentioned in the SYNOPSIS together wastes |
|
|
128 | <blink>I<< B<776> kilobytes >></blink> of precious memory in my perl, for |
|
|
129 | I<every single perl process using our code>, which on our machines, is a |
|
|
130 | lot. In comparison, this module only uses I<< B<four> >> kilobytes (I even |
|
|
131 | had to write it out so it looks like more) of memory on the same platform. |
80 | |
132 | |
81 | The money/time/effort/electricity invested in these gigabytes (probably |
133 | The money/time/effort/electricity invested in these gigabytes (probably |
82 | petabytes globally!) of wasted memory could easily save 42 trees, and a |
134 | petabytes globally!) of wasted memory could easily save 42 trees, and a |
83 | kitten! |
135 | kitten! |
84 | |
136 | |
|
|
137 | Unfortunately, until everybods applies more common sense, there will still |
|
|
138 | often be modules that pull in the monster pragmas. But one can hope... |
|
|
139 | |
85 | =cut |
140 | =cut |
86 | |
141 | |
87 | package Async::Interrupt; |
142 | package common::sense; |
88 | |
143 | |
|
|
144 | our $VERSION = '2.0'; |
|
|
145 | |
|
|
146 | # paste this into pelr to find bitmask |
|
|
147 | |
89 | no warnings; |
148 | # no warnings; |
|
|
149 | # use warnings qw(FATAL closed threads internal debugging pack substr malloc unopened portable prototype |
|
|
150 | # inplace io pipe unpack regexp deprecated exiting glob digit printf |
|
|
151 | # utf8 layer reserved parenthesis taint closure semicolon); |
|
|
152 | # no warnings qw(exec newline); |
|
|
153 | # BEGIN { warn join "", map "\\x$_", unpack "(H2)*", ${^WARNING_BITS}; exit 0 }; |
90 | |
154 | |
91 | BEGIN { |
155 | # overload should be included |
92 | $VERSION = '0.03'; |
|
|
93 | |
156 | |
94 | require XSLoader; |
157 | sub import { |
95 | XSLoader::load Async::Interrupt::, $VERSION; |
158 | # verified with perl 5.8.0, 5.10.0 |
|
|
159 | ${^WARNING_BITS} = "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03"; |
|
|
160 | |
|
|
161 | # use strict vars subs |
|
|
162 | $^H |= 0x00000600; |
|
|
163 | |
|
|
164 | # use feature |
|
|
165 | $^H{feature_switch} = |
|
|
166 | $^H{feature_say} = |
|
|
167 | $^H{feature_state} = 1; |
96 | } |
168 | } |
97 | |
169 | |
98 | our $DIED = sub { warn "$@" }; |
|
|
99 | |
|
|
100 | =cut |
|
|
101 | |
|
|
102 | 1; |
170 | 1; |
103 | |
171 | |
104 | =back |
172 | =back |
105 | |
173 | |
106 | =head1 EXAMPLE |
174 | =head1 THERE IS NO 'no common::sense'!!!! !!!! !! |
107 | |
175 | |
108 | There really should be a complete C/XS example. Bug me about it. |
176 | This module doesn't offer an unimport. First of all, it wastes even more |
|
|
177 | memory, second, and more importantly, who with even a bit of common sense |
|
|
178 | would want no common sense? |
109 | |
179 | |
110 | =head1 IMPLEMENTATION DETAILS AND LIMITATIONS |
180 | =head1 STABILITY AND FUTURE VERSIONS |
111 | |
181 | |
112 | This module works by "hijacking" SIGKILL, which is guaranteed to be always |
182 | Future versions might change just about everything in this module. We |
113 | available in perl, but also cannot be caught, so is always available. |
183 | might test our modules and upload new ones working with newer versions of |
|
|
184 | this module, and leave you standing in the rain because we didn't tell |
|
|
185 | you. In fact, we did so when switching from 1.0 to 2.0, which enabled gobs |
|
|
186 | of warnings, and made them FATAL on top. |
114 | |
187 | |
115 | Basically, this module fakes the receive of a SIGKILL signal and |
188 | Maybe we will load some nifty modules that try to emulate C<say> or so |
116 | then catches it. This makes normal signal handling slower (probably |
189 | with perls older than 5.10 (this module, of course, should work with older |
117 | unmeasurably), but has the advantage of not requiring a special runops nor |
190 | perl versions - supporting 5.8 for example is just common sense at this |
118 | slowing down normal perl execution a bit. |
191 | time. Maybe not in the future, but of course you can trust our common |
|
|
192 | sense to be consistent with, uhm, our opinion). |
119 | |
193 | |
120 | It assumes that C<sig_atomic_t> and C<int> are both exception-safe to |
194 | =head1 WHAT OTHER PEOPLE HAD TO SAY ABOUT THIS MODULE |
121 | modify (C<sig_atomic_> is used by this module, and perl itself uses |
195 | |
122 | C<int>, so we can assume that this is quite portable, at least w.r.t. |
196 | apeiron |
123 | signals). |
197 | |
|
|
198 | "... wow" |
|
|
199 | "I hope common::sense is a joke." |
|
|
200 | |
|
|
201 | crab |
|
|
202 | |
|
|
203 | "i wonder how it would be if joerg schilling wrote perl modules." |
|
|
204 | |
|
|
205 | H.Merijn Brand |
|
|
206 | |
|
|
207 | "Just one more reason to drop JSON::XS from my distribution list" |
|
|
208 | |
|
|
209 | Pista Palo |
|
|
210 | |
|
|
211 | "Something in short supply these days..." |
|
|
212 | |
|
|
213 | Steffen Schwigon |
|
|
214 | |
|
|
215 | "This module is quite for sure *not* just a repetition of all the other |
|
|
216 | 'use strict, use warnings'-approaches, and it's also not the opposite. |
|
|
217 | [...] And for its chosen middle-way it's also not the worst name ever. |
|
|
218 | And everything is documented." |
|
|
219 | |
|
|
220 | BKB |
|
|
221 | |
|
|
222 | "[Deleted - thanks to Steffen Schwigon for pointing out this review was |
|
|
223 | in error.]" |
|
|
224 | |
|
|
225 | Somni |
|
|
226 | |
|
|
227 | "the arrogance of the guy" |
|
|
228 | "I swear he tacked somenoe else's name onto the module |
|
|
229 | just so he could use the royal 'we' in the documentation" |
|
|
230 | |
|
|
231 | dngor |
|
|
232 | |
|
|
233 | "Heh. '"<elmex at ta-sa.org>"' The quotes are semantic |
|
|
234 | distancing from that e-mail address." |
|
|
235 | |
|
|
236 | Jerad Pierce |
|
|
237 | |
|
|
238 | "Awful name (not a proper pragma), and the SYNOPSIS doesn't tell you |
|
|
239 | anything either. Nor is it clear what features have to do with "common |
|
|
240 | sense" or discipline." |
|
|
241 | |
|
|
242 | acme |
|
|
243 | |
|
|
244 | "THERE IS NO 'no common::sense'!!!! !!!! !!" |
|
|
245 | |
|
|
246 | apeiron (meta-comment about us commenting^Wquoting his comment) |
|
|
247 | |
|
|
248 | How about quoting this: get a clue, you fucktarded amoeba. |
124 | |
249 | |
125 | =head1 AUTHOR |
250 | =head1 AUTHOR |
126 | |
251 | |
127 | Marc Lehmann <schmorp@schmorp.de> |
252 | Marc Lehmann <schmorp@schmorp.de> |
128 | http://home.schmorp.de/ |
253 | http://home.schmorp.de/ |
129 | |
254 | |
|
|
255 | Robin Redeker, "<elmex at ta-sa.org>". |
|
|
256 | |
130 | =cut |
257 | =cut |
131 | |
258 | |