Mojolicious::Plugin::Loop - Loop plugin for Mojolicious
use Mojolicious::Lite; plugin 'loop';
%= loop [1,2,3,4], begin --- key/val: <%= loop->key %>/<%= loop->val %> count: <%= loop->index %> + 1 = <%= loop->count %> (index + 1) size: <%= loop->max %> + 1 = <%= loop->size %> (max + 1) prev: <%= loop->peek(-1) // 'undef' %> (peek -1) next: <%= loop->peek(1) // 'undef' %> (peek +1) parity: <%= loop->parity %> odd/even: <%= loop->odd ? 'odd' : loop->even ? 'even' : 'unknown' %> first: <%= loop->first ? 'yes' : 'no' %> last: <%= loop->last ? 'yes' : 'no' %> % end %= loop {a => 1, b => 2, c => 3}, begin --- key/val: <%= loop->key %>/<%= loop->val %> count: <%= loop->index %> + 1 = <%= loop->count %> (index + 1) size: <%= loop->max %> + 1 = <%= loop->size %> (max + 1) prev: <%= loop->peek(-1) // 'undef' %> (peek -1) next: <%= loop->peek(1) // 'undef' %> (peek +1) parity: <%= loop->parity %> odd/even: <%= loop->odd ? 'odd' : loop->even ? 'even' : 'unknown' %> first: <%= loop->first ? 'yes' : 'no' %> last: <%= loop->last ? 'yes' : 'no' %> % end
Mojolicious::Plugin::Loop is a plugin with helpers for iterating over either array, hashes or array/hash-like structures.
NOTE: THIS MODULE IS EXPERIMENTAL AND THE API MAY CHANGE AT ANY TIME
$int = $loop->count;
Returns "index" + 1.
$bool = $loop->even;
Returns true if "count" is 2, 4, 6, ...
$bool = $loop->first;
Returns true if "index" is zero.
$int = $loop->index;
Returns the index number, starting on 0.
$str = $self->key; # hash $int = $self->key; # array
Returns "index" if iterating over an array or the current key if iterating over a hash.
$bool = $loop->last;
Returns true if "index" is "max".
$int = $loop->max;
Returns "size" - 1.
$bool = $self->next;
Move the iterator forward one step. Example:
% my $i = loop [1, 2, 3]; % while ($i->next) { %= $i->val; % }
$bool = $loop->odd;
Returns true if "count" is 1, 3, 5, ...
$str = $loop->parity;
Returns either the string "odd" or "even".
$any = $loop->peek($index); $any = $loop->peek(-3);
Returns either the value in the array, or the key in the hash, relative to the current item. Examples:
# [24, 25, 26] $loop->index == 2 $loop->peek(-1) == 25 # {a => 24, b => 25, c => 26} $loop->index == 1 $loop->peek(1) == "c"
$self = $self->reset;
Used to reset the iterator.
$int = $loop->size;
Returns the number of items in the array, or number of keys in the hash.
$any = $loop->val;
Returns the value of the current item in the array or hash.
Used to register the plugin in the Mojolicious application.
Jan Henning Thorsen
Marcus Ramberg
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
Template::Iterator.
To install Mojolicious::Plugin::Loop, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::Loop
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::Loop
For more information on module installation, please visit the detailed CPAN module installation guide.