--- AnyEvent-MP/MP.pm 2009/08/02 14:44:37 1.8 +++ AnyEvent-MP/MP.pm 2009/08/02 19:29:41 1.16 @@ -84,10 +84,11 @@ use base "Exporter"; -our $VERSION = '0.01'; +our $VERSION = '0.02'; our @EXPORT = qw( NODE $NODE $PORT snd rcv _any_ create_port create_port_on + create_miniport become_slave become_public ); @@ -126,7 +127,7 @@ =cut sub create_port { - my $id = "$AnyEvent::MP::Base::UNIQ." . ++$AnyEvent::MP::Base::ID; + my $id = "$AnyEvent::MP::Base::UNIQ." . $AnyEvent::MP::Base::ID++; my $self = bless { id => "$NODE#$id", @@ -157,6 +158,38 @@ $self } +=item $portid = miniport { my @msg = @_; $finished } + +Creates a "mini port", that is, a very lightweight port without any +pattern matching behind it, and returns its ID. + +The block will be called for every message received on the port. When the +callback returns a true value its job is considered "done" and the port +will be destroyed. Otherwise it will stay alive. + +The message will be passed as-is, no extra argument (ie.. no port id) will +be passed to the callback. + +If you need the local port id in the callback, this works nicely: + + my $port; $port = miniport { + snd $otherport, reply => $port; + }; + +=cut + +sub miniport(&) { + my $cb = shift; + my $id = "$AnyEvent::MP::Base::UNIQ." . $AnyEvent::MP::Base::ID++; + + $AnyEvent::MP::Base::PORT{$id} = sub { + &$cb + and delete $AnyEvent::MP::Base::PORT{$id}; + }; + + "$NODE#$id" +} + package AnyEvent::MP::Port; =back