# https://perlmonks.org/index.pl?node_id=11132953 use strict; use warnings; use Data::Dump qw/pp dd/; my $obj = "CiscoService"; while () { chomp; my ($indent,$data) = /^(\s*)(.*)$/; my ($call, @args) = split/\s+/, $data; $call =~ s/^!$/__out__/; $call =~ s/-/_/g; if ( my $meth = $obj->can($call) ) { $obj = $meth->( $obj, length($indent),@args) } else { warn "<$call> not impemented"; } } package CiscoService; use Data::Dump qw/pp dd/; sub service { my ($self,$indent,@args) = @_; return bless {__head__ => \@args, sub=>{} },__PACKAGE__; } sub description { my ($self,$indent,@args) = @_; $self->{sub}{description} = \@args; return $self; } sub service_policy { my ($self,$indent,@args) = @_; $self->{sub}{"service-policy"} = \@args; return $self; } sub encapsulation { my ($self,$indent,@args) = @_; $self->{sub}{encapsulation} = \@args; return $self; } sub rewrite { my ($self,$indent,@args) = @_; $self->{sub}{rewrite} = \@args; return $self; } sub xconnect { my ($self,$indent,@args) = @_; $self->{sub}{xconnect} = {head =>\@args, sub=>{}}; return $self; } sub mtu { my ($self,$indent,@args) = @_; $self->{sub}{xconnect}{sub}{mtu} = \@args; return $self; } sub __out__ { my ($self,$indent,@args) = @_; pp $self; return "CiscoService"; } package main; __DATA__ service instance 20 ethernet description Internal site encapsulation dot1q 17 rewrite ingress tag pop 1 symmetric xconnect 192.168.51.20 838969735 encapsulation mpls mtu 9202 ! service instance 21 ethernet description Client 1 important encapsulation dot1q 18 rewrite ingress tag pop 1 symmetric xconnect 192.168.51.21 838969736 encapsulation mpls mtu 9202 ! service instance 22 ethernet description Client other 2 encapsulation dot1q 22 rewrite ingress tag pop 1 symmetric xconnect 192.168.56.22 838925386 encapsulation mpls mtu 9202 ! service instance 23 ethernet description Client name with fun stuff encapsulation dot1q 3200 second-dot1q 5 rewrite ingress tag pop 2 symmetric service-policy input CoS_222_333_23 xconnect 192.168.56.23 571125465 encapsulation mpls mtu 9202 ! service instance 24 ethernet description Client - not as important encapsulation dot1q 3200 second-dot1q 6 rewrite ingress tag pop 2 symmetric service-policy input 222_444_24 xconnect 192.168.56.24 571637077 encapsulation mpls mtu 9202 !