Michael Shipper > Data-Range-Compare-Stream-4.027 > Data::Range::Compare::Stream::Cookbook::FILE_RESULT_EXAMPLE


Annotate this POD


New  1
Open  0
View/Report Bugs
Source   Latest Release: Data-Range-Compare-Stream-4.029


Data::Range::Compare::Stream::Cookbook::FILE_RESULT_EXAMPLE - Doc on how to build file result iterators


This section shows how to roll your own Consolidation result iterator, based on file iteration

Special notes ^

This example assumes your data contains no overlaps no duplicates and is pre-sorted in the desired order

Implementing a new Data::Range::Compare::Stream::Iterator::Consolidate class

Source File

  1 2
  3 5
  9 11
  12 12
  98 174

Example package

  package MyIterator;
  use strict;
  use warnings;
  use IO::File;
  use IO::Select;
  use base qw(Data::Range::Compare::Stream::Iterator::Base);
  use Data::Range::Compare::Stream::Iterator::Consolidate::Result;
  use Data::Range::Compare::Stream;
  sub new {
    my ($class,%args)=@_;
    my $has_next;
    my $self=$class->SUPER::new(%args);
    if(defined($args{filename})) {
      my $fh=IO::File->new($args{filename});
      if($fh) {
         my $line=$fh->getline;
      } else {
        $self->{msg}="Error could not open $args{filename} error was: $!";
    return $self;
  sub get_next {
    my ($self)=@_;
    return undef unless $self->has_next;
    my $line=$self->{next_line};
    chomp $line;
    my $range=new Data::Range::Compare::Stream(split /\s+/,$line);
    return new Data::Range::Compare::Stream::Iterator::Consolidate::Result($range,$range,$range);


Putting it all togeather

Now we can use the result iterator.

  use strict;
  use warnings;
  use MyIterator;

  my $iterator=new MyIterator(filename=>'file_example.src');
  while($iterator->has_next) {
    print $iterator->get_next,"\n";


Michael Shipper

Source-Forge Project ^

As of version 0.001 the Project has been moved to Source-Forge.net

Data Range Compare https://sourceforge.net/projects/data-range-comp/


Copyright 2011 Michael Shipper. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: