ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Handle.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Handle.pm (file contents):
Revision 1.35 by root, Mon May 26 05:46:35 2008 UTC vs.
Revision 1.36 by root, Mon May 26 18:26:52 2008 UTC

733 733
734 1 734 1
735 } 735 }
736}; 736};
737 737
738=item regex => $accept[, $reject[, $skip], $cb->($data)
739
740Makes a regex match against the regex object C<$accept> and returns
741everything up to and including the match.
742
743Example: read a single line terminated by '\n'.
744
745 $handle->push_read (regex => qr<\n>, sub { ... });
746
747If C<$reject> is given and not undef, then it determines when the data is
748to be rejected: it is matched against the data when the C<$accept> regex
749does not match and generates an C<EBADMSG> error when it matches. This is
750useful to quickly reject wrong data (to avoid waiting for a timeout or a
751receive buffer overflow).
752
753Example: expect a single decimal number followed by whitespace, reject
754anything else (not the use of an anchor).
755
756 $handle->push_read (regex => qr<^[0-9]+\s>, qr<[^0-9]>, sub { ... });
757
758If C<$skip> is given and not C<undef>, then it will be matched against
759the receive buffer when neither C<$accept> nor C<$reject> match,
760and everything preceding and including the match will be accepted
761unconditionally. This is useful to skip large amounts of data that you
762know cannot be matched, so that the C<$accept> or C<$reject> regex do not
763have to start matching from the beginning. This is purely an optimisation
764and is usually worth only when you expect more than a few kilobytes.
765
766Example: expect a http header, which ends at C<\015\012\015\012>. Since we
767expect the header to be very large (it isn't in practise, but...), we use
768a skip regex to skip initial portions. The skip regex is tricky in that
769it only accepts something not ending in either \015 or \012, as these are
770required for the accept regex.
771
772 $handle->push_read (regex =>
773 qr<\015\012\015\012>,
774 undef, # no reject
775 qr<^.*[^\015\012]>,
776 sub { ... });
777
778=cut
779
780register_read_type regex => sub {
781 my ($self, $cb, $accept, $reject, $skip) = @_;
782
783 my $data;
784 my $rbuf = \$self->{rbuf};
785
786 sub {
787 # accept
788 if ($$rbuf =~ $accept) {
789 $data .= substr $$rbuf, 0, $+[0], "";
790 $cb->($self, $data);
791 return 1;
792 }
793
794 # reject
795 if ($reject && $$rbuf =~ $reject) {
796 $! = &Errno::EBADMSG;
797 $self->error;
798 }
799
800 # skip
801 if ($skip && $$rbuf =~ $skip) {
802 $data .= substr $$rbuf, 0, $+[0], "";
803 }
804
805 ()
806 }
807};
808
738=back 809=back
739 810
740=item AnyEvent::Handle::register_read_type type => $coderef->($self, $cb, @args) 811=item AnyEvent::Handle::register_read_type type => $coderef->($self, $cb, @args)
741 812
742This function (not method) lets you add your own types to C<push_read>. 813This function (not method) lets you add your own types to C<push_read>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines