File Coverage

blib/lib/Piper/Queue.pm
Criterion Covered Total %
statement 30 30 100.0
branch n/a
condition 2 2 100.0
subroutine 10 10 100.0
pod 4 4 100.0
total 46 46 100.0


line stmt bran cond sub pod time code
1             #####################################################################
2             ## AUTHOR: Mary Ehlers, regina.verbae@gmail.com
3             ## ABSTRACT: Simple FIFO queue used by Piper
4             #####################################################################
5              
6             package Piper::Queue;
7              
8 3     3   1879 use v5.10;
  3         8  
9 3     3   95 use strict;
  3         6  
  3         61  
10 3     3   10 use warnings;
  3         6  
  3         81  
11              
12 3     3   434 use Types::Standard qw(ArrayRef);
  3         58730  
  3         18  
13              
14 3     3   1930 use Moo;
  3         8018  
  3         16  
15 3     3   2061 use namespace::clean;
  3         8616  
  3         16  
16              
17             with 'Piper::Role::Queue';
18              
19             our $VERSION = '0.05'; # from Piper-0.05.tar.gz
20              
21             #pod =head1 SYNOPSIS
22             #pod
23             #pod =for stopwords dequeued
24             #pod
25             #pod use Piper::Queue;
26             #pod
27             #pod my $queue = Piper::Queue->new();
28             #pod $queue->enqueue(qw(x y));
29             #pod $queue->ready; # 2
30             #pod $queue->dequeue; # 'x'
31             #pod $queue->requeue('x');
32             #pod $queue->dequeue; # 'x'
33             #pod
34             #pod =head1 CONSTRUCTOR
35             #pod
36             #pod =head2 new
37             #pod
38             #pod =cut
39              
40             has queue => (
41             is => 'ro',
42             isa => ArrayRef,
43             default => sub { [] },
44             );
45              
46             #pod =head1 METHODS
47             #pod
48             #pod =head2 dequeue($num)
49             #pod
50             #pod Remove and return at most C<$num> items from the queue. The default S is 1>.
51             #pod
52             #pod If C<$num> is greater than the number of items remaining in the queue, only the number remaining will be dequeued.
53             #pod
54             #pod Returns an array of items if wantarray, otherwise returns the last of the dequeued items, which allows singleton dequeues:
55             #pod
56             #pod my @results = $queue->dequeue($num);
57             #pod my $single = $queue->dequeue;
58             #pod
59             #pod =cut
60              
61             sub dequeue {
62 179     179 1 14298 my ($self, $num) = @_;
63 179   100     336 $num //= 1;
64 179         184 splice @{$self->queue}, 0, $num;
  179         669  
65             }
66              
67             #pod =head2 enqueue(@items)
68             #pod
69             #pod Add C<@items> to the queue.
70             #pod
71             #pod =cut
72              
73             sub enqueue {
74 184     184 1 3111 my $self = shift;
75 184         215 push @{$self->queue}, @_;
  184         762  
76             }
77              
78             #pod =head2 ready
79             #pod
80             #pod Returns the number of elements in the queue.
81             #pod
82             #pod =cut
83              
84             sub ready {
85 1022     1022 1 25032 my ($self) = @_;
86 1022         963 return scalar @{$self->queue};
  1022         6442  
87             }
88              
89             #pod =head2 requeue(@items)
90             #pod
91             #pod Inserts C<@items> to the top of the queue in an order such that C would subsequently return C<$items[0]> and so forth.
92             #pod
93             #pod =cut
94              
95             sub requeue {
96 8     8 1 1862 my $self = shift;
97 8         11 unshift @{$self->queue}, @_;
  8         30  
98             }
99              
100             1;
101              
102             __END__