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