… | |
… | |
25 | modes to enable and make fatal, we found that we (and our code written so |
25 | modes to enable and make fatal, we found that we (and our code written so |
26 | far, and others) fully agree on every option, even though we never used |
26 | far, and others) fully agree on every option, even though we never used |
27 | warnings before, so it seems this module indeed reflects a "common" sense |
27 | warnings before, so it seems this module indeed reflects a "common" sense |
28 | among some long-time Perl coders. |
28 | among some long-time Perl coders. |
29 | |
29 | |
|
|
30 | The basic philosophy behind the choices made in common::sense can be |
|
|
31 | summarised as: "enforcing strict policies to catch as many bugs as |
|
|
32 | possible, while at the same time, not limiting the expressive power |
|
|
33 | available to the programmer". |
|
|
34 | |
|
|
35 | Two typical examples of this philosophy are uninitialised and malloc |
|
|
36 | warnings: |
|
|
37 | |
|
|
38 | C<undef> is a well-defined feature of perl, and enabling warnings for |
|
|
39 | using it rarely catches any bugs, but considerably limits you in what you |
|
|
40 | can do, so uninitialised warnings are disabled. |
|
|
41 | |
|
|
42 | Freeing something twice on the C level is a serious bug, usually causing |
|
|
43 | memory corruption. It often leads to side effects much later in the |
|
|
44 | program and there are no advantages to not reporting this, so malloc |
|
|
45 | warnings are fatal by default. |
|
|
46 | |
|
|
47 | What follows is a more thorough discussion of what this module does, |
|
|
48 | and why it does it, and what the advantages (and disadvantages) of this |
|
|
49 | approach are. |
|
|
50 | |
30 | =over 4 |
51 | =over 4 |
31 | |
52 | |
32 | =item use strict qw(subs vars) |
53 | =item use strict qw(subs vars) |
33 | |
54 | |
34 | Using C<use strict> is definitely common sense, but C<use strict |
55 | Using C<use strict> is definitely common sense, but C<use strict |
… | |
… | |
72 | Perl, regardless of use feature or not, so a new major perl release means |
93 | Perl, regardless of use feature or not, so a new major perl release means |
73 | changes to many modules - new keywords are just the tip of the iceberg. |
94 | changes to many modules - new keywords are just the tip of the iceberg. |
74 | |
95 | |
75 | If your code isn't alive, it's dead, Jim - be an active maintainer. |
96 | If your code isn't alive, it's dead, Jim - be an active maintainer. |
76 | |
97 | |
|
|
98 | But nobody forces you to use those extra features in modules meant for |
|
|
99 | older versions of perl - common::sense of course works there as well. |
|
|
100 | There is also an important other mode where having additional features by |
|
|
101 | default is useful: commandline hacks and internal use scripts: See "much |
|
|
102 | reduced typing", below. |
|
|
103 | |
77 | |
104 | |
78 | =item no warnings, but a lot of new errors |
105 | =item no warnings, but a lot of new errors |
79 | |
106 | |
80 | Ah, the dreaded warnings. Even worse, the horribly dreaded C<-w> |
107 | Ah, the dreaded warnings. Even worse, the horribly dreaded C<-w> |
81 | switch: Even though we don't care if other people use warnings (and |
108 | switch: Even though we don't care if other people use warnings (and |
… | |
… | |
124 | |
151 | |
125 | We quickly agreed that indeed the code should be changed, even though it |
152 | We quickly agreed that indeed the code should be changed, even though it |
126 | happened to do the right thing when the warning was switched off. |
153 | happened to do the right thing when the warning was switched off. |
127 | |
154 | |
128 | |
155 | |
|
|
156 | =item much reduced typing |
|
|
157 | |
|
|
158 | Especially with version 2.0 of common::sense, the amount of boilerplate |
|
|
159 | code you need to add to gte I<this> policy is daunting. Nobody would write |
|
|
160 | this out in throwaway scripts, commandline hacks or in quick internal-use |
|
|
161 | scripts. |
|
|
162 | |
|
|
163 | By using common::sense you get a defined set of policies (ours, but maybe |
|
|
164 | yours, too, if you accept them), and they are easy to apply to your |
|
|
165 | scripts: typing C<use common::sense;> is even shorter than C<use warnings; |
|
|
166 | use strict; use feature ...>. |
|
|
167 | |
|
|
168 | And you can immediately use the features of your installed perl, which |
|
|
169 | is more difficult in code you release, but not usually an issue for |
|
|
170 | internal-use code (downgrades of your production perl should be rare, |
|
|
171 | right?). |
|
|
172 | |
|
|
173 | |
129 | =item mucho reduced memory usage |
174 | =item mucho reduced memory usage |
130 | |
175 | |
131 | Just using all those pragmas mentioned in the SYNOPSIS together wastes |
176 | Just using all those pragmas mentioned in the SYNOPSIS together wastes |
132 | <blink>I<< B<776> kilobytes >></blink> of precious memory in my perl, for |
177 | <blink>I<< B<776> kilobytes >></blink> of precious memory in my perl, for |
133 | I<every single perl process using our code>, which on our machines, is a |
178 | I<every single perl process using our code>, which on our machines, is a |
… | |
… | |
143 | |
188 | |
144 | =cut |
189 | =cut |
145 | |
190 | |
146 | package common::sense; |
191 | package common::sense; |
147 | |
192 | |
148 | our $VERSION = '2.01'; |
193 | our $VERSION = '2.02'; |
149 | |
194 | |
150 | # paste this into perl to find bitmask |
195 | # paste this into perl to find bitmask |
151 | |
196 | |
152 | # no warnings; |
197 | # no warnings; |
153 | # use warnings qw(FATAL closed threads internal debugging pack substr malloc unopened portable prototype |
198 | # use warnings qw(FATAL closed threads internal debugging pack substr malloc unopened portable prototype |
… | |
… | |
303 | why do you disable them? |
348 | why do you disable them? |
304 | |
349 | |
305 | Well, we don't do this either - we selectively disagree with the |
350 | Well, we don't do this either - we selectively disagree with the |
306 | usefulness of some warnings over others. This module is aimed at |
351 | usefulness of some warnings over others. This module is aimed at |
307 | experienced Perl programmers, not people migrating from other languages |
352 | experienced Perl programmers, not people migrating from other languages |
308 | who might be surprised about stuff such as C<undef>. |
353 | who might be surprised about stuff such as C<undef>. On the other hand, |
|
|
354 | this does not exclude the usefulness of this module for total newbies, due |
|
|
355 | to its strictness in enforcing policy, while at the same time not limiting |
|
|
356 | the expresive power of perl. |
309 | |
357 | |
310 | In fact, this module is considerably I<more> strict than the canonical |
358 | This module is considerably I<more> strict than the canonical C<use |
311 | C<use strict; use warnings>, as it makes all warnings fatal in nature, so |
359 | strict; use warnings>, as it makes all its warnings fatal in nature, so |
312 | you can get away with as many things as with the canonical approach. |
360 | you can not get away with as many things as with the canonical approach. |
313 | |
361 | |
314 | This was not implemented in version 1.0 because of the daunting number |
362 | This was not implemented in version 1.0 because of the daunting number |
315 | of warning categories and the difficulty in getting exactly the set of |
363 | of warning categories and the difficulty in getting exactly the set of |
316 | warnings you wish (i.e. look at the SYNOPSIS in how complicated it is to |
364 | warnings you wish (i.e. look at the SYNOPSIS in how complicated it is to |
317 | get a specific set of warnings - it is not reasonable to put this into |
365 | get a specific set of warnings - it is not reasonable to put this into |
… | |
… | |
330 | It's a fact, yeah. But it's trivial to install, most popular modules have |
378 | It's a fact, yeah. But it's trivial to install, most popular modules have |
331 | many more dependencies and we consider dependencies a good thing - it |
379 | many more dependencies and we consider dependencies a good thing - it |
332 | leads to better APIs, more thought about interworking of modules and so |
380 | leads to better APIs, more thought about interworking of modules and so |
333 | on. |
381 | on. |
334 | |
382 | |
|
|
383 | =item Why do you use JSON and not YAML for your META.yml? |
|
|
384 | |
|
|
385 | This is not true - YAML supports a large subset of JSON, and this subset |
|
|
386 | is what META.yml is written in, so it would be correct to say "the |
|
|
387 | META.yml is written in a common subset of YAML and JSON". |
|
|
388 | |
|
|
389 | The META.yml follows the YAML, JSON and META.yml specifications, and is |
|
|
390 | correctly parsed by CPAN, so if you have trouble with it, the problem is |
|
|
391 | likely on your side. |
|
|
392 | |
335 | =item But! But! |
393 | =item But! But! |
336 | |
394 | |
337 | Yeah, we know. |
395 | Yeah, we know. |
338 | |
396 | |
339 | =back |
397 | =back |