Data::Page::Viewport 1.02 review

Download
by rbytes.net on

Data::Page::Viewport is a Perl module to scroll thru data a page, or just an item, at a time. Synopsis This is a complete, test

License: Perl Artistic License
File size: 7K
Developer: Ron Savage
0 stars award from rbytes.net

Data::Page::Viewport is a Perl module to scroll thru data a page, or just an item, at a time.

Synopsis

This is a complete, tested, runnable program.
#!/usr/bin/perl

use strict;
use warnings;

use Data::Page::Viewport;

# -----------------------------------------------

my(@data) = (qw/zero one two three four five six
seven eight nine ten eleven twelve thirteen fourteen/);
my($page) = Data::Page::Viewport -> new
(
data_size => scalar @data,
page_size => 4
);

print "Data bounds: 0 .. $#data. n";
print "Data: ", join(', ', @data), ". n";
print "Page bounds: 0 .. 3. n";
print "Page data: ", join(', ', @data[0 .. 3]), ". n";
print "n";

my(@bound);

for (-2, 1, 4, 4, 1, 3, 3, -2, 1, 2, 1, -4, -4,
-1, 1, 2, -1, -2, -2, -1, -4, 4, 4, 4)
{
print "Offset: $_. n";

@bound = $page -> offset($_) -> bounds();

print "Page bounds: $bound[0] .. $bound[1]. n";
print 'Page data: ',
join(', ', @data[$bound[0] .. $bound[1] ]),
". n";
print '-' x 50, "n";
}

Data::Page::Viewport is a pure Perl module.

This module keeps track of what items are on the 'current' page, when you scroll forwards or backwards within a data set.

Similarly to Data::Page, you can call sub offset(N), for + or - N, to scroll thru the data a page at a time.

And, like Set::Window, you can call sub offset(N), for + or - 1, to scroll thru the data an item at a time.

Clearly, N does not have to be fixed.

The viewport provides access to the 'current' page, and the code shifts indexes into and out of the viewport, according to the parameter passed to sub offset().

Note that the data is not passed into this module. The module only keeps track of the indexes within the viewport, i.e. indexes on the 'current' page.

You call sub bounds() on the object (of type Set::Window) returned by sub offset(), to determine what indexes are on the 'current' page at any particular point in time.

Also note that, unlike Set::Window, the boundaries of the viewport are rigid, so that changes to the indexes caused by sub offset() are limited by the size of the data set.

This means, if you do this:

my($page) = Data::Page::Viewport -> new
(
data_size => $#data, # 0 .. $#data.
page_size => $page_size, # 1 .. N.
);

my(@bound) = $page -> offset(- 1) -> bounds();

the call to sub offset(- 1) will have no effect.

That is, when trying to go back past the beginning of the data set, the bounds will be locked to values within 0 .. data_size.

Similarly, a call which would go beyond the other end of the data set, will lock the bounds to the same range.

In short, you can't fall off the edge by calling sub offset().

This in turn means that the values returned by sub bounds() will always be valid indexes within the range 0 .. data_size.

The module implements this by building 2 objects of type Set::Window, one for the original data set (which never changes), and one for the 'current' page, which changes each time sub offset() is called (until the boundaries are hit, of course).

Note: No range checking is performed on the parameters to sub new().

Note: It should be obvious by now that this module differs from Data::Page, and indeed all such modules, in that they never change the items which are on a given page. They only allow you to change the page known as the 'current' page. This module differs, in that, by calling sub offset(+ or - N), you are effectively changing the items which are deemed to be on the 'current' page.

Requirements:
Perl

Data::Page::Viewport 1.02 keywords