#!/usr/bin/perl # -*- Encoding: utf-8; Mode: cperl -*- use strict; use utf8; use Storable qw(freeze dclone); use Benchmark qw(:all) ; use POE::Filter::Line; my @s=map {join '', map { 'SEPARATOR'.'data'x(100+int(rand(100))) } (1..100)} (1..10); cmpthese(100, { Regexp => sub { my $data=dclone(\@s); my $f=POE::Filter::Regexp->new(qr/SEPARATOR/); $f->get($data); }, Index => sub { my $data=dclone(\@s); my $f=POE::Filter::Index->new(qr/SEPARATOR/); $f->get($data); }, Line => sub { my $data=dclone(\@s); my $f=POE::Filter::Line->new(InputRegexp => qr/SEPARATOR/); $f->get($data); }, }); package POE::Filter::Regexp; use strict; use vars qw($VERSION); $VERSION='Production 1.0'; sub new { my ($class,$re)=@_; $re ||= qr/\n/; die "Param in new must be a Regexp but this is a ".ref($re) unless ref $re eq 'Regexp'; return bless [ [], # ready queue '', # raw unparsed data $re, ], $class; } sub get { my ($self, $stream) = @_; my @ret; while($_=shift @$stream){ $self->[1].=$_; $_=''; #yah we'r cl'r mems !!!111 my $p=0; while($self->[1]=~/$self->[2]/gm) { next unless $-[0]; #begin of stream push @ret, substr($self->[1], $p, $-[0]-$p); $p=$-[0]; } substr($self->[1], 0, $p)=''; } $self->[1]=''.$self->[1]; #Clean holes in string. return \@ret; } package POE::Filter::Index; use strict; use vars qw($VERSION); $VERSION='Devel'; sub new { my ($class,$sep)=@_; $sep ||= "\n"; return bless [ [], # ready queue '', # raw unparsed data $sep, ], $class; } sub get { my ($self, $stream) = @_; my @ret; while($_=shift @$stream){ $self->[1].=$_; $_=''; #yah we'r cl'r mems 4 4ture iterations !!!111 one one my $b=0; #base my $p; while(($p=index($self->[1], $self->[2], $b+1)) >0) { push @ret, substr($self->[1], $b, $p-$b); $b=$p; } substr($self->[1], 0, $b)=''; } $self->[1]=''.$self->[1]; #Clean holes in string. return \@ret; }