List::Priority - Perl extension for a list that manipulates objects by their priority
use List::Priority; # Create an instance my $list = List::Priority->new(); # Insert some elements, each woth a unique priority $list->insert(2,'World!'); $list->insert(5,'Hello'); $list->insert(3,' '); # Print while (my $element = $list->pop()) { print $element; }
If you want to handle multiple data bits by their order of importance - This one's for you.
Logic: Precedence to highest priority object. If more than one object hold the highest priority - FIFO is king.
Duplicate objects are currently not allowed.
I'd like to thank Joseph N. Hall and Randal L. Schwartz for their excellent book "Effective Perl Programming" for one of the code hacks...
$p_list = List::Priority->new();
new is the constructor for List::Priority objects
Arguments:
- Accepts an Key-Value list with the list attributes.
Key: SIZE - The maximum size of the list. Inserting after the size is reached will result either in a no-op, or the removal of the most recent lowest priority objects - according to the insert()'s priority. Example : $list = List::Priority->new(SIZE => 10);
$result = $p_list->insert($priority, $scalar);
Inserts the scalar to the list
1. Priority must be numeric. 2. Scalar can be any scalar, including references (objects)
Return value:
1 on success, a string describing the error upon failure
$object = $p_list->pop();
Extracts the scalar from the list according to the specified logic.
- Optional - The specific priority value to pop from, instead of the most important one.
Example : $best_object_p3 = $list->pop(3);
The object on success, undef upon failure
$object = $p_list->shift();
Extracts the scalar from the list according to the reversed specified logic (least worthy first).
- Optional - The specific priority value to shift from, instead of the least important one.
Example : $worst_object_p3 = $list->shift(3);
None. All interfaces are OO.
Well, I'm dangerously neglecting the testing bit...
Eyal Udassin, <eyaludassin@hotmail.com>
Set::Scalar.
1 POD Error
The following errors were encountered while parsing the POD:
You forgot a '=back' before '=head1'
To install List::Priority, copy and paste the appropriate command in to your terminal.
cpanm
cpanm List::Priority
CPAN shell
perl -MCPAN -e shell install List::Priority
For more information on module installation, please visit the detailed CPAN module installation guide.