… | |
… | |
139 | |
139 | |
140 | You can use this function to implement your own optimized reader when neither |
140 | You can use this function to implement your own optimized reader when neither |
141 | readline nor sysread are viable candidates, like this: |
141 | readline nor sysread are viable candidates, like this: |
142 | |
142 | |
143 | # first get the _real_ non-blocking filehandle |
143 | # first get the _real_ non-blocking filehandle |
144 | # and fetch the current contents of the read buffer |
144 | # and fetch a reference to the read buffer |
145 | my $nb_fh = $fh->fh; |
145 | my $nb_fh = $fh->fh; |
146 | my $buf = $fh->rbuf; |
146 | my $buf = \$fh->rbuf; |
147 | |
147 | |
148 | for(;;) { |
148 | for(;;) { |
149 | # now use buffer contents, modifying |
149 | # now use buffer contents, modifying |
150 | # if necessary to reflect the removed data |
150 | # if necessary to reflect the removed data |
151 | |
151 | |
152 | last if $buf ne ""; # we have leftover data |
152 | last if $$buf ne ""; # we have leftover data |
153 | |
153 | |
154 | # read another buffer full of data |
154 | # read another buffer full of data |
155 | $fh->readable or die "end of file"; |
155 | $fh->readable or die "end of file"; |
156 | sysread $nb_fh, $buf, 8192; |
156 | sysread $nb_fh, $$buf, 8192; |
157 | } |
157 | } |
158 | |
|
|
159 | # put the read buffer back |
|
|
160 | $fh->rbuf = $buf; |
|
|
161 | |
158 | |
162 | =cut |
159 | =cut |
163 | |
160 | |
164 | sub fh { |
161 | sub fh { |
165 | (tied ${$_[0]})->[0]; |
162 | (tied ${$_[0]})->[0]; |