… | |
… | |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use common::sense; |
7 | use common::sense; |
8 | |
8 | |
9 | # roughly the same as, with much lower memory usage: |
9 | # supposed to be the same, with much lower memory usage, as: |
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 no warnings |
|
|
24 | |
|
|
25 | The dreaded warnings. Even worse, the horribly dreaded C<-w> switch. Even |
|
|
26 | though we don't care if other people use warnings (and certainly there are |
|
|
27 | useful ones), a lot of warnings simply go against the spirit of Perl, most |
|
|
28 | prominently, the warnings related to C<undef>. There is nothing wrong with |
|
|
29 | C<undef>: it has well-defined semantics, it is useful, and spitting out |
|
|
30 | warnings you never asked for is just evil. |
|
|
31 | |
|
|
32 | So every module needs C<no warnings> to avoid somebody accidentally using |
|
|
33 | C<-w> and forcing his bad standards on our code. No will do. |
|
|
34 | |
|
|
35 | Funnily enough, L<perllexwarn> explicitly mentions C<-w> (and not in a |
|
|
36 | favourable way), but standard utilities, such as L<prove>, or MakeMaker |
|
|
37 | when running C<make test> enable them blindly. |
|
|
38 | |
|
|
39 | =item use strict qw(subs vars) |
28 | =item use strict qw(subs vars) |
40 | |
29 | |
41 | 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 |
42 | 'refs'> definitely overshoots it's usefulness. After almost two |
31 | 'refs'> definitely overshoots its usefulness. After almost two |
43 | 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 |
44 | useful. Specifically, constructs like these: |
33 | useful. Specifically, constructs like these: |
45 | |
34 | |
46 | @{ $var->[0] } |
35 | @{ $var->[0] } |
47 | |
36 | |
… | |
… | |
49 | scope, and C<$var> can legally be C<undef>: |
38 | scope, and C<$var> can legally be C<undef>: |
50 | |
39 | |
51 | @{ $var->[0] || [] } |
40 | @{ $var->[0] || [] } |
52 | |
41 | |
53 | 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 |
54 | 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): |
55 | |
45 | |
56 | @{ defined $var->[0] ? $var->[0] : [] } |
46 | @{ defined $var->[0] ? $var->[0] : [] } |
57 | |
47 | |
58 | ... which nobody with a bit of common sense would consider |
48 | ... which nobody with a bit of common sense would consider |
|
|
49 | writing: clear code is clearly something else. |
|
|
50 | |
59 | 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 |
60 | even with C<use strict> in scope: |
52 | C<use strict> in scope: |
61 | |
53 | |
62 | for (@{ $var->[0] }) { ... |
54 | for (@{ $var->[0] }) { ... |
63 | |
55 | |
64 | 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 | |
65 | |
58 | |
66 | =item use feature qw(say state given) |
59 | =item use feature qw(say state given) |
67 | |
60 | |
68 | 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 |
69 | something breaks because it didn't anticipate future changes, so be |
62 | something breaks because it didn't anticipate future changes, so be |
70 | 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 | |
71 | 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 |
72 | 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. |
73 | |
70 | |
74 | If your code isn't alive, it's dead, jim. |
71 | If your code isn't alive, it's dead, Jim - be an active maintainer. |
75 | |
72 | |
76 | =item much less memory |
|
|
77 | |
73 | |
78 | Just using all those pragmas together waste <blink>I<< B<776> kilobytes |
74 | =item no warnings, but a lot of new errors |
79 | >></blink> of precious memory in my perl, for I<every single perl process |
75 | |
80 | using our code>, which on our machines, is a lot. In comparison, this |
76 | Ah, the dreaded warnings. Even worse, the horribly dreaded C<-w> |
81 | module only uses I<< B<four> >> kilobytes (I even had to write it out so |
77 | switch: Even though we don't care if other people use warnings (and |
|
|
78 | certainly there are useful ones), a lot of warnings simply go against the |
|
|
79 | spirit of Perl. |
|
|
80 | |
|
|
81 | Most prominently, the warnings related to C<undef>. There is nothing wrong |
|
|
82 | with C<undef>: it has well-defined semantics, it is useful, and spitting |
|
|
83 | out warnings you never asked for is just evil. |
|
|
84 | |
|
|
85 | The result was that every one of our modules did C<no warnings> in the |
|
|
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. |
|
|
90 | |
|
|
91 | Funnily enough, L<perllexwarn> explicitly mentions C<-w> (and not in a |
|
|
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. |
|
|
95 | |
|
|
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. |
|
|
99 | |
|
|
100 | This resulted in the rather impressive list in the SYNOPSIS. When we |
|
|
101 | weren't sure, we didn't include the warning, so the list might grow in |
|
|
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 |
82 | it looks like more) of memory on the same platform. |
131 | had to write it out so it looks like more) of memory on the same platform. |
83 | |
132 | |
84 | The money/time/effort/electricity invested in these gigabytes (probably |
133 | The money/time/effort/electricity invested in these gigabytes (probably |
85 | 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 |
86 | kitten! |
135 | kitten! |
87 | |
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 | |
88 | =cut |
140 | =cut |
89 | |
141 | |
90 | package common::sense; |
142 | package common::sense; |
91 | |
143 | |
92 | our $VERSION = '0.03'; |
144 | our $VERSION = '2.01'; |
|
|
145 | |
|
|
146 | # paste this into perl to find bitmask |
|
|
147 | |
|
|
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 }; |
|
|
154 | |
|
|
155 | # overload should be included |
93 | |
156 | |
94 | sub import { |
157 | sub import { |
95 | # no warnings |
158 | # verified with perl 5.8.0, 5.10.0 |
96 | ${^WARNING_BITS} ^= ${^WARNING_BITS}; |
159 | ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03"; |
97 | |
160 | |
98 | # use strict vars subs |
161 | # use strict vars subs |
99 | $^H |= 0x00000600; |
162 | $^H |= 0x00000600; |
100 | |
163 | |
101 | # use feature |
164 | # use feature |
… | |
… | |
106 | |
169 | |
107 | 1; |
170 | 1; |
108 | |
171 | |
109 | =back |
172 | =back |
110 | |
173 | |
111 | =head1 NO 'no common::sense' |
174 | =head1 THERE IS NO 'no common::sense'!!!! !!!! !! |
112 | |
175 | |
113 | This module doesn't offer an unimport. First of all, it wastes even more |
176 | This module doesn't offer an unimport. First of all, it wastes even more |
114 | memory, second, and more importantly, who with even a bit of common sense |
177 | memory, second, and more importantly, who with even a bit of common sense |
115 | would want no common sense? |
178 | would want no common sense? |
116 | |
179 | |
|
|
180 | =head1 STABILITY AND FUTURE VERSIONS |
|
|
181 | |
|
|
182 | Future versions might change just about everything in this module. We |
|
|
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. |
|
|
187 | |
|
|
188 | Maybe we will load some nifty modules that try to emulate C<say> or so |
|
|
189 | with perls older than 5.10 (this module, of course, should work with older |
|
|
190 | perl versions - supporting 5.8 for example is just common sense at this |
|
|
191 | time. Maybe not in the future, but of course you can trust our common |
|
|
192 | sense to be consistent with, uhm, our opinion). |
|
|
193 | |
|
|
194 | =head1 WHAT OTHER PEOPLE HAD TO SAY ABOUT THIS MODULE |
|
|
195 | |
|
|
196 | apeiron |
|
|
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 | Adam Kennedy |
|
|
206 | |
|
|
207 | "Very interesting, efficient, and potentially something I'd use all the time." |
|
|
208 | [...] |
|
|
209 | "So no common::sense for me, alas." |
|
|
210 | |
|
|
211 | H.Merijn Brand |
|
|
212 | |
|
|
213 | "Just one more reason to drop JSON::XS from my distribution list" |
|
|
214 | |
|
|
215 | Pista Palo |
|
|
216 | |
|
|
217 | "Something in short supply these days..." |
|
|
218 | |
|
|
219 | Steffen Schwigon |
|
|
220 | |
|
|
221 | "This module is quite for sure *not* just a repetition of all the other |
|
|
222 | 'use strict, use warnings'-approaches, and it's also not the opposite. |
|
|
223 | [...] And for its chosen middle-way it's also not the worst name ever. |
|
|
224 | And everything is documented." |
|
|
225 | |
|
|
226 | BKB |
|
|
227 | |
|
|
228 | "[Deleted - thanks to Steffen Schwigon for pointing out this review was |
|
|
229 | in error.]" |
|
|
230 | |
|
|
231 | Somni |
|
|
232 | |
|
|
233 | "the arrogance of the guy" |
|
|
234 | "I swear he tacked somenoe else's name onto the module |
|
|
235 | just so he could use the royal 'we' in the documentation" |
|
|
236 | |
|
|
237 | dngor |
|
|
238 | |
|
|
239 | "Heh. '"<elmex at ta-sa.org>"' The quotes are semantic |
|
|
240 | distancing from that e-mail address." |
|
|
241 | |
|
|
242 | Jerad Pierce |
|
|
243 | |
|
|
244 | "Awful name (not a proper pragma), and the SYNOPSIS doesn't tell you |
|
|
245 | anything either. Nor is it clear what features have to do with "common |
|
|
246 | sense" or discipline." |
|
|
247 | |
|
|
248 | acme |
|
|
249 | |
|
|
250 | "THERE IS NO 'no common::sense'!!!! !!!! !!" |
|
|
251 | |
|
|
252 | apeiron (meta-comment about us commenting^Wquoting his comment) |
|
|
253 | |
|
|
254 | How about quoting this: get a clue, you fucktarded amoeba. |
|
|
255 | |
|
|
256 | =head1 FREQUQNTLY ASKED QUESTIONS |
|
|
257 | |
|
|
258 | Or frequently-come-up confusions. |
|
|
259 | |
|
|
260 | =over 4 |
|
|
261 | |
|
|
262 | =item Is this module meant to be serious? |
|
|
263 | |
|
|
264 | Yes, we would have put it under the C<Acme::> namespace otherwise. |
|
|
265 | |
|
|
266 | =item But the manpage is written in a funny/stupid/... way? |
|
|
267 | |
|
|
268 | This was meant to make it clear that our common sense is a subjective |
|
|
269 | thing and other people can use their own notions, taking the steam out |
|
|
270 | of anybody who might be offended (as some people are always offended no |
|
|
271 | matter what you do). |
|
|
272 | |
|
|
273 | This was a failure. |
|
|
274 | |
|
|
275 | But we hope the manpage still is somewhat entertaining even though it |
|
|
276 | explains boring rationale. |
|
|
277 | |
|
|
278 | =item Why do you impose your conventions on my code? |
|
|
279 | |
|
|
280 | For some reason people keep thinking that C<common::sense> imposes |
|
|
281 | process-wide limits, even though the SYNOPSIS makes it clear that it works |
|
|
282 | like other similar modules - only on the scope that uses them. |
|
|
283 | |
|
|
284 | So, no, we don't - nobody is forced to use this module, and using a module |
|
|
285 | that relies on common::sense does not impose anything on you. |
|
|
286 | |
|
|
287 | =item Why do you think only your notion of common::sense is valid? |
|
|
288 | |
|
|
289 | Well, we don't, and have clearly written this in the documentation to |
|
|
290 | every single release. We were just faster than anybody else w.r.t. to |
|
|
291 | grabbing the namespace. |
|
|
292 | |
|
|
293 | =item But everybody knows that you have to use strict and use warnings, |
|
|
294 | why do you disable them? |
|
|
295 | |
|
|
296 | Well, we don't do this either - we selectively disagree with the |
|
|
297 | usefulness of some warnings over others. This module is aimed at |
|
|
298 | experienced Perl programmers, not people migrating from other languages |
|
|
299 | who might be surprised about stuff such as C<undef>. |
|
|
300 | |
|
|
301 | In fact, this module is considerably I<more> strict than the canonical |
|
|
302 | C<use strict; use warnings>, as it makes all warnings fatal in nature, so |
|
|
303 | you can get away with as many things as with the canonical approach. |
|
|
304 | |
|
|
305 | This was not implemented in version 1.0 because of the daunting number |
|
|
306 | of warning categories and the difficulty in getting exactly the set of |
|
|
307 | warnings you wish (i.e. look at the SYNOPSIS in how complicated it is to |
|
|
308 | get a specific set of warnings - it is not reasonable to put this into |
|
|
309 | every module, the maintainance effort would be enourmous). |
|
|
310 | |
|
|
311 | =item But many modules C<use strict> or C<use warnings>, so the memory |
|
|
312 | savings do not apply? |
|
|
313 | |
|
|
314 | I am suddenly so sad. |
|
|
315 | |
|
|
316 | But yes, that's true. Fortunately C<common::sense> still uses only a |
|
|
317 | miniscule amount of RAM. |
|
|
318 | |
|
|
319 | =item But it adds another dependency to your modules! |
|
|
320 | |
|
|
321 | It's a fact, yeah. But it's trivial to install, most popular modules have |
|
|
322 | many more dependencies and we consider dependencies a good thing - it |
|
|
323 | leads to better APIs, more thought about interworking of modules and so |
|
|
324 | on. |
|
|
325 | |
|
|
326 | =item But! But! |
|
|
327 | |
|
|
328 | Yeah, we know. |
|
|
329 | |
|
|
330 | =back |
|
|
331 | |
117 | =head1 AUTHOR |
332 | =head1 AUTHOR |
118 | |
333 | |
119 | Marc Lehmann <schmorp@schmorp.de> |
334 | Marc Lehmann <schmorp@schmorp.de> |
120 | http://home.schmorp.de/ |
335 | http://home.schmorp.de/ |
121 | |
336 | |
122 | Robin Redeker, "<elmex at ta-sa.org>". |
337 | Robin Redeker, "<elmex at ta-sa.org>". |
123 | |
338 | |
124 | |
|
|
125 | =cut |
339 | =cut |
126 | |
340 | |