1 |
=head1 NAME |
2 |
|
3 |
common::sense - save a tree AND a kitten, use common::sense! |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use common::sense; |
8 |
|
9 |
# Supposed to be mostly the same, with much lower memory usage, as: |
10 |
|
11 |
# use utf8; |
12 |
# use strict qw(vars subs); |
13 |
# use feature qw(say state switch); |
14 |
# use feature qw(unicode_strings unicode_eval current_sub fc evalbytes); |
15 |
# no feature qw(array_base); |
16 |
# no warnings; |
17 |
# use warnings qw(FATAL closed threads internal debugging pack |
18 |
# prototype inplace io pipe unpack malloc glob |
19 |
# digit printf layer reserved taint closure semicolon); |
20 |
# no warnings qw(exec newline unopened); |
21 |
|
22 |
=head1 DESCRIPTION |
23 |
|
24 |
“Nothing is more fairly distributed than common sense: no one thinks |
25 |
he needs more of it than he already has.” |
26 |
|
27 |
– René Descartes |
28 |
|
29 |
This module implements some sane defaults for Perl programs, as defined by |
30 |
two typical (or not so typical - use your common sense) specimens of Perl |
31 |
coders. In fact, after working out details on which warnings and strict |
32 |
modes to enable and make fatal, we found that we (and our code written so |
33 |
far, and others) fully agree on every option, even though we never used |
34 |
warnings before, so it seems this module indeed reflects a "common" sense |
35 |
among some long-time Perl coders. |
36 |
|
37 |
The basic philosophy behind the choices made in common::sense can be |
38 |
summarised as: "enforcing strict policies to catch as many bugs as |
39 |
possible, while at the same time, not limiting the expressive power |
40 |
available to the programmer". |
41 |
|
42 |
Two typical examples of how this philosophy is applied in practise is the |
43 |
handling of uninitialised and malloc warnings: |
44 |
|
45 |
=over 4 |
46 |
|
47 |
=item I<uninitialised> |
48 |
|
49 |
C<undef> is a well-defined feature of perl, and enabling warnings for |
50 |
using it rarely catches any bugs, but considerably limits you in what you |
51 |
can do, so uninitialised warnings are disabled. |
52 |
|
53 |
=item I<malloc> |
54 |
|
55 |
Freeing something twice on the C level is a serious bug, usually causing |
56 |
memory corruption. It often leads to side effects much later in the |
57 |
program and there are no advantages to not reporting this, so malloc |
58 |
warnings are fatal by default. |
59 |
|
60 |
=back |
61 |
|
62 |
Unfortunately, there is no fine-grained warning control in perl, so often |
63 |
whole groups of useful warnings had to be excluded because of a single |
64 |
useless warning (for example, perl puts an arbitrary limit on the length |
65 |
of text you can match with some regexes before emitting a warning, making |
66 |
the whole C<regexp> category useless). |
67 |
|
68 |
What follows is a more thorough discussion of what this module does, |
69 |
and why it does it, and what the advantages (and disadvantages) of this |
70 |
approach are. |
71 |
|
72 |
=head1 RATIONALE |
73 |
|
74 |
=over 4 |
75 |
|
76 |
=item use utf8 |
77 |
|
78 |
While it's not common sense to write your programs in UTF-8, it's quickly |
79 |
becoming the most common encoding, is the designated future default |
80 |
encoding for perl sources, and the most convenient encoding available |
81 |
(you can do really nice quoting tricks...). Experience has shown that our |
82 |
programs were either all pure ascii or utf-8, both of which will stay the |
83 |
same. |
84 |
|
85 |
There are few drawbacks to enabling UTF-8 source code by default (mainly |
86 |
some speed hits due to bugs in older versions of perl), so this module |
87 |
enables UTF-8 source code encoding by default. |
88 |
|
89 |
|
90 |
=item use strict qw(subs vars) |
91 |
|
92 |
Using C<use strict> is definitely common sense, but C<use strict |
93 |
'refs'> definitely overshoots its usefulness. After almost two |
94 |
decades of Perl hacking, we decided that it does more harm than being |
95 |
useful. Specifically, constructs like these: |
96 |
|
97 |
@{ $var->[0] } |
98 |
|
99 |
Must be written like this (or similarly), when C<use strict 'refs'> is in |
100 |
scope, and C<$var> can legally be C<undef>: |
101 |
|
102 |
@{ $var->[0] || [] } |
103 |
|
104 |
This is annoying, and doesn't shield against obvious mistakes such as |
105 |
using C<"">, so one would even have to write (at least for the time |
106 |
being): |
107 |
|
108 |
@{ defined $var->[0] ? $var->[0] : [] } |
109 |
|
110 |
... which nobody with a bit of common sense would consider |
111 |
writing: clear code is clearly something else. |
112 |
|
113 |
Curiously enough, sometimes perl is not so strict, as this works even with |
114 |
C<use strict> in scope: |
115 |
|
116 |
for (@{ $var->[0] }) { ... |
117 |
|
118 |
If that isn't hypocrisy! And all that from a mere program! |
119 |
|
120 |
|
121 |
=item use feature qw(say state given ...) |
122 |
|
123 |
We found it annoying that we always have to enable extra features. If |
124 |
something breaks because it didn't anticipate future changes, so be |
125 |
it. 5.10 broke almost all our XS modules and nobody cared either (or at |
126 |
least I know of nobody who really complained about gratuitous changes - |
127 |
as opposed to bugs). |
128 |
|
129 |
Few modules that are not actively maintained work with newer versions of |
130 |
Perl, regardless of use feature or not, so a new major perl release means |
131 |
changes to many modules - new keywords are just the tip of the iceberg. |
132 |
|
133 |
If your code isn't alive, it's dead, Jim - be an active maintainer. |
134 |
|
135 |
But nobody forces you to use those extra features in modules meant for |
136 |
older versions of perl - common::sense of course works there as well. |
137 |
There is also an important other mode where having additional features by |
138 |
default is useful: commandline hacks and internal use scripts: See "much |
139 |
reduced typing", below. |
140 |
|
141 |
There is one notable exception: C<unicode_eval> is not enabled by |
142 |
default. In our opinion, C<use feature> had one main effect - newer perl |
143 |
versions don't value backwards compatibility and the ability to write |
144 |
modules for multiple perl versions much, after all, you can use feature. |
145 |
|
146 |
C<unicode_eval> doesn't add a new feature, it breaks an existing function. |
147 |
|
148 |
=item no warnings, but a lot of new errors |
149 |
|
150 |
Ah, the dreaded warnings. Even worse, the horribly dreaded C<-w> |
151 |
switch: Even though we don't care if other people use warnings (and |
152 |
certainly there are useful ones), a lot of warnings simply go against the |
153 |
spirit of Perl. |
154 |
|
155 |
Most prominently, the warnings related to C<undef>. There is nothing wrong |
156 |
with C<undef>: it has well-defined semantics, it is useful, and spitting |
157 |
out warnings you never asked for is just evil. |
158 |
|
159 |
The result was that every one of our modules did C<no warnings> in the |
160 |
past, to avoid somebody accidentally using and forcing his bad standards |
161 |
on our code. Of course, this switched off all warnings, even the useful |
162 |
ones. Not a good situation. Really, the C<-w> switch should only enable |
163 |
warnings for the main program only. |
164 |
|
165 |
Funnily enough, L<perllexwarn> explicitly mentions C<-w> (and not in a |
166 |
favourable way, calling it outright "wrong"), but standard utilities, such |
167 |
as L<prove>, or MakeMaker when running C<make test>, still enable them |
168 |
blindly. |
169 |
|
170 |
For version 2 of common::sense, we finally sat down a few hours and went |
171 |
through I<every single warning message>, identifying - according to |
172 |
common sense - all the useful ones. |
173 |
|
174 |
This resulted in the rather impressive list in the SYNOPSIS. When we |
175 |
weren't sure, we didn't include the warning, so the list might grow in |
176 |
the future (we might have made a mistake, too, so the list might shrink |
177 |
as well). |
178 |
|
179 |
Note the presence of C<FATAL> in the list: we do not think that the |
180 |
conditions caught by these warnings are worthy of a warning, we I<insist> |
181 |
that they are worthy of I<stopping> your program, I<instantly>. They are |
182 |
I<bugs>! |
183 |
|
184 |
Therefore we consider C<common::sense> to be much stricter than C<use |
185 |
warnings>, which is good if you are into strict things (we are not, |
186 |
actually, but these things tend to be subjective). |
187 |
|
188 |
After deciding on the list, we ran the module against all of our code that |
189 |
uses C<common::sense> (that is almost all of our code), and found only one |
190 |
occurrence where one of them caused a problem: one of elmex's (unreleased) |
191 |
modules contained: |
192 |
|
193 |
$fmt =~ s/([^\s\[]*)\[( [^\]]* )\]/\x0$1\x1$2\x0/xgo; |
194 |
|
195 |
We quickly agreed that indeed the code should be changed, even though it |
196 |
happened to do the right thing when the warning was switched off. |
197 |
|
198 |
|
199 |
=item much reduced typing |
200 |
|
201 |
Especially with version 2.0 of common::sense, the amount of boilerplate |
202 |
code you need to add to get I<this> policy is daunting. Nobody would write |
203 |
this out in throwaway scripts, commandline hacks or in quick internal-use |
204 |
scripts. |
205 |
|
206 |
By using common::sense you get a defined set of policies (ours, but maybe |
207 |
yours, too, if you accept them), and they are easy to apply to your |
208 |
scripts: typing C<use common::sense;> is even shorter than C<use warnings; |
209 |
use strict; use feature ...>. |
210 |
|
211 |
And you can immediately use the features of your installed perl, which |
212 |
is more difficult in code you release, but not usually an issue for |
213 |
internal-use code (downgrades of your production perl should be rare, |
214 |
right?). |
215 |
|
216 |
|
217 |
=item mucho reduced memory usage |
218 |
|
219 |
Just using all those pragmas mentioned in the SYNOPSIS together wastes |
220 |
<blink>I<< B<776> kilobytes >></blink> of precious memory in my perl, for |
221 |
I<every single perl process using our code>, which on our machines, is a |
222 |
lot. In comparison, this module only uses I<< B<four> >> kilobytes (I even |
223 |
had to write it out so it looks like more) of memory on the same platform. |
224 |
|
225 |
The money/time/effort/electricity invested in these gigabytes (probably |
226 |
petabytes globally!) of wasted memory could easily save 42 trees, and a |
227 |
kitten! |
228 |
|
229 |
Unfortunately, until everybody applies more common sense, there will still |
230 |
often be modules that pull in the monster pragmas. But one can hope... |
231 |
|
232 |
=back |
233 |
|
234 |
=head1 THERE IS NO 'no common::sense'!!!! !!!! !! |
235 |
|
236 |
This module doesn't offer an unimport. First of all, it wastes even more |
237 |
memory, second, and more importantly, who with even a bit of common sense |
238 |
would want no common sense? |
239 |
|
240 |
=head1 STABILITY AND FUTURE VERSIONS |
241 |
|
242 |
Future versions might change just about everything in this module. We |
243 |
might test our modules and upload new ones working with newer versions of |
244 |
this module, and leave you standing in the rain because we didn't tell |
245 |
you. In fact, we did so when switching from 1.0 to 2.0, which enabled gobs |
246 |
of warnings, and made them FATAL on top. |
247 |
|
248 |
Maybe we will load some nifty modules that try to emulate C<say> or so |
249 |
with perls older than 5.10 (this module, of course, should work with older |
250 |
perl versions - supporting 5.8 for example is just common sense at this |
251 |
time. Maybe not in the future, but of course you can trust our common |
252 |
sense to be consistent with, uhm, our opinion). |
253 |
|
254 |
=head1 WHAT OTHER PEOPLE HAD TO SAY ABOUT THIS MODULE |
255 |
|
256 |
apeiron |
257 |
|
258 |
"... wow" |
259 |
"I hope common::sense is a joke." |
260 |
|
261 |
crab |
262 |
|
263 |
"i wonder how it would be if joerg schilling wrote perl modules." |
264 |
|
265 |
Adam Kennedy |
266 |
|
267 |
"Very interesting, efficient, and potentially something I'd use all the time." |
268 |
[...] |
269 |
"So no common::sense for me, alas." |
270 |
|
271 |
H.Merijn Brand |
272 |
|
273 |
"Just one more reason to drop JSON::XS from my distribution list" |
274 |
|
275 |
Pista Palo |
276 |
|
277 |
"Something in short supply these days..." |
278 |
|
279 |
Steffen Schwigon |
280 |
|
281 |
"This module is quite for sure *not* just a repetition of all the other |
282 |
'use strict, use warnings'-approaches, and it's also not the opposite. |
283 |
[...] And for its chosen middle-way it's also not the worst name ever. |
284 |
And everything is documented." |
285 |
|
286 |
BKB |
287 |
|
288 |
"[Deleted - thanks to Steffen Schwigon for pointing out this review was |
289 |
in error.]" |
290 |
|
291 |
Somni |
292 |
|
293 |
"the arrogance of the guy" |
294 |
"I swear he tacked somenoe else's name onto the module |
295 |
just so he could use the royal 'we' in the documentation" |
296 |
|
297 |
Anonymous Monk |
298 |
|
299 |
"You just gotta love this thing, its got META.json!!!" |
300 |
|
301 |
dngor |
302 |
|
303 |
"Heh. '"<elmex at ta-sa.org>"' The quotes are semantic |
304 |
distancing from that e-mail address." |
305 |
|
306 |
Jerad Pierce |
307 |
|
308 |
"Awful name (not a proper pragma), and the SYNOPSIS doesn't tell you |
309 |
anything either. Nor is it clear what features have to do with "common |
310 |
sense" or discipline." |
311 |
|
312 |
acme |
313 |
|
314 |
"THERE IS NO 'no common::sense'!!!! !!!! !!" |
315 |
|
316 |
apeiron (meta-comment about us commenting^Wquoting his comment) |
317 |
|
318 |
"How about quoting this: get a clue, you fucktarded amoeba." |
319 |
|
320 |
quanth |
321 |
|
322 |
"common sense is beautiful, json::xs is fast, Anyevent, EV are fast and |
323 |
furious. I love mlehmannware ;)" |
324 |
|
325 |
apeiron |
326 |
|
327 |
"... it's mlehmann's view of what common sense is. His view of common |
328 |
sense is certainly uncommon, insofar as anyone with a clue disagrees |
329 |
with him." |
330 |
|
331 |
apeiron (another meta-comment) |
332 |
|
333 |
"apeiron wonders if his little informant is here to steal more quotes" |
334 |
|
335 |
ew73 |
336 |
|
337 |
"... I never got past the SYNOPSIS before calling it shit." |
338 |
[...] |
339 |
How come no one ever quotes me. :(" |
340 |
|
341 |
chip (not willing to explain his cryptic questions about links in Changes files) |
342 |
|
343 |
"I'm willing to ask the question I've asked. I'm not willing to go |
344 |
through the whole dance you apparently have choreographed. Either |
345 |
answer the completely obvious question, or tell me to fuck off again." |
346 |
|
347 |
=head1 FREQUENTLY ASKED QUESTIONS |
348 |
|
349 |
Or frequently-come-up confusions. |
350 |
|
351 |
=over 4 |
352 |
|
353 |
=item Is this module meant to be serious? |
354 |
|
355 |
Yes, we would have put it under the C<Acme::> namespace otherwise. |
356 |
|
357 |
=item But the manpage is written in a funny/stupid/... way? |
358 |
|
359 |
This was meant to make it clear that our common sense is a subjective |
360 |
thing and other people can use their own notions, taking the steam out |
361 |
of anybody who might be offended (as some people are always offended no |
362 |
matter what you do). |
363 |
|
364 |
This was a failure. |
365 |
|
366 |
But we hope the manpage still is somewhat entertaining even though it |
367 |
explains boring rationale. |
368 |
|
369 |
=item Why do you impose your conventions on my code? |
370 |
|
371 |
For some reason people keep thinking that C<common::sense> imposes |
372 |
process-wide limits, even though the SYNOPSIS makes it clear that it works |
373 |
like other similar modules - i.e. only within the scope that C<use>s them. |
374 |
|
375 |
So, no, we don't - nobody is forced to use this module, and using a module |
376 |
that relies on common::sense does not impose anything on you. |
377 |
|
378 |
=item Why do you think only your notion of common::sense is valid? |
379 |
|
380 |
Well, we don't, and have clearly written this in the documentation to |
381 |
every single release. We were just faster than anybody else w.r.t. to |
382 |
grabbing the namespace. |
383 |
|
384 |
=item But everybody knows that you have to use strict and use warnings, |
385 |
why do you disable them? |
386 |
|
387 |
Well, we don't do this either - we selectively disagree with the |
388 |
usefulness of some warnings over others. This module is aimed at |
389 |
experienced Perl programmers, not people migrating from other languages |
390 |
who might be surprised about stuff such as C<undef>. On the other hand, |
391 |
this does not exclude the usefulness of this module for total newbies, due |
392 |
to its strictness in enforcing policy, while at the same time not limiting |
393 |
the expressive power of perl. |
394 |
|
395 |
This module is considerably I<more> strict than the canonical C<use |
396 |
strict; use warnings>, as it makes all its warnings fatal in nature, so |
397 |
you can not get away with as many things as with the canonical approach. |
398 |
|
399 |
This was not implemented in version 1.0 because of the daunting number |
400 |
of warning categories and the difficulty in getting exactly the set of |
401 |
warnings you wish (i.e. look at the SYNOPSIS in how complicated it is to |
402 |
get a specific set of warnings - it is not reasonable to put this into |
403 |
every module, the maintenance effort would be enormous). |
404 |
|
405 |
=item But many modules C<use strict> or C<use warnings>, so the memory |
406 |
savings do not apply? |
407 |
|
408 |
I suddenly feel sad... |
409 |
|
410 |
But yes, that's true. Fortunately C<common::sense> still uses only a |
411 |
miniscule amount of RAM. |
412 |
|
413 |
=item But it adds another dependency to your modules! |
414 |
|
415 |
It's a fact, yeah. But it's trivial to install, most popular modules have |
416 |
many more dependencies. And we consider dependencies a good thing - it |
417 |
leads to better APIs, more thought about interworking of modules and so |
418 |
on. |
419 |
|
420 |
=item Why do you use JSON and not YAML for your META.yml? |
421 |
|
422 |
This is not true - YAML supports a large subset of JSON, and this subset |
423 |
is what META.yml is written in, so it would be correct to say "the |
424 |
META.yml is written in a common subset of YAML and JSON". |
425 |
|
426 |
The META.yml follows the YAML, JSON and META.yml specifications, and is |
427 |
correctly parsed by CPAN, so if you have trouble with it, the problem is |
428 |
likely on your side. |
429 |
|
430 |
=item But! But! |
431 |
|
432 |
Yeah, we know. |
433 |
|
434 |
=back |
435 |
|
436 |
=head1 AUTHOR |
437 |
|
438 |
Marc Lehmann <schmorp@schmorp.de> |
439 |
http://home.schmorp.de/ |
440 |
|
441 |
Robin Redeker, "<elmex at ta-sa.org>". |
442 |
|
443 |
=cut |
444 |
|