… | |
… | |
130 | |
130 | |
131 | Returns the "real" (non-blocking) filehandle. Use this if you want to |
131 | Returns the "real" (non-blocking) filehandle. Use this if you want to |
132 | do operations on the file handle you cannot do using the Coro::Handle |
132 | do operations on the file handle you cannot do using the Coro::Handle |
133 | interface. |
133 | interface. |
134 | |
134 | |
135 | =item $fh->unsysread($data) |
135 | =item $fh->rbuf |
136 | |
136 | |
137 | Pushes the given data into the input buffer. The following calls to |
137 | Returns the current contents of the raed buffer (this is an lvalue, so you |
138 | read/sysread will first return this data. |
138 | can change the read buffer if you like). |
|
|
139 | |
|
|
140 | You can use this fucntion to implement your own optimized reader when neither |
|
|
141 | readline nor sysread are viable candidates, like this: |
|
|
142 | |
|
|
143 | # first get the _real_ non-blocking filehandle |
|
|
144 | # and fetch the current contents of the read buffer |
|
|
145 | my $nb_fh = $fh->fh; |
|
|
146 | my $buf = $fh->rbuf; |
|
|
147 | |
|
|
148 | for(;;) { |
|
|
149 | # now use buffer contents, modifying |
|
|
150 | # if necessary to reflect the removed data |
|
|
151 | |
|
|
152 | last if $buf ne ""; # we have leftover data |
|
|
153 | |
|
|
154 | # read another buffer full of data |
|
|
155 | $fh->readable or die "end of file"; |
|
|
156 | sysread $nb_fh, $buf, 8192; |
|
|
157 | } |
|
|
158 | |
|
|
159 | # put the read buffer back |
|
|
160 | $fh->rbuf = $buf; |
139 | |
161 | |
140 | =cut |
162 | =cut |
141 | |
163 | |
142 | sub fh { |
164 | sub fh { |
143 | (tied ${$_[0]})->[0]; |
165 | (tied ${$_[0]})->[0]; |
144 | } |
166 | } |
145 | |
167 | |
146 | sub unsysread { |
168 | sub rbuf : lvalue { |
147 | substr tied(${$_[0]})->[3], 0, 0, $_[1]; |
169 | tied(${$_[0]})->[3]; |
148 | } |
170 | } |
149 | |
171 | |
150 | package Coro::Handle::FH; |
172 | package Coro::Handle::FH; |
151 | |
173 | |
152 | no warnings qw(uninitialized); |
174 | no warnings qw(uninitialized); |