Algorithm-SlidingWindow

 view release on metacpan or  search on metacpan

lib/Algorithm/SlidingWindow.pm  view on Meta::CPAN


    $w->clear;

=head1 DESCRIPTION

C<Algorithm::SlidingWindow> implements a fixed-capacity sliding window
using an array-backed circular buffer.

When the window reaches capacity and new elements are added, the oldest
elements are automatically evicted. Eviction is normal behavior and is
not considered an error.

The module is designed to:

=over 4

=item *

Handle all Perl scalar types equally (numbers, strings, references, objects)

=item *

t/basic.t  view on Meta::CPAN


use lib 'lib';
use Algorithm::SlidingWindow;

sub dies_like {
    my ($code, $re) = @_;
    my $ok = eval { $code->(); 1 };
    my $err = $@;

    ok(!$ok, "dies as expected");
    like($err, $re, "error matches");
}


# --- constructor validation ---

dies_like(sub { Algorithm::SlidingWindow->new() }, qr/capacity is required/);
dies_like(sub { Algorithm::SlidingWindow->new(capacity => 0) }, qr/capacity must be > 0/);
dies_like(sub { Algorithm::SlidingWindow->new(capacity => 'x') }, qr/capacity must be a positive integer/);
dies_like(sub { Algorithm::SlidingWindow->new(capacity => 5, nope => 1) }, qr/unknown argument 'nope'/);
dies_like(sub { Algorithm::SlidingWindow->new(capacity => 5, on_evict => 123) }, qr/on_evict must be a CODE reference/);



( run in 1.650 second using v1.01-cache-2.11-cpan-c966e8aa7e8 )