File Coverage

blib/lib/ZMQ/Raw/Loop/Handle.pm
Criterion Covered Total %
statement 44 45 97.7
branch 15 16 93.7
condition 13 18 72.2
subroutine 12 14 85.7
pod 1 2 50.0
total 85 95 89.4


line stmt bran cond sub pod time code
1             package ZMQ::Raw::Loop::Handle;
2             $ZMQ::Raw::Loop::Handle::VERSION = '0.38';
3 14     14   95 use strict;
  14         28  
  14         401  
4 14     14   68 use warnings;
  14         25  
  14         318  
5 14     14   65 use Carp;
  14         26  
  14         755  
6 14     14   90 use Scalar::Util qw/weaken/;
  14         27  
  14         1155  
7              
8 0     0   0 sub CLONE_SKIP { 1 }
9              
10             my @attributes;
11              
12             BEGIN
13             {
14 14     14   73 @attributes = qw/
15             handle
16             timer
17             timeout
18             on_readable
19             on_writable
20             on_timeout
21             /;
22              
23 14     14   100 no strict 'refs';
  14         50  
  14         1351  
24 14         34 foreach my $accessor (@attributes)
25             {
26 84         722 *{$accessor} = sub
27             {
28 48 100   48   375 @_ > 1 ? $_[0]->{$accessor} = $_[1] : $_[0]->{$accessor}
        48      
        48      
        0      
29 84         312 };
30             }
31             }
32              
33 14     14   118 use ZMQ::Raw;
  14         28  
  14         4299  
34              
35             =head1 NAME
36              
37             ZMQ::Raw::Loop::Handle - Handle class
38              
39             =head1 VERSION
40              
41             version 0.38
42              
43             =head1 DESCRIPTION
44              
45             A L represents a handle.
46              
47             B: The API of this module is unstable and may change without warning
48             (any change will be appropriately documented in the changelog).
49              
50             =head1 SYNOPSIS
51              
52             use ZMQ::Raw;
53              
54             my $handle = ZMQ::Raw::Loop::Handle->new
55             (
56             handle => $handle,
57             timeout => 30,
58              
59             on_readable => sub
60             {
61             },
62             on_writable => sub
63             {
64             },
65             on_timeout => sub
66             {
67             },
68             );
69              
70             my $loop = ZMQ::Raw::Loop->new;
71             $loop->add ($handle);
72             $loop->run;
73              
74             =head1 METHODS
75              
76             =head2 new( %args )
77              
78             Create a new handle.
79              
80             =cut
81              
82             sub new
83             {
84 9     9 1 4768 my ($this, %args) = @_;
85              
86 9 100       70 if (!$args{handle})
87             {
88 1         193 croak "handle not provided";
89             }
90              
91 8 100 100     28 if (!$args{on_readable} && !$args{on_writable})
92             {
93 1         101 croak "on_readable or on_writable needed";
94             }
95              
96 7 100 100     35 if ($args{on_readable} && ref ($args{on_readable}) ne 'CODE')
97             {
98 1         123 croak "on_readable not a code ref";
99             }
100              
101 6 100 66     22 if ($args{on_writable} && ref ($args{on_writable}) ne 'CODE')
102             {
103 1         100 croak "on_writable not a code ref";
104             }
105              
106 5 100 66     24 if ($args{on_timeout} && ref ($args{on_timeout}) ne 'CODE')
107             {
108 1         97 croak "on_timeout not a code ref";
109             }
110              
111 4 100 66     20 if ($args{on_timeout} && !exists ($args{timeout}))
112             {
113 1         87 croak "on_timeout provided but timeout not set";
114             }
115              
116 3   33     13 my $class = ref ($this) || $this;
117             my $self =
118             {
119             handle => $args{handle},
120             timeout => $args{timeout},
121             on_readable => $args{on_readable},
122             on_writable => $args{on_writable},
123             on_timeout => $args{on_timeout},
124 3         15 };
125              
126 3         94 return bless $self, $class;
127             }
128              
129              
130              
131             sub loop
132             {
133 4     4 0 13 my ($this, $loop) = @_;
134              
135 4 50       14 if (scalar (@_) > 1)
136             {
137 4         16 $this->{loop} = $loop;
138 4         13 weaken ($this->{loop});
139             }
140              
141 4         10 return $this->{loop};
142             }
143              
144             =for Pod::Coverage handle timer loop timeout on_readable on_writable on_timeout
145              
146             =head1 AUTHOR
147              
148             Jacques Germishuys
149              
150             =head1 LICENSE AND COPYRIGHT
151              
152             Copyright 2017 Jacques Germishuys.
153              
154             This program is free software; you can redistribute it and/or modify it
155             under the terms of either: the GNU General Public License as published
156             by the Free Software Foundation; or the Artistic License.
157              
158             See http://dev.perl.org/licenses/ for more information.
159              
160             =cut
161              
162             1; # End of ZMQ::Raw::Loop::Handle