ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/common-sense/sense.pm
(Generate patch)

Comparing common-sense/sense.pm (file contents):
Revision 1.1 by root, Thu Jul 9 16:14:16 2009 UTC vs.
Revision 1.17 by root, Tue Sep 8 16:04:52 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines