The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

List::MRU - Perl module implementing a simple fixed-size MRU-ordered list.

SYNOPSIS

  use List::MRU;

  # Constructor
  $lm = List::MRU->new(max => 20);
  # Constructor with explicit 'eq' subroutine for obj equality tests
  $lm = List::MRU->new(max => 20, 'eq' => sub {
    $_[0]->stringify eq $_[1]->stringify
  });

  # Add item, moving to head of list if already exists
  $lm->add($item);

  # Iterate in most-recently-added order
  for my $item ($lm->list) {
    print $item;
  }

  # Accessors
  $max = $lm->max;        # max items in list
  $count = $lm->count;    # current items in list

DESCRIPTION

Perl module implementing a simple fixed-size most-recently-used- (MRU)-ordered list of values/objects. Well, really it's a most- recently-added list - items added to the list are just promoted to the front of the list if they already exist, otherwise they are added there.

Works fine with with non-scalar items, but you will need to supply an explicit 'eq' subroutine to the constructor to handle testing for the 'same' object (or alternatively have overloaded the 'eq' operator for your object).

SEE ALSO

Tie::Cache::LRU, which was kind of what I wanted, but didn't retain ordering.

AUTHOR

Gavin Carr <gavin@openfusion.com.au>

COPYRIGHT AND LICENSE

Copyright 2005 by Open Fusion Pty. Ltd.

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