| 1 |
root |
1.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 |
|
|
# roughly the same as, with much lower memory usage: |
| 10 |
|
|
# |
| 11 |
|
|
# use strict qw(vars subs); |
| 12 |
|
|
# use feature qw(say state switch); |
| 13 |
|
|
# no warnings; |
| 14 |
|
|
|
| 15 |
|
|
=head1 DESCRIPTION |
| 16 |
|
|
|
| 17 |
|
|
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 |
| 19 |
|
|
Perl coders. |
| 20 |
|
|
|
| 21 |
|
|
=over 4 |
| 22 |
|
|
|
| 23 |
|
|
=item use strict qw(subs vars) |
| 24 |
|
|
|
| 25 |
|
|
Using C<use strict> is definitely common sense, but C<use strict |
| 26 |
|
|
'refs'> definitely overshoots it's usefulness. After almost two |
| 27 |
|
|
decades of Perl hacking, we decided that it does more harm than being |
| 28 |
|
|
useful. Specifically, constructs like these: |
| 29 |
|
|
|
| 30 |
|
|
@{ $var->[0] } |
| 31 |
|
|
|
| 32 |
|
|
Must be written like this, when C<use strict 'refs'> is in scope, and |
| 33 |
|
|
C<$var> can legally be C<undef>: |
| 34 |
|
|
|
| 35 |
|
|
@{ $var->[0] || [] } |
| 36 |
|
|
|
| 37 |
|
|
This is annoying, and doesn't shield against obvious mistakes such as |
| 38 |
|
|
using C<"">, so one would even have to write: |
| 39 |
|
|
|
| 40 |
|
|
@{ defined $var->[0] ? $var->[0] : [] } |
| 41 |
|
|
|
| 42 |
|
|
... which nobody with a bit of common sense would consider |
| 43 |
|
|
writing. Curiously enough, sometimes, perl is not so strict, as this works |
| 44 |
|
|
even with C<use strict> in scope: |
| 45 |
|
|
|
| 46 |
|
|
for (@{ $var->[0] }) { ... |
| 47 |
|
|
|
| 48 |
|
|
If that isnt hipocrasy! And all that from a mere program! |
| 49 |
|
|
|
| 50 |
|
|
=item use feature qw(say state given) |
| 51 |
|
|
|
| 52 |
|
|
We found it annoying that we always have to enable extra features. If |
| 53 |
|
|
something breaks because it didn't anticipate future changes, so be |
| 54 |
|
|
it. 5.10 broke almost all our XS modules and nobody cared either - and few |
| 55 |
|
|
modules that are no longer maintained work with newer versions of Perl, |
| 56 |
|
|
regardless of use feature. |
| 57 |
|
|
|
| 58 |
|
|
If your code isn'talive, it's dead, jim. |
| 59 |
|
|
|
| 60 |
|
|
=item no warnings |
| 61 |
|
|
|
| 62 |
|
|
The dreaded warnings. Even worse, the horribly dreaded C<-w> switch. Even |
| 63 |
|
|
though we don't care if other people use warnings (and certainly there are |
| 64 |
|
|
useful ones), a lot of warnings simply go against the spirit of Perl, most |
| 65 |
|
|
prominently, the warnings related to C<undef>. There is nothing wrong with |
| 66 |
|
|
C<undef>: it has well-defined semantics, it is useful, and spitting out |
| 67 |
|
|
warnings you never asked for is just evil. |
| 68 |
|
|
|
| 69 |
|
|
So every module needs C<no warnings> to avoid somebody accidentally using |
| 70 |
|
|
C<-w> and forcing his bad standards on our code. No will do. |
| 71 |
|
|
|
| 72 |
|
|
(Also, why isn't this a C<use feature> switch? Adding warnings is |
| 73 |
|
|
apparently considered O.K., even if it breaks your programs). |
| 74 |
|
|
|
| 75 |
|
|
=item much less memory |
| 76 |
|
|
|
| 77 |
|
|
Just using all those pragmas together waste <blink>I<< B<776> kilobytes |
| 78 |
|
|
>></blink> of precious memory in my perl, for I<every single perl process |
| 79 |
|
|
using our code>, which on our machines, is a lot. |
| 80 |
|
|
|
| 81 |
|
|
The money/time/effort/electricity invested in these gigabytes (probably |
| 82 |
|
|
petabytes globally!) of wasted memory could easily save 42 trees, and a |
| 83 |
|
|
kitten! |
| 84 |
|
|
|
| 85 |
|
|
=cut |
| 86 |
|
|
|
| 87 |
root |
1.2 |
package common::sense; |
| 88 |
root |
1.1 |
|
| 89 |
root |
1.2 |
our $VERSION = '0.03'; |
| 90 |
root |
1.1 |
|
| 91 |
root |
1.2 |
sub import { |
| 92 |
|
|
${^WARNING_BITS} ^= ${^WARNING_BITS}; |
| 93 |
root |
1.1 |
} |
| 94 |
|
|
|
| 95 |
|
|
=cut |
| 96 |
|
|
|
| 97 |
|
|
1; |
| 98 |
|
|
|
| 99 |
|
|
=back |
| 100 |
|
|
|
| 101 |
|
|
=head1 EXAMPLE |
| 102 |
|
|
|
| 103 |
|
|
There really should be a complete C/XS example. Bug me about it. |
| 104 |
|
|
|
| 105 |
|
|
=head1 IMPLEMENTATION DETAILS AND LIMITATIONS |
| 106 |
|
|
|
| 107 |
|
|
This module works by "hijacking" SIGKILL, which is guaranteed to be always |
| 108 |
|
|
available in perl, but also cannot be caught, so is always available. |
| 109 |
|
|
|
| 110 |
|
|
Basically, this module fakes the receive of a SIGKILL signal and |
| 111 |
|
|
then catches it. This makes normal signal handling slower (probably |
| 112 |
|
|
unmeasurably), but has the advantage of not requiring a special runops nor |
| 113 |
|
|
slowing down normal perl execution a bit. |
| 114 |
|
|
|
| 115 |
|
|
It assumes that C<sig_atomic_t> and C<int> are both exception-safe to |
| 116 |
|
|
modify (C<sig_atomic_> is used by this module, and perl itself uses |
| 117 |
|
|
C<int>, so we can assume that this is quite portable, at least w.r.t. |
| 118 |
|
|
signals). |
| 119 |
|
|
|
| 120 |
|
|
=head1 AUTHOR |
| 121 |
|
|
|
| 122 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
| 123 |
|
|
http://home.schmorp.de/ |
| 124 |
|
|
|
| 125 |
|
|
=cut |
| 126 |
|
|
|