NAME

Catalyst::View::EmbeddedPerl::PerRequest::EachInfo - Helper class for iteration metadata

SYNOPSIS

use Catalyst::View::EmbeddedPerl::PerRequest::EachInfo;

my $info = Catalyst::View::EmbeddedPerl::PerRequest::EachInfo->new($current_index, $total_count);

# Access current index and total length
my $current = $info->current;
my $total   = $info->total;

# Conditional checks
if ($info->is_first) { ... }
if ($info->is_last)  { ... }
if ($info->is_even)  { ... }

# Callback-based conditionals
$info->if_first(sub { print "First element!\n" });
$info->if_even(sub { print "Even index!\n" });

DESCRIPTION

This class provides metadata and utility methods for managing information about the current index and total length in a loop or iterable context.

METHODS

new

my $info = Catalyst::View::EmbeddedPerl::PerRequest::EachInfo->new($current_index, $total_count);

Constructor. Creates a new instance of the class. Accepts two arguments:

  • $current_index - The current index in the iteration.

  • $total_count - The total number of items in the iteration.

current

my $current = $info->current;

Returns the current index.

total

my $total = $info->total;

Returns the total number of items.

first_index

my $first = $info->first_index;

Returns the first index (always 0).

last_index

my $last = $info->last_index;

Returns the last index (total - 1).

is_first

if ($info->is_first) { ... }

Returns true if the current index is the first index.

is_not_first

if ($info->is_not_first) { ... }

Returns true if the current index is not the first index.

is_last

if ($info->is_last) { ... }

Returns true if the current index is the last index.

is_not_last

if ($info->is_not_last) { ... }

Returns true if the current index is not the last index.

is_even

if ($info->is_even) { ... }

Returns true if the current index is even.

is_odd

if ($info->is_odd) { ... }

Returns true if the current index is odd.

if_even

$info->if_even(sub { print "Even index!\n" });

Executes the provided callback if the current index is even. Returns the result of the callback if executed, otherwise an empty string.

if_odd

$info->if_odd(sub { print "Odd index!\n" });

Executes the provided callback if the current index is odd. Returns the result of the callback if executed, otherwise an empty string.

if_first

$info->if_first(sub { print "This is the first element!\n" });

Executes the provided callback if the current index is the first index. Returns the result of the callback if executed, otherwise an empty string.

if_last

$info->if_last(sub { print "This is the last element!\n" });

Executes the provided callback if the current index is the last index. Returns the result of the callback if executed, otherwise an empty string.

if_not_first

$info->if_not_first(sub { print "Not the first element!\n" });

Executes the provided callback if the current index is not the first index. Returns the result of the callback if executed, otherwise an empty string.

if_not_last

$info->if_not_last(sub { print "Not the last element!\n" });

Executes the provided callback if the current index is not the last index. Returns the result of the callback if executed, otherwise an empty string.

AUTHOR

John Napiorkowski <jjnapiork@cpan.org>

LICENSE

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