… | |
… | |
13 | |
13 | |
14 | Loading it makes AnyEvent check all arguments to AnyEvent-methods, at the |
14 | Loading it makes AnyEvent check all arguments to AnyEvent-methods, at the |
15 | expense of being slower (often the argument checking takes longer than the |
15 | expense of being slower (often the argument checking takes longer than the |
16 | actual function). |
16 | actual function). |
17 | |
17 | |
|
|
18 | B<< Currently, only AnyEvent I<methods> are checked, the AE:: I<functions> are not |
|
|
19 | affected. >> |
|
|
20 | |
18 | Normally, you don't load this module yourself but instead use it |
21 | Normally, you don't load this module yourself but instead use it |
19 | indirectly via the C<PERL_ANYEVENT_STRICT> environment variable (see |
22 | indirectly via the C<PERL_ANYEVENT_STRICT> environment variable (see |
20 | L<AnyEvent>). However, this module can be loaded at any time. |
23 | L<AnyEvent>). However, this module can be loaded manually at any time. |
21 | |
24 | |
22 | =cut |
25 | =cut |
23 | |
26 | |
24 | package AnyEvent::Strict; |
27 | package AnyEvent::Strict; |
25 | |
28 | |
26 | no warnings; # *sigh* |
|
|
27 | |
|
|
28 | use Carp qw(croak); |
29 | use Carp qw(croak); |
29 | |
30 | |
30 | use AnyEvent (); |
31 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
|
|
32 | |
|
|
33 | our @ISA; |
31 | |
34 | |
32 | AnyEvent::post_detect { |
35 | AnyEvent::post_detect { |
33 | # assume the first ISA member is the implementation |
36 | # assume the first ISA member is the implementation |
34 | # # and link us in before it in the chain. |
37 | # # and link us in before it in the chain. |
35 | my $MODEL = shift @AnyEvent::ISA; |
38 | my $MODEL = shift @AnyEvent::ISA; |
36 | unshift @ISA, $MODEL; |
39 | unshift @ISA, $MODEL; |
37 | unshift @AnyEvent::ISA, AnyEvent::Strict:: |
40 | unshift @AnyEvent::ISA, AnyEvent::Strict::; |
38 | }; |
41 | }; |
39 | |
42 | |
40 | sub io { |
43 | sub io { |
41 | my $class = shift; |
44 | my $class = shift; |
42 | my %arg = @_; |
45 | my %arg = @_; |
43 | |
46 | |
44 | ref $arg{cb} |
47 | ref $arg{cb} |
45 | or croak "AnyEvent->io called with illegal cb argument '$arg{cb}'"; |
48 | or croak "AnyEvent->io called with illegal cb argument '$arg{cb}'"; |
46 | delete $arg{cb}; |
49 | delete $arg{cb}; |
47 | |
50 | |
|
|
51 | $arg{poll} =~ /^[rw]$/ |
|
|
52 | or croak "AnyEvent->io called with illegal poll argument '$arg{poll}'"; |
|
|
53 | |
|
|
54 | if ($arg{fh} =~ /^\s*\d+\s*$/) { |
|
|
55 | $arg{fh} = AnyEvent::_dupfh $arg{poll}, $arg{fh}; |
|
|
56 | } else { |
48 | defined fileno $arg{fh} |
57 | defined eval { fileno $arg{fh} } |
49 | or croak "AnyEvent->io called with illegal fh argument '$arg{fh}'"; |
58 | or croak "AnyEvent->io called with illegal fh argument '$arg{fh}'"; |
|
|
59 | } |
|
|
60 | |
50 | -f $arg{fh} |
61 | -f $arg{fh} |
51 | and croak "AnyEvent->io called with fh argument pointing to a file"; |
62 | and croak "AnyEvent->io called with fh argument pointing to a file"; |
|
|
63 | |
|
|
64 | delete $arg{poll}; |
52 | delete $arg{fh}; |
65 | delete $arg{fh}; |
53 | |
|
|
54 | $arg{poll} =~ /^[rw]$/ |
|
|
55 | or croak "AnyEvent->io called with illegal poll argument '$arg{poll}'"; |
|
|
56 | delete $arg{poll}; |
|
|
57 | |
66 | |
58 | croak "AnyEvent->io called with unsupported parameter(s) " . join ", ", keys %arg |
67 | croak "AnyEvent->io called with unsupported parameter(s) " . join ", ", keys %arg |
59 | if keys %arg; |
68 | if keys %arg; |
60 | |
69 | |
61 | $class->SUPER::io (@_) |
70 | $class->SUPER::io (@_) |
… | |
… | |
89 | |
98 | |
90 | ref $arg{cb} |
99 | ref $arg{cb} |
91 | or croak "AnyEvent->signal called with illegal cb argument '$arg{cb}'"; |
100 | or croak "AnyEvent->signal called with illegal cb argument '$arg{cb}'"; |
92 | delete $arg{cb}; |
101 | delete $arg{cb}; |
93 | |
102 | |
94 | eval "require POSIX; 0 < &POSIX::SIG$arg{signal}" |
103 | defined AnyEvent::Base::sig2num $arg{signal} and $arg{signal} == 0 |
95 | or croak "AnyEvent->signal called with illegal signal name '$arg{signal}'"; |
104 | or croak "AnyEvent->signal called with illegal signal name '$arg{signal}'"; |
96 | delete $arg{signal}; |
105 | delete $arg{signal}; |
97 | |
106 | |
98 | croak "AnyEvent->signal called with unsupported parameter(s) " . join ", ", keys %arg |
107 | croak "AnyEvent->signal called with unsupported parameter(s) " . join ", ", keys %arg |
99 | if keys %arg; |
108 | if keys %arg; |