| 1 | package Net::ISC::DHCPd::Config::Block; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Net::ISC::DHCPd::Config::Block - Unknown config blocks | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | See L<Net::ISC::DHCPd::Config::Role> for methods and attributes without | ||
| 10 | documentation. | ||
| 11 | |||
| 12 | An instance from this class, comes from / will produce: | ||
| 13 | |||
| 14 | $type $name { | ||
| 15 | $body_attribute_value | ||
| 16 | } | ||
| 17 | |||
| 18 | $type "$string" { | ||
| 19 | $body_attribute_value | ||
| 20 | } | ||
| 21 | |||
| 22 | IMPORTANT! "Blocks" may be redefined to a "real" instance later on. | ||
| 23 | This is simply here as a "catch-all" feature, in case something | ||
| 24 | could not be parsed. | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | See L<Net::ISC::DHCPd::Config/SYNOPSIS>. | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | use Moose; | ||
| 33 | |||
| 34 | with 'Net::ISC::DHCPd::Config::Role'; | ||
| 35 | |||
| 36 | =head1 ATTRIBUTES | ||
| 37 | |||
| 38 | =head2 type | ||
| 39 | |||
| 40 | See L</SYNOPSIS>. | ||
| 41 | |||
| 42 | =head2 name | ||
| 43 | |||
| 44 | See L</SYNOPSIS>. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | has [qw/ type name /] => ( | ||
| 49 | is => 'ro', | ||
| 50 | isa => 'Str', | ||
| 51 | ); | ||
| 52 | |||
| 53 | =head2 quoted | ||
| 54 | |||
| 55 | This flag tells if the name should be quoted or not. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | has quoted => ( | ||
| 60 | is => 'ro', | ||
| 61 | isa => 'Bool', | ||
| 62 | ); | ||
| 63 | |||
| 64 | =head2 body | ||
| 65 | |||
| 66 | The body of the block, without trailing newline at end. | ||
| 67 | This text is not parsed, so the containing text can be anything. | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | has _body => ( | ||
| 72 | is => 'rw', | ||
| 73 | isa => 'ArrayRef', | ||
| 74 | default => sub { [] }, | ||
| 75 | ); | ||
| 76 | |||
| 77 | sub body { join "\n", @{ shift->_body } } | ||
| 78 | |||
| 79 | =head2 regex | ||
| 80 | |||
| 81 | See L<Net::ISC::DHCPd::Config::Role/regex>. | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | our $regex = qr/^\s* ([\w-]+) \s+ (\S*) \s* { /x; | ||
| 86 | |||
| 87 | has _depth => ( | ||
| 88 | is => 'ro', | ||
| 89 | isa => 'Int', | ||
| 90 | traits => ['Counter'], | ||
| 91 | default => 1, | ||
| 92 | handles => { | ||
| 93 | _inc_depth => 'inc', | ||
| 94 | _dec_depth => 'dec', | ||
| 95 | }, | ||
| 96 | ); | ||
| 97 | |||
| 98 | =head1 METHODS | ||
| 99 | |||
| 100 | =head2 BUILD | ||
| 101 | |||
| 102 | Will convert body (as string) in C<new()> into a list which is used | ||
| 103 | internally. | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | sub BUILD { | ||
| 108 | my($self, $args) = @_; | ||
| 109 | push @{ $self->_body }, split /\n/, $args->{'body'} if(defined $args->{'body'}); | ||
| 110 | } | ||
| 111 | |||
| 112 | =head2 slurp | ||
| 113 | |||
| 114 | This method is used by L<Net::ISC::DHCPd::Config::Role/parse>, and will | ||
| 115 | slurp the content of the function, instead of trying to parse the | ||
| 116 | statements. | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub slurp { | ||
| 121 | my($self, $line) = @_; | ||
| 122 | |||
| 123 | $self->_inc_depth if($line =~ /{/); | ||
| 124 | $self->_dec_depth if($line =~ /}/); | ||
| 125 | |||
| 126 | if($self->_depth) { | ||
| 127 | chomp $line; | ||
| 128 | $line =~ s/^\s{4}//; | ||
| 129 | push @{ $self->_body }, $line; | ||
| 130 | return 'next'; | ||
| 131 | } | ||
| 132 | else { | ||
| 133 | return 'last'; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | =head2 captured_to_args | ||
| 138 | |||
| 139 | See L<Net::ISC::DHCPd::Config::Role/captured_to_args>. | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | sub captured_to_args { | ||
| 144 | my($type, $name) = @_; | ||
| 145 | my $quoted = $name =~ s/^"(.*)"$/$1/ ? 1 : 0; | ||
| 146 | |||
| 147 | return { type => $type, name => $name, quoted => $quoted } | ||
| 148 | } | ||
| 149 | |||
| 150 | =head2 generate | ||
| 151 | |||
| 152 | See L<Net::ISC::DHCPd::Config::Role/generate>. | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | sub generate { | ||
| 157 | my $self = shift; | ||
| 158 | my $format = $self->quoted ? '%s "%s" {' : '%s %s {'; | ||
| 159 | |||
| 160 | return( | ||
| 161 | sprintf($format, $self->type, $self->name), | ||
| 162 | @{ $self->_body }, | ||
| 163 | '}', | ||
| 164 | ); | ||
| 165 | } | ||
| 166 | |||
| 167 | =head1 COPYRIGHT & LICENSE | ||
| 168 | |||
| 169 | =head1 AUTHOR | ||
| 170 | |||
| 171 | See L<Net::ISC::DHCPd>. | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | __PACKAGE__->meta->make_immutable; | ||
| 176 | |||
| 177 | 1; |