… | |
… | |
68 | |
68 | |
69 | no warnings; |
69 | no warnings; |
70 | use strict 'vars'; |
70 | use strict 'vars'; |
71 | use Carp; |
71 | use Carp; |
72 | |
72 | |
73 | our $VERSION = '0.4'; |
73 | our $VERSION = '1.02'; |
74 | our $MODEL; |
74 | our $MODEL; |
75 | |
75 | |
76 | our $AUTOLOAD; |
76 | our $AUTOLOAD; |
77 | our @ISA; |
77 | our @ISA; |
78 | |
78 | |
79 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
79 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
80 | |
80 | |
|
|
81 | our @REGISTRY; |
|
|
82 | |
81 | my @models = ( |
83 | my @models = ( |
82 | [Coro => Coro::Event::], |
84 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
83 | [Event => Event::], |
85 | [Event:: => AnyEvent::Impl::Event::], |
84 | [Glib => Glib::], |
86 | [Glib:: => AnyEvent::Impl::Glib::], |
85 | [Tk => Tk::], |
87 | [Tk:: => AnyEvent::Impl::Tk::], |
86 | ); |
88 | ); |
87 | |
89 | |
88 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait cancel DESTROY); |
90 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait cancel DESTROY); |
89 | |
91 | |
90 | sub AUTOLOAD { |
92 | sub AUTOLOAD { |
… | |
… | |
93 | $method{$AUTOLOAD} |
95 | $method{$AUTOLOAD} |
94 | or croak "$AUTOLOAD: not a valid method for AnyEvent objects"; |
96 | or croak "$AUTOLOAD: not a valid method for AnyEvent objects"; |
95 | |
97 | |
96 | unless ($MODEL) { |
98 | unless ($MODEL) { |
97 | # check for already loaded models |
99 | # check for already loaded models |
98 | for (@models) { |
100 | for (@REGISTRY, @models) { |
99 | my ($model, $package) = @$_; |
101 | my ($package, $model) = @$_; |
100 | if (${"$package\::VERSION"} > 0) { |
102 | if (${"$package\::VERSION"} > 0) { |
101 | eval "require AnyEvent::Impl::$model"; |
103 | if (eval "require $model") { |
|
|
104 | $MODEL = $model; |
102 | warn "AnyEvent: found model '$model', using it.\n" if $MODEL && $verbose > 1; |
105 | warn "AnyEvent: found model '$model', using it.\n" if $verbose > 1; |
103 | last if $MODEL; |
106 | last; |
|
|
107 | } |
104 | } |
108 | } |
105 | } |
109 | } |
106 | |
110 | |
107 | unless ($MODEL) { |
111 | unless ($MODEL) { |
108 | # try to load a model |
112 | # try to load a model |
109 | |
113 | |
110 | for (@models) { |
114 | for (@REGISTRY, @models) { |
111 | my ($model, $package) = @$_; |
115 | my ($package, $model) = @$_; |
112 | eval "require AnyEvent::Impl::$model"; |
116 | if (eval "require $model") { |
|
|
117 | $MODEL = $model; |
113 | warn "AnyEvent: autprobed and loaded model '$model', using it.\n" if $MODEL && $verbose > 1; |
118 | warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; |
114 | last if $MODEL; |
119 | last; |
|
|
120 | } |
115 | } |
121 | } |
116 | |
122 | |
117 | $MODEL |
123 | $MODEL |
118 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk."; |
124 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk."; |
119 | } |
125 | } |
… | |
… | |
124 | my $class = shift; |
130 | my $class = shift; |
125 | $class->$AUTOLOAD (@_); |
131 | $class->$AUTOLOAD (@_); |
126 | } |
132 | } |
127 | |
133 | |
128 | =back |
134 | =back |
|
|
135 | |
|
|
136 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
|
|
137 | |
|
|
138 | If you need to support another event library which isn't directly |
|
|
139 | supported by AnyEvent, you can supply your own interface to it by |
|
|
140 | pushing, before the first watcher gets created, the package name of |
|
|
141 | the event module and the package name of the interface to use onto |
|
|
142 | C<@AnyEvent::REGISTRY>. You can do that before and even without loading |
|
|
143 | AnyEvent. |
|
|
144 | |
|
|
145 | Example: |
|
|
146 | |
|
|
147 | push @AnyEvent::REGISTRY, [urxvt => urxvt::anyevent::]; |
|
|
148 | |
|
|
149 | This tells AnyEvent to (literally) use the C<urxvt::anyevent::> |
|
|
150 | package/class when it finds the C<urxvt> package/module is loaded. When |
|
|
151 | AnyEvent is loaded and asked to find a suitable event model, it will |
|
|
152 | first check for the presence of urxvt. |
|
|
153 | |
|
|
154 | The class should prove implementations for all watcher types (see |
|
|
155 | L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> |
|
|
156 | (Source code) and so on for actual examples, use C<perldoc -m |
|
|
157 | AnyEvent::Impl::Glib> to see the sources). |
|
|
158 | |
|
|
159 | The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) |
|
|
160 | uses the above line as-is. An interface isn't included in AnyEvent |
|
|
161 | because it doesn't make sense outside the embedded interpreter inside |
|
|
162 | I<rxvt-unicode>, and it is updated and maintained as part of the |
|
|
163 | I<rxvt-unicode> distribution. |
|
|
164 | |
|
|
165 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
|
|
166 | condition variables: code blocking while waiting for a condition will |
|
|
167 | C<die>. This still works with most modules/usages, and blocking calls must |
|
|
168 | not be in an interactive appliation, so it makes sense. |
129 | |
169 | |
130 | =head1 ENVIRONMENT VARIABLES |
170 | =head1 ENVIRONMENT VARIABLES |
131 | |
171 | |
132 | The following environment variables are used by this module: |
172 | The following environment variables are used by this module: |
133 | |
173 | |