File Coverage

blib/lib/Tk/SignalSlot.pm
Criterion Covered Total %
statement 9 40 22.5
branch 0 12 0.0
condition 0 6 0.0
subroutine 3 7 42.8
pod 3 3 100.0
total 15 68 22.0


line stmt bran cond sub pod time code
1              
2             package Tk::SignalSlot;
3              
4 1     1   8746 use strict;
  1         3  
  1         103  
5 1     1   6 use Carp;
  1         3  
  1         727  
6              
7             our $VERSION = 0.1;
8              
9             my %connected;
10             my @ids;
11             my $newID = 0;
12              
13             sub connect {
14 0     0 1   my ($parent, $event, $code) = @_;
15              
16 0 0 0       unless ($event && $code) {
17 0           croak "connect usage - need an event description and a callback";
18             }
19              
20             # create the callback object.
21 0           my $cb = Tk::Callback->new($code);
22              
23             # if this is the first time we connect to this event,
24             # create the Tk binding for it.
25 0 0         unless (exists $connected{$parent}{$event}) {
26             $parent->Tk::bind($event => sub {
27 0   0 0     $_ && $_->Call() for @{$connected{$parent}{$event}};
  0            
28 0           });
29              
30 0           $connected{$parent}{$event} = [];
31             }
32              
33 0           my $index = @{$connected{$parent}{$event}};
  0            
34 0           $connected{$parent}{$event}[$index] = $cb;
35              
36 0           $ids[$newID] = [$parent, $event, $index];
37 0           return $newID++;
38             }
39              
40             sub disconnect {
41 0     0 1   my ($w, $id) = @_;
42              
43 0 0         if ($id > $#ids) {
44 0           carp "disconnect - Callback ID $id not defined!";
45 0           return undef;
46             }
47              
48 0           my ($parent, $event, $index) = @{$ids[$id]};
  0            
49              
50 0 0         unless (defined $connected{$parent}{$event}[$index]) {
51 0           carp "disconnect - Callback ID $id already disconnected!";
52 0           return undef;
53             }
54              
55 0           $connected{$parent}{$event}[$index] = undef;
56              
57 0           return 1;
58             }
59              
60             sub is_connected {
61 0     0 1   my ($w, $id) = @_;
62              
63 0 0         return 0 if $id > $#ids;
64              
65 0           my ($parent, $event, $index) = @{$ids[$id]};
  0            
66 0 0         return 0 unless defined $connected{$parent}{$event}[$index];
67              
68 0           return 1;
69             }
70              
71             package Tk;
72              
73 1     1   6 use strict;
  1         5  
  1         74  
74              
75             *connect = \&Tk::SignalSlot::connect;
76             *disconnect = \&Tk::SignalSlot::disconnect;
77             *is_connected = \&Tk::SignalSlot::is_connected;
78              
79             1;
80              
81             __END__