| 1 | package Data::FDSet; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | our $VERSION; | |||||||||
| 7 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | $VERSION = '0.02'; | |||||||||
| 9 | } | ||||||||||
| 10 | |||||||||||
| 11 | =encoding utf-8 | ||||||||||
| 12 | |||||||||||
| 13 | =head1 NAME | ||||||||||
| 14 | |||||||||||
| 15 | Data::FDSet - Syntactic sugar for L<select()|perlfunc/select> masks | ||||||||||
| 16 | |||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||
| 18 | |||||||||||
| 19 | Object-oriented syntax: | ||||||||||
| 20 | |||||||||||
| 21 | my $fdset = Data::FDSet->new(); | ||||||||||
| 22 | |||||||||||
| 23 | # These accept either filehandles or file descriptors: | ||||||||||
| 24 | $fdset->add( $some_filehandle, fileno($other_fh) ); | ||||||||||
| 25 | $fdset->remove( $other_fh ); | ||||||||||
| 26 | |||||||||||
| 27 | my $rout = Data::FDSet->new(); | ||||||||||
| 28 | |||||||||||
| 29 | my $got = select( $$rout = $$fdset, undef, undef, 10 ); | ||||||||||
| 30 | |||||||||||
| 31 | if ($got > 1) { | ||||||||||
| 32 | my $fds_to_read_ar = $rout->get_fds(); | ||||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | Or, if you’d rather avoid object-oriented syntax: | ||||||||||
| 36 | |||||||||||
| 37 | my $rout = q<>; | ||||||||||
| 38 | Data::FDSet::add(\$rout, $some_filehandle, fileno($other_fh)) | ||||||||||
| 39 | |||||||||||
| 40 | my $fds_to_read_ar = Data::FDSet::get_fds(\$rout); | ||||||||||
| 41 | |||||||||||
| 42 | =head1 DESCRIPTION | ||||||||||
| 43 | |||||||||||
| 44 | This little module makes working with 4-argument L<select()|perlfunc/select> | ||||||||||
| 45 | a bit easier by providing object methods to do the typical operations done | ||||||||||
| 46 | on the bitmasks in connection with that function. These methods parallel | ||||||||||
| 47 | the functions that C provides to handle C<struct fd_set>. | ||||||||||
| 48 | |||||||||||
| 49 | =cut | ||||||||||
| 50 | |||||||||||
| 51 | #---------------------------------------------------------------------- | ||||||||||
| 52 | |||||||||||
| 53 | =head1 INTERFACE NOTE | ||||||||||
| 54 | |||||||||||
| 55 | A Data::FDSet object is a blessed scalar reference to a bitmask. | ||||||||||
| 56 | Unlike with most Perl objects, you may safely reference the object | ||||||||||
| 57 | internals, e.g., by doing | ||||||||||
| 58 | |||||||||||
| 59 | $$rout_obj = $rin; | ||||||||||
| 60 | |||||||||||
| 61 | … to replace the bitmask contents. (For this reason, this class defines | ||||||||||
| 62 | no method to do the above.) | ||||||||||
| 63 | |||||||||||
| 64 | =head1 METHODS | ||||||||||
| 65 | |||||||||||
| 66 | =head2 $obj = I<CLASS>->new( [ $BITMASK ] ); | ||||||||||
| 67 | |||||||||||
| 68 | Instantiates this class. $BITMASK may optionally be passed to | ||||||||||
| 69 | initialize the object state. | ||||||||||
| 70 | |||||||||||
| 71 | =cut | ||||||||||
| 72 | |||||||||||
| 73 | 4 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 74 | 4 | my ($class) = @_; | |||||||||
| 75 | |||||||||||
| 76 | 4 | ▶ | my $sr = defined($_[1]) ? \$_[1] : \do { my $v = q<> }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | |||||||||||
| 78 | 4 | return bless $sr, $class; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | =head2 $obj = I<OBJ>->evacuate() | ||||||||||
| 82 | |||||||||||
| 83 | Empty out the object. Analogous to L<FD_ZERO(2)>. | ||||||||||
| 84 | |||||||||||
| 85 | Returns I<OBJ>. | ||||||||||
| 86 | |||||||||||
| 87 | =cut | ||||||||||
| 88 | |||||||||||
| 89 | 0 | ▶ | sub evacuate { | ||||||||
|
Subroutine
evacuate: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 90 | 0 | ${ $_[0] } = q<>; | |||||||||
| 91 | |||||||||||
| 92 | 0 | return $_[0]; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | =head2 $obj = I<OBJ>->add( $FD_OR_FH [, $FD_OR_FH, .. ] ) | ||||||||||
| 96 | |||||||||||
| 97 | Add one or more file descriptors to the object. | ||||||||||
| 98 | Accepts either Perl filehandles or file descriptors. | ||||||||||
| 99 | Analogous to L<FD_SET(2)>. | ||||||||||
| 100 | |||||||||||
| 101 | =cut | ||||||||||
| 102 | |||||||||||
| 103 | 1 | ▶ | sub add { | ||||||||
|
Subroutine
add: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 104 | 1 | for my $arg ( @_[ 1 .. $#_ ] ) { | |||||||||
| 105 | 3 | ▶ | vec( ${ $_[0] }, defined(fileno($arg)) ? fileno($arg) : $arg, 1 ) = 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 1 | return $_[0]; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | =head2 $obj = I<OBJ>->remove( $FD_OR_FH [, $FD_OR_FH, .. ] ) | ||||||||||
| 112 | |||||||||||
| 113 | The complement of C<add()>. | ||||||||||
| 114 | Analogous to L<FD_CLR(2)>. | ||||||||||
| 115 | |||||||||||
| 116 | =cut | ||||||||||
| 117 | |||||||||||
| 118 | 1 | ▶ | sub remove { | ||||||||
|
Subroutine
remove: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 119 | 1 | for my $arg ( @_[ 1 .. $#_ ] ) { | |||||||||
| 120 | 2 | ▶ | vec( ${ $_[0] }, defined(fileno($arg)) ? fileno($arg) : $arg, 1 ) = 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | 1 | return $_[0]; | |||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | =head2 $yn = I<OBJ>->has( $FD_OR_FH ) | ||||||||||
| 127 | |||||||||||
| 128 | Tests for a file descriptor’s presence in the object. | ||||||||||
| 129 | Accepts either a Perl filehandles or a file descriptor. | ||||||||||
| 130 | Analogous to L<FD_ISSET(2)>. | ||||||||||
| 131 | |||||||||||
| 132 | =cut | ||||||||||
| 133 | |||||||||||
| 134 | 1 | ▶ | sub has { | ||||||||
|
Subroutine
has: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 135 | 1 | ▶ | return vec( ${ $_[0] }, defined(fileno($_[1])) ? fileno($_[1]) : $_[1], 1 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | =head2 $fds_ar = I<OBJ>->get_fds() | ||||||||||
| 139 | |||||||||||
| 140 | Returns a reference to an array of the file descriptors that are | ||||||||||
| 141 | in the object. | ||||||||||
| 142 | |||||||||||
| 143 | =cut | ||||||||||
| 144 | |||||||||||
| 145 | 5 | ▶ | sub get_fds { | ||||||||
|
Subroutine
get_fds: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 146 | 5 | my $max = 8 * length(${ $_[0] }) - 1; | |||||||||
| 147 | |||||||||||
| 148 | 5 | my @fds; | |||||||||
| 149 | |||||||||||
| 150 | 5 | for my $fd ( 0 .. $max ) { | |||||||||
| 151 | 56 | ▶ | if ( vec(${ $_[0] }, $fd, 1) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 152 | 7 | push @fds, $fd; | |||||||||
| 153 | } | ||||||||||
| 154 | } | ||||||||||
| 155 | |||||||||||
| 156 | 5 | return \@fds; | |||||||||
| 157 | } | ||||||||||
| 158 | |||||||||||
| 159 | 1 | 1; | |||||||||