Ansible

 view release on metacpan or  search on metacpan

lib/Ansible.pm  view on Meta::CPAN


sub contextcount {
    my $self = shift;
    my (@a) = $self->setcontext(@_);
    printf STDERR "CONTEXTCOUNT = %d\n", scalar(@a) if $debug_context;
    print STDERR map { "CC: $_\n" } @a if $debug_context;
    return scalar(@a);
}

sub unsetcontext {
    my $self = shift;
    return(("exit") x $self->contextcount(@_));
}

sub teql {
    my ($self, $b) = @_;
    my $a = $self->text;
    $a =~ s/^\s+/ /g;
    $a =~ s/^ //;
    $a =~ s/ $//;
    chomp($a);
    $b =~ s/^\s+/ /g;
    $b =~ s/^ //;
    $b =~ s/ $//;
    chomp($b);
    return $a eq $b;
}

sub set {
    my $self = shift;
    my $new = pop;
    my (@designators) = @_;
    #my ($self, $designator, $new) = @_;
    print STDERR "\nSET\n" if $debug_set;
    return undef unless $self;
    my $old;
    #my @designators;
    print STDERR "\nSELF $self->{$debg}" if $debug_set;
    # move into the block if possible
    $self = $self->subs
        if $self->subs;
    print STDERR "\nSELF $self->{$debg}" if $debug_set;
    #if (ref $designator eq 'ARRAY') {
    #	@designators = @$designator;
    #	$old = $self->get(@designators);
    #	$designator = pop(@designators);
    #} elsif ($designator) {
    #	$old = $self->get($designator);
    #} else {
    #	$old = $self;
    #}
    my $designator;
    if ( @designators ) {
        $old = $self->get(@designators);
        $designator = pop(@designators);
    }
    else {
        $old = $self;
    }
    print STDERR "\nOLD $old->{$debg}" if $debug_set;
    my (@lines) = expand(grep (/./, split(/\n/, $new)));
    if ( $lines[0] =~ /^(\s+)/ ) {
        my $ls = $1;
        my $m = 1;
        map { substr($_, 0, length($ls)) eq $ls or $m = 0 } @lines;
        map { substr($_, 0, length($ls)) = '' } @lines
            if $m;
    }
    my $indent = (' ' x $self->contextcount(@designators));
    for $_ ( @lines ) {
        s/(\S)\s+/$1 /g;
        s/\s+$//;
        $_ = 'exit' if /^\s*!\s*$/;
        $_ = "$indent$_";
    }
    print STDERR "SET TO {\n@lines\n}\n" if $debug_set;
    my $desig = shift(@lines);
    my @o;
    undef $old
        if ! $old;
    if ( ! $old ) {
        print STDERR "NO OLD\n" if $debug_set;
        push(@o, openangle($self->setcontext(@designators)));
        push(@o, $desig);
    }
    elsif ( ! $designator && ! looks_like_a_block($desig, @lines) ) {
        if ( $self->block && $self->context ) {
            unshift(@lines, $desig);
            $old = $self->context;
            undef $desig;
        }
        else {
            unshift(@lines, $desig);
            print STDERR "IN NASTY BIT\n" if $debug_set;
            #
            # this is a messy situation: we've got a random
            # block of stuff to set inside a random block.
            # In theorey we could avoid the die, I'll leave
            # that as an exercise for the reader.
            #
            confess "You cannot set nested configurations with set(undef, \$config) -- use a designator on the set method"
                if grep (/^$indent\s/, @lines);
            my (@t) = split(/\n/, $self->text);
            my (%t);
            @t{strim(@t)} = @t;
            while ( @lines ) {
                my $l = strim(shift(@lines));
                if ( $t{$l} ) {
                    delete $t{$l};
                }
                else {
                    push(@o, "$indent$l");
                }
            }
            for my $k ( keys %t ) {
                unshift(@o, iinvert($indent, $k));
            }
            unshift(@o, $self->setcontext)
                if @o;
        }
    }



( run in 0.577 second using v1.01-cache-2.11-cpan-5b529ec07f3 )