X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=lib%2FBit%2FMorseSignals%2FReceiver.pm;h=1eb7befe980215f25a39526ba1335299e42c68ce;hb=68eb455d71803aa45fd67f97c9dd628fff59e1c4;hp=42f6b4fcca68fefed9422077afbcaffb98c8899c;hpb=f90efd6aef2e807ed35c07341456ce727d6668bc;p=perl%2Fmodules%2FBit-MorseSignals.git diff --git a/lib/Bit/MorseSignals/Receiver.pm b/lib/Bit/MorseSignals/Receiver.pm index 42f6b4f..1eb7bef 100644 --- a/lib/Bit/MorseSignals/Receiver.pm +++ b/lib/Bit/MorseSignals/Receiver.pm @@ -3,11 +3,11 @@ package Bit::MorseSignals::Receiver; use strict; use warnings; -use Carp qw/croak/; -use Encode qw/decode_utf8/; -use Storable qw/thaw/; +use Carp qw; +use Encode qw; +use Storable qw; -use Bit::MorseSignals qw/:consts/; +use Bit::MorseSignals qw<:consts>; =head1 NAME @@ -15,17 +15,17 @@ Bit::MorseSignals::Receiver - Base class for Bit::MorseSignals receivers. =head1 VERSION -Version 0.01 +Version 0.08 =cut -our $VERSION = '0.01'; +our $VERSION = '0.08'; =head1 SYNOPSIS - use Bit::MorseSignals; + use Bit::MorseSignals::Receiver; - my $pants = new Bit::MorseSignals::Receiver done => sub { print "received $_[1]!\n" }; + my $pants = Bit::MorseSignals::Receiver->new(done => sub { print "received $_[1]!\n" }); while (...) { my $bit = comes_from_somewhere_lets_say_signals(); $pants->push($bit); @@ -46,15 +46,17 @@ sub _check_self { =head1 METHODS -=head2 C<< new [ done => $cb ] >> +=head2 C -L object constructor. With the C<'done'> option, you can specify a callback that will be triggered every time a message is completed, and in which C<$_[0]> will be the receiver object and C<$_[1]> the message received. + my $bmr = Bit::MorseSignals::Receiver->new(done => $cb); + +L object constructor. With the C<'done'> option, you can specify a callback that will be triggered every time a message is completed, and in which C<$_[0]> will be the receiver object and C<$_[1]> the message received. =cut sub new { my $class = shift; - $class = ref $class || $class || return; + return unless $class = ref $class || $class; croak 'Optional arguments must be passed as key => value pairs' if @_ % 2; my %opts = @_; my $self = { @@ -66,7 +68,9 @@ sub new { return $self; } -=head2 C +=head2 C + + $bmr->push($bit); Tells the receiver that you have received the bit C<$bit>. Returns true while the message isn't completed, and C as soon as it is. @@ -98,7 +102,12 @@ sub push { substr $self->{buf}, -$base, $base, ''; my @demanglers = (sub { $_[0] }, \&decode_utf8, \&thaw ); # BM_DATA_{PLAIN, UTF8, STORABLE} - $self->{msg} = $demanglers[$self->{type}]->($self->{buf}); + $self->{msg} = defined $demanglers[$self->{type}] + ? do { + local $SIG{__DIE__} = sub { warn @_ }; + $demanglers[$self->{type}]->($self->{buf}) + } + : $self->{buf}; $self->reset; $self->{done}->($self, $self->{msg}) if $self->{done}; return; @@ -109,9 +118,10 @@ sub push { vec($self->{buf}, $self->{len}++, 1) = $bit; if ($self->{len} >= 3) { - my $type = 2 * vec($self->{buf}, 0, 1) - + vec($self->{buf}, 1, 1); - @{$self}{qw/state type buf len/} = (3, $type, '', 0); + my $type = 2 * vec($self->{buf}, 1, 1) + + vec($self->{buf}, 0, 1); + $type = BM_DATA_PLAIN if vec($self->{buf}, 2, 1); + @{$self}{qw} = (3, $type, '', 0); } } elsif ($self->{state} == 1) { # end of signature @@ -123,7 +133,7 @@ sub push { } else { # first bit - @{$self}{qw/state sig sig_bit sig_len buf len/} + @{$self}{qw} = (1, '', $bit, 1, '', 0 ); vec($self->{sig}, 0, 1) = $bit; @@ -142,7 +152,7 @@ sub reset { my ($self) = @_; _check_self($self); $self->{state} = 0; - @{$self}{qw/sig sig_bit sig_len type buf len/} = (); + @{$self}{qw} = (); return $self; } @@ -184,9 +194,9 @@ L, L. =head1 AUTHOR -Vincent Pit, C<< >> +Vincent Pit, C<< >>, L. -You can contact me by mail or on #perl @ FreeNode (vincent or Prof_Vince). +You can contact me by mail or on C (vincent). =head1 BUGS @@ -198,6 +208,8 @@ You can find documentation for this module with the perldoc command. perldoc Bit::MorseSignals::Receiver +Tests code coverage report is available at L. + =head1 COPYRIGHT & LICENSE Copyright 2008 Vincent Pit, all rights reserved.