summaryrefslogtreecommitdiffabout
path: root/lib/Config/HAProxy/Iterator.pm
blob: 6b0208e59b26f73589487b48c1a64d94c092509e (plain)
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
package Config::HAProxy::Iterator;
use strict;
use warnings;
use Config::HAProxy::Node;
use Carp;

use constant {
    NO_RECURSION => 0,
    INORDER => 1,
    POSTORDER => 2
};

sub new {
    my $class = shift;
    my $node = shift;
    my $self = bless { }, $class;
    if ($node->is_section) {
	$self->{_list} = [ $node->tree() ];
    } else {
	$self->{_list} = [ $node ];
    }
    local %_ = @_;
    if (defined($_{recursive})) {
	$self->{_recursive} = $_{recursive};
    } elsif ($_{inorder}) {
	$self->{_recursive} = INORDER;
    } elsif ($_{postorder}) {
	$self->{_recursive} = POSTORDER;
    } else {
	$self->{_recursive} = NO_RECURSION;
    }
    return $self;
}

sub recursive  { shift->{_recursive} }
sub inorder  { shift->{_recursive} == INORDER }
sub postorder { shift->{_recursive} == POSTORDER } 

sub next {
    my $self = shift;

    if ($self->{_itr}) {
	if (defined(my $v = $self->{_itr}->next())) {
	    return $v;
	} else {
	    delete $self->{_itr};
	    return $self->{_cur} if $self->postorder;
	}
    }

    if (defined($self->{_cur} = shift @{$self->{_list}})) {
	if ($self->recursive && $self->{_cur}->is_section) {
	    $self->{_itr} = $self->{_cur}->iterator(recursive => $self->recursive);
	    if ($self->inorder) {
		return $self->{_cur};
	    } else {
		return $self->next();
	    }
	}
    }

    return $self->{_cur};
}

1;

	    

	

Return to:

Send suggestions and report system problems to the System administrator.