File: iterate.pm

package info (click to toggle)
libcatmandu-perl 1.2024-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,552 kB
  • sloc: perl: 17,037; makefile: 24; sh: 1
file content (95 lines) | stat: -rw-r--r-- 1,682 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package Catmandu::Fix::Bind::iterate;

use Catmandu::Sane;

our $VERSION = '1.2024';

use Moo;
use Catmandu::Util;
use Catmandu::Fix::Has;
use namespace::clean;

with 'Catmandu::Fix::Bind', 'Catmandu::Fix::Bind::Group';

has start => (fix_opt => 1);
has end   => (fix_opt => 1);
has step  => (fix_opt => 1);
has var   => (fix_opt => 1);

sub unit {
    my ($self, $data) = @_;
    $data;
}

sub bind {
    my ($self, $mvar, $func, $name, $fixer) = @_;

    my $start = $self->start;
    my $end   = $self->end;
    my $step  = $self->step // 1;
    my $var   = $self->var;

    if (   Catmandu::Util::is_number($start)
        && Catmandu::Util::is_number($end)
        && Catmandu::Util::is_number($step))
    {
        for (my $i = $start; $i <= $end; $i = $i + $step) {
            $mvar->{$var} = $i if defined($var);
            $mvar = $func->($mvar);
        }
    }

    delete $mvar->{$var} if defined($var);

    return $mvar;
}

1;

__END__

=pod

=head1 NAME

Catmandu::Fix::Bind::iterate - a binder iterates fixes in a loop

=head1 SYNOPSIS


     # Create:
     #   numbers = [1,2,3,4,5,6,7,8,9,10]
     do iterate(start:1, end: 10, step: 1, var: i)
        copy_field(i,numbers.$append)
     end

=head1 DESCRIPTION

The list binder will iterate over all the elements in a list and fixes the
values in context of that list.

=head1 CONFIGURATION

=head2 start

Start value of the iterator.

=head2 end

End value of the iterator.

=head2 step

Increase the interator with this value for every step.

=head3 var

Optional variable holding the value of the current step

=head1 SEE ALSO

L<Catmandu::Fix::Bind> ,
L<Catmandu::Fix::list> ,
L<Catmandu::Fix::with> ,

=cut