| 1 | package Net::ISC::DHCPd::Config::Role; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Net::ISC::DHCPd::Config::Role - Role with generic config methods and attributes | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | This role contains common methods and attributes for each of the config | ||||||||||||||||||||||||||||
| 10 | classes in the L<Net::ISC::DHCPd::Config> namespace. | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 WARNINGS | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | This module will warn when a line in the input config could not be parsed. | ||||||||||||||||||||||||||||
| 15 | This can be turned off by adding the line below before calling L</parse>. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | no warnings 'net_isc_dhcpd_config_parse'; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =cut | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 27 | ▶ | use Class::Load; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 27 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 27 | requires 'generate'; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head2 parent | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | The parent node in the config tree. This must be an object which does | ||||||||||||||||||||||||||||
| 31 | this role. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =cut | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 27 | has parent => ( | |||||||||||||||||||||||||||
| 36 | is => 'rw', | ||||||||||||||||||||||||||||
| 37 | does => 'Net::ISC::DHCPd::Config::Role', | ||||||||||||||||||||||||||||
| 38 | weak_ref => 1, | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head2 root | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | The root node in the config tree. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | has root => ( | ||||||||||||||||||||||||||||
| 48 | is => 'ro', | ||||||||||||||||||||||||||||
| 49 | isa => 'Object', | ||||||||||||||||||||||||||||
| 50 | weak_ref => 1, | ||||||||||||||||||||||||||||
| 51 | 26 | ▶ | default => sub { $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 27 | ); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head2 depth | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | Integer value that counts how far this node is from the root node. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 27 | has depth => ( | |||||||||||||||||||||||||||
| 61 | is => 'ro', | ||||||||||||||||||||||||||||
| 62 | isa => 'Int', | ||||||||||||||||||||||||||||
| 63 | lazy => 1, | ||||||||||||||||||||||||||||
| 64 | builder => '_build_depth', | ||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub _build_depth { | ||||||||||||||||||||||||||
|
Subroutine
_build_depth: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 68 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 69 | 0 | my $obj = $self; | |||||||||||||||||||||||||||
| 70 | 0 | my $i = 0; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | while($obj = $obj->parent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | $i++; | |||||||||||||||||||||||||||
| 74 | 0 | ▶ | last if($obj == $self->root); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | return $i; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head2 children | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Holds a list of possible child objects as objects. This list is used | ||||||||||||||||||||||||||||
| 83 | when L</parse> or L</generate_config_from_children> is called. | ||||||||||||||||||||||||||||
| 84 | The child list has a default value set from L</create_children> in each | ||||||||||||||||||||||||||||
| 85 | of the config modules. This is a static list, which reflects the actual | ||||||||||||||||||||||||||||
| 86 | documentation from C<dhcpd.conf(5)>. Example: | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | package Net::ISC::DHCPd::Config::Foo; | ||||||||||||||||||||||||||||
| 89 | __PACKAGE__->create_children("Net::ISC::DHCPd::Config::Host"); | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | package main; | ||||||||||||||||||||||||||||
| 92 | $config = Net::ISC::DHCPd::Config::Foo->new; | ||||||||||||||||||||||||||||
| 93 | $config->add_host({ ... }); | ||||||||||||||||||||||||||||
| 94 | @host_objects = $config->find_hosts({ ... }); | ||||||||||||||||||||||||||||
| 95 | $config->remove_host({ ... }); | ||||||||||||||||||||||||||||
| 96 | @host_objects = $config->hosts; | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | The L</create_children> method will autogenerate three methods and an | ||||||||||||||||||||||||||||
| 99 | attribute. The name of the attribute and methods will be the last part | ||||||||||||||||||||||||||||
| 100 | of the config class, with "s" at the end in some cases. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =over 4 | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =item foos | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | C<foos> is the name the attribute as well as the accessor. The accessor | ||||||||||||||||||||||||||||
| 107 | will auto-deref the array-ref to a list if called in list context. (yes: | ||||||||||||||||||||||||||||
| 108 | be aware of this!). | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =item add_foo | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Instead of pushing values directly to the C<foos> list, an C<add_foo> | ||||||||||||||||||||||||||||
| 113 | method is available. It can take either a hash, hash-ref or an object | ||||||||||||||||||||||||||||
| 114 | to add/construct a new child. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =item find_foos | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | This method will return zero or more objects as a list. It takes | ||||||||||||||||||||||||||||
| 119 | a hash-ref which will be matched against the object attributes of | ||||||||||||||||||||||||||||
| 120 | the children. | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =item remove_foo | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | This method will remove zero or more children from the C<foos> attribute. | ||||||||||||||||||||||||||||
| 125 | The method takes a hash-ref which will be used to match against the | ||||||||||||||||||||||||||||
| 126 | child list. It returns the number of child nodes actually matched and | ||||||||||||||||||||||||||||
| 127 | removed. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =back | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # should be overridden by anything that has children | ||||||||||||||||||||||||||||
| 134 | 185 | ▶ | sub children { } | ||||||||||||||||||||||||||
|
Subroutine
children: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # actual children | ||||||||||||||||||||||||||||
| 137 | has _children => ( | ||||||||||||||||||||||||||||
| 138 | is => 'ro', | ||||||||||||||||||||||||||||
| 139 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||
| 140 | 315 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 141 | 27 | ); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head2 comments | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | @str = $self->comments; | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Will return all the comments before this element appeared in the config file. | ||||||||||||||||||||||||||||
| 148 | The comments will not contain leading hash symbol spaces, nor trailing newline. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =cut | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | has _comments => ( | ||||||||||||||||||||||||||||
| 153 | is => 'ro', | ||||||||||||||||||||||||||||
| 154 | traits => ['Array'], | ||||||||||||||||||||||||||||
| 155 | init_arg => 'comments', | ||||||||||||||||||||||||||||
| 156 | 41 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 157 | 27 | handles => { | |||||||||||||||||||||||||||
| 158 | comments => 'elements', | ||||||||||||||||||||||||||||
| 159 | }, | ||||||||||||||||||||||||||||
| 160 | ); | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head1 CHILD METHODS | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head2 regex | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Regex used to scan a line of config text, which then spawns an | ||||||||||||||||||||||||||||
| 167 | a new node to the config tree. This is used inside l</parse>. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | THIS IS A STATIC METHOD. SELF is not used. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =cut | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 27 | has _filehandle => ( | |||||||||||||||||||||||||||
| 174 | is => 'ro', | ||||||||||||||||||||||||||||
| 175 | lazy_build => 1, | ||||||||||||||||||||||||||||
| 176 | ); | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 20 | ▶ | sub _build__filehandle { | ||||||||||||||||||||||||||
|
Subroutine
_build__filehandle: called CC=5 SCAR=17.6
| |||||||||||||||||||||||||||||
| 179 | 20 | my $self = shift; | |||||||||||||||||||||||||||
| 180 | 20 | my $file; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # get filehandle from parent to prevent seeking file from beginning | ||||||||||||||||||||||||||||
| 183 | 20 | ▶ | if(my $parent = $self->parent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | return $parent->_filehandle; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 20 | ▶ | if ($self->fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 18 | return $self->fh; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 2 | $file = $self->file; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 2 | ▶ | if($file->is_relative and !-e $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$file->is_relative and not -e $file
MC/DC0%0/2
$file->is_relative and not -e $file
$file->is_relative
not -e $file
Truth table33%1/3
A and not(B)
A= $file->is_relativeB= not -e $file
| |||||||||||||||||||||||||||||
| 194 | 0 | $file = Path::Class::File->new($self->root->file->dir, $file); | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 2 | return $file->openr; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =head1 METHODS | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head2 BUILD | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | Used to convert input arguments to child nodes. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =cut | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 315 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 210 | 315 | my($self, $args) = @_; | |||||||||||||||||||||||||||
| 211 | # skip the expensive method check if called through the parser. The only | ||||||||||||||||||||||||||||
| 212 | # time this code applies is with code like add_method( submethod => { } ); | ||||||||||||||||||||||||||||
| 213 | 315 | ▶ | return if ($args->{'parse'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | 41 | my $meta = $self->meta; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 41 | for my $key (sort keys %$args) { | |||||||||||||||||||||||||||
| 217 | 87 | my $list = $args->{$key}; | |||||||||||||||||||||||||||
| 218 | 87 | my $method = "add_$key"; | |||||||||||||||||||||||||||
| 219 | 87 | $method =~ s/s$//; | |||||||||||||||||||||||||||
| 220 | 87 | ▶ | if(ref $list eq 'ARRAY' and $meta->has_method($method)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $list eq "ARRAY" and $meta->has_method($method)
MC/DC50%1/2
ref $list eq "ARRAY" and $meta->has_method($method)
ref $list eq "ARRAY"
$meta->has_method($method)
Truth table66%2/3
A and B
A= ref $list eq "ARRAY"B= $meta->has_method($method)
| |||||||||||||||||||||||||||||
| 221 | 7 | for my $element (@$list) { | |||||||||||||||||||||||||||
| 222 | 7 | $self->$method($element); | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =head2 filename_callback | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | Callback for changing file paths when include files are on different relative paths. | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | # here is an example: | ||||||||||||||||||||||||||||
| 233 | my $cb = sub { | ||||||||||||||||||||||||||||
| 234 | my $file = shift; | ||||||||||||||||||||||||||||
| 235 | print "We're in callback and file is $file\n"; | ||||||||||||||||||||||||||||
| 236 | if ($file =~ /catphotos/) { | ||||||||||||||||||||||||||||
| 237 | return "/dog.conf"; | ||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | }; | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =cut | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 27 | has filename_callback => ( | |||||||||||||||||||||||||||
| 244 | is => 'rw', | ||||||||||||||||||||||||||||
| 245 | isa => 'CodeRef', | ||||||||||||||||||||||||||||
| 246 | ); | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =head2 parse | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | Will read a line of the time from the current config | ||||||||||||||||||||||||||||
| 251 | L<file|Net::ISC::DHCPd::Config::Root/file>. For each line, this method | ||||||||||||||||||||||||||||
| 252 | will loop though each object in L</children> and try to match the line | ||||||||||||||||||||||||||||
| 253 | against a given child and create a new node in the object graph if it | ||||||||||||||||||||||||||||
| 254 | match the L</regex>. This method is called recursively for each child | ||||||||||||||||||||||||||||
| 255 | when possible. | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =cut | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 109 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=18 SCAR=29.0
Pod
documented
| |||||||||||||||||||||||||||||
| 260 | 109 | my $self = shift; | |||||||||||||||||||||||||||
| 261 | 109 | my $fh = $_[1]; | |||||||||||||||||||||||||||
| 262 | 109 | my $linebuf = $_[2]; | |||||||||||||||||||||||||||
| 263 | 109 | my($n, @comments); | |||||||||||||||||||||||||||
| 264 | 109 | my $lines; | |||||||||||||||||||||||||||
| 265 | 109 | my $line_from_array=0; | |||||||||||||||||||||||||||
| 266 | # if $fh is a File::Temp or IO::File object then comparing on assignment | ||||||||||||||||||||||||||||
| 267 | # above makes it convert to a number (refaddr), which takes 4µs/call | ||||||||||||||||||||||||||||
| 268 | 109 | ▶ | if (!defined($fh)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 25 | $fh = $self->_filehandle; | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | LINE: | ||||||||||||||||||||||||||||
| 273 | 109 | while(1) { | |||||||||||||||||||||||||||
| 274 | 499 | my $line; | |||||||||||||||||||||||||||
| 275 | 499 | ▶ | if (defined($linebuf->[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | 49 | $line = pop(@{$linebuf}); | |||||||||||||||||||||||||||
| 277 | 49 | $line_from_array=1; | |||||||||||||||||||||||||||
| 278 | } else { | ||||||||||||||||||||||||||||
| 279 | 450 | ▶ | defined($line = readline $fh) or last LINE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 424 | $n++; | |||||||||||||||||||||||||||
| 281 | 424 | chomp $line; | |||||||||||||||||||||||||||
| 282 | 424 | $line_from_array=0; | |||||||||||||||||||||||||||
| 283 | # From here we need to preprocess the line to see if it can be broken | ||||||||||||||||||||||||||||
| 284 | # into multiple lines. Something like group { option test; } | ||||||||||||||||||||||||||||
| 285 | # lines with comments can't be handled by this so we do them first | ||||||||||||||||||||||||||||
| 286 | 424 | ▶ | if($line =~ /^\s*\#\s*(.*)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | 28 | push @comments, $1; | |||||||||||||||||||||||||||
| 288 | 28 | next LINE; | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | # lines with statements and comment, reprocess the statement and add the comment to comments array | ||||||||||||||||||||||||||||
| 291 | 396 | ▶ | if($line =~ /^(.*)\s*\#\s*(.*)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 1 | push @comments, $2; | |||||||||||||||||||||||||||
| 293 | 1 | push @{$linebuf}, $1; | |||||||||||||||||||||||||||
| 294 | 1 | next LINE; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | # after semicolon or braces if there isn't a semicolon or return insert a newline | ||||||||||||||||||||||||||||
| 298 | 395 | ▶ | if ($line =~ s/([;\{\}])([^;\n\r])/$1\n$2/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | 14 | push(@{$linebuf}, reverse split(/\n/, $line)); | |||||||||||||||||||||||||||
| 300 | 14 | next LINE; | |||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 430 | ▶ | if ($line =~ /^(?:\s*|\s*\{\s*)$/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 306 | 54 | next LINE; | |||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | elsif($line =~ /^\s*\}\s*$/) { | ||||||||||||||||||||||||||||
| 309 | 83 | ▶ | next LINE if($self->root == $self); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | 83 | last LINE; | |||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | # this is how we handle incomplete lines | ||||||||||||||||||||||||||||
| 314 | # we need a space for lines like 'option\ndomain-name-servers' | ||||||||||||||||||||||||||||
| 315 | 293 | ▶ | if ($lines) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | 19 | $lines .= ' '.$line; | |||||||||||||||||||||||||||
| 317 | } else { | ||||||||||||||||||||||||||||
| 318 | 274 | $lines = $line; | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | CHILD: | ||||||||||||||||||||||||||||
| 322 | 293 | for my $child ($self->children) { | |||||||||||||||||||||||||||
| 323 | 27 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 324 | 2001 | my $regex = ${"$child".'::regex'}; | |||||||||||||||||||||||||||
| 325 | 2001 | ▶ | my @c = $lines =~ $regex or next CHILD; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | 274 | my $add = 'add_' .lc +($child =~ /::(\w+)$/)[0]; | |||||||||||||||||||||||||||
| 327 | 274 | my $method = $child->can('captured_to_args'); | |||||||||||||||||||||||||||
| 328 | 274 | my $args = $method->(@c); | |||||||||||||||||||||||||||
| 329 | 274 | my $obj; | |||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 274 | $args->{'comments'} = [@comments]; | |||||||||||||||||||||||||||
| 332 | 274 | $args->{'parse'} = 1; | |||||||||||||||||||||||||||
| 333 | 274 | @comments = (); | |||||||||||||||||||||||||||
| 334 | 274 | undef $lines; | |||||||||||||||||||||||||||
| 335 | 274 | $obj = $self->$add($args); | |||||||||||||||||||||||||||
| 336 | 274 | ▶ | $n += $obj->_parse_slurp($fh, $linebuf) if ($obj->can('slurp')); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | # the recursive statement is used for Include.pm | ||||||||||||||||||||||||||||
| 339 | 274 | ▶ | $n += $obj->parse('recursive', $fh, $linebuf) if(@_ = $obj->children); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 274 | next LINE; | |||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | # if we get here that means our parse failed. If the incoming line | ||||||||||||||||||||||||||||
| 345 | # doesn't have a semicolon then we can guess it's a partial line and | ||||||||||||||||||||||||||||
| 346 | # append the next line to it. | ||||||||||||||||||||||||||||
| 347 | # we could do this with Slurp but then everything would need to | ||||||||||||||||||||||||||||
| 348 | # support slurp and odd semicolon handling. If we figure out a way to | ||||||||||||||||||||||||||||
| 349 | # merge the lines then the normal parser should be able to cover it. | ||||||||||||||||||||||||||||
| 350 | 19 | ▶ | if ($lines !~ /;/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 351 | 19 | next LINE; | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 0 | ▶ | if(warnings::enabled('net_isc_dhcpd_config_parse')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 355 | 0 | warn sprintf qq[Could not parse "%s" at %s line %s\n], | |||||||||||||||||||||||||||
| 356 | $line, | ||||||||||||||||||||||||||||
| 357 | $self->root->file, | ||||||||||||||||||||||||||||
| 358 | $fh->input_line_number | ||||||||||||||||||||||||||||
| 359 | ; | ||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 109 | ▶ | return $n ? $n : '0e0'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | =head2 _parse_slurp | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | This is a simplified parser for the slurp method. It's only used when slurp | ||||||||||||||||||||||||||||
| 369 | is available in a child method. | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | =cut | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | 7 | ▶ | sub _parse_slurp { | ||||||||||||||||||||||||||
|
Subroutine
_parse_slurp: called CC=7 SCAR=21.8
| |||||||||||||||||||||||||||||
| 375 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 376 | 7 | my $fh = shift; | |||||||||||||||||||||||||||
| 377 | 7 | my $linebuf = shift; | |||||||||||||||||||||||||||
| 378 | 7 | my($n, @comments); | |||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | LINE: | ||||||||||||||||||||||||||||
| 381 | 7 | while(1) { | |||||||||||||||||||||||||||
| 382 | 35 | my $line; | |||||||||||||||||||||||||||
| 383 | 35 | ▶ | if (defined($linebuf->[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | 0 | $line = pop(@{$linebuf}); | |||||||||||||||||||||||||||
| 385 | } else { | ||||||||||||||||||||||||||||
| 386 | 35 | ▶ | defined($line = readline $fh) or last LINE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 387 | 35 | $n++; | |||||||||||||||||||||||||||
| 388 | 35 | chomp $line; | |||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | 35 | ▶ | if($self->can('slurp')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 393 | 35 | my $action = $self->slurp($line); # next or last | |||||||||||||||||||||||||||
| 394 | 35 | ▶ | if($action eq 'next') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 395 | 28 | next LINE; | |||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||
| 397 | elsif($action eq 'last') { | ||||||||||||||||||||||||||||
| 398 | 7 | last LINE; | |||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | elsif($action eq 'backtrack') { | ||||||||||||||||||||||||||||
| 401 | 0 | push(@{$linebuf}, $line); | |||||||||||||||||||||||||||
| 402 | 0 | last LINE; | |||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | 7 | return $n; | |||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | =head2 captured_to_args | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | $hash_ref = $self->captured_to_args(@list); | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | Called when a L</regex> matches, with a list of captured strings. | ||||||||||||||||||||||||||||
| 414 | This method then returns a hash-ref passed on to the constructor when | ||||||||||||||||||||||||||||
| 415 | a new node in the object graph is constructed. | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | THIS IS A STATIC METHOD. SELF is not used. | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | =cut | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | 11 | ▶ | sub captured_to_args { | ||||||||||||||||||||||||||
|
Subroutine
captured_to_args: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 422 | 11 | return {}; | |||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | =head2 create_children | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | This method takes a list of classes, and creates builder method for | ||||||||||||||||||||||||||||
| 428 | the L</children> attribute, an attribute and helper methods. See | ||||||||||||||||||||||||||||
| 429 | L</children> for more details. | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | =cut | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | 297 | ▶ | sub create_children { | ||||||||||||||||||||||||||
|
Subroutine
create_children: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||
| 434 | 297 | my $self = shift; | |||||||||||||||||||||||||||
| 435 | 297 | my $meta = $self->meta; | |||||||||||||||||||||||||||
| 436 | 297 | my @children = @_; | |||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | 297 | for my $class (@children) { | |||||||||||||||||||||||||||
| 439 | 2862 | my $name = lc (($class =~ /::(\w+)$/)[0]); | |||||||||||||||||||||||||||
| 440 | 2862 | my $attr = $name .'s'; | |||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | # hack so the child method for class is classes instead of classs | ||||||||||||||||||||||||||||
| 443 | 2862 | ▶ | $attr = $name . 'es' if ($name =~ /s$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | 2862 | Class::Load::load_class($class); | |||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | 2862 | ▶ | unless($meta->find_method_by_name($attr)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 449 | 0 | ▶ | $meta->add_method("add_${name}" => sub { shift->_add_child($class, @_) }); | ||||||||||||||||||||||||||
|
Subroutine
add_optioncode: not called CC=1 SCAR=0.4
Subroutine
add_conditional: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 450 | 0 | ▶ | $meta->add_method("find_${attr}" => sub { shift->_find_children($class, @_) }); | ||||||||||||||||||||||||||
|
Subroutine
find_options: not called CC=1 SCAR=0.4
Subroutine
find_keyvalues: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 451 | 0 | ▶ | $meta->add_method("remove_${attr}" => sub { shift->_remove_children($class, @_) }); | ||||||||||||||||||||||||||
|
Subroutine
remove_options: not called CC=1 SCAR=0.4
Subroutine
remove_range6s: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 452 | 5 | ▶ | $meta->add_method($attr => sub { | ||||||||||||||||||||||||||
|
Subroutine
blocks: called CC=2 SCAR=7.1
Subroutine
zones: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 453 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 454 | 5 | ▶ | return $self->_set_children($class, @_) if(@_); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 455 | 5 | return $self->_get_children_by_class($class); | |||||||||||||||||||||||||||
| 456 | 2862 | }); | |||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | 297 | return \@children; | |||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | 0 | ▶ | sub _set_children { | ||||||||||||||||||||||||||
|
Subroutine
_set_children: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 464 | 0 | my($self, $attr, $class, $children) = @_; | |||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | 0 | for my $child (@$children) { | |||||||||||||||||||||||||||
| 467 | 0 | ▶ | $child = $class->new(parent => $self, root => $self->root, %$child) if(ref $child eq 'HASH'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | 0 | @{ $self->_children } = @$children; | |||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | 163 | ▶ | sub _get_children_by_class { | ||||||||||||||||||||||||||
|
Subroutine
_get_children_by_class: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 474 | 163 | my($self, $class) = @_; | |||||||||||||||||||||||||||
| 475 | 163 | my @children = grep { $class eq ref $_ } @{ $self->_children }; | |||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | 163 | ▶ | return wantarray ? @children : \@children; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 289 | ▶ | sub _add_child { | ||||||||||||||||||||||||||
|
Subroutine
_add_child: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 481 | 289 | my $self = shift; | |||||||||||||||||||||||||||
| 482 | 289 | my $class = shift; | |||||||||||||||||||||||||||
| 483 | 289 | ▶ | my $child = @_ == 1 ? $_[0] : {@_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 484 | 289 | my $children = $self->_children; | |||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | 289 | ▶ | if(ref $child eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 487 | 289 | $child = $class->new(parent => $self, root => $self->root, %$child); | |||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | 289 | push @$children, $child; | |||||||||||||||||||||||||||
| 491 | 289 | return $child; | |||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | 3 | ▶ | sub _find_children { | ||||||||||||||||||||||||||
|
Subroutine
_find_children: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 495 | 3 | my($self, $class, $query) = @_; | |||||||||||||||||||||||||||
| 496 | 3 | my @children; | |||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | 3 | ▶ | if(ref $query ne 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 499 | 0 | return; | |||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | CHILD: | ||||||||||||||||||||||||||||
| 503 | 3 | for my $child (@{ $self->_children }) { | |||||||||||||||||||||||||||
| 504 | 41 | ▶ | if($class ne ref $child) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 505 | 39 | next CHILD; | |||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||
| 507 | 2 | for my $key (keys %$query) { | |||||||||||||||||||||||||||
| 508 | 2 | ▶ | next CHILD unless($child->$key eq $query->{$key}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||
| 510 | 1 | push @children, $child; | |||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | 3 | return @children; | |||||||||||||||||||||||||||
| 514 | } | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | 10 | ▶ | sub _remove_children { | ||||||||||||||||||||||||||
|
Subroutine
_remove_children: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 517 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 518 | 10 | my $class = shift; | |||||||||||||||||||||||||||
| 519 | 10 | ▶ | my $query = shift or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 520 | 10 | my $children = $self->_children; | |||||||||||||||||||||||||||
| 521 | 10 | my $i = 0; | |||||||||||||||||||||||||||
| 522 | 10 | my @removed; | |||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||
| 524 | CHILD: | ||||||||||||||||||||||||||||
| 525 | 10 | ▶ | while($i < @$children) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 526 | 73 | ▶ | if($class ne ref $children->[$i]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 527 | 58 | next CHILD; | |||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||
| 529 | 15 | for my $key (keys %$query) { | |||||||||||||||||||||||||||
| 530 | 13 | ▶ | next CHILD unless($children->[$i]->$key eq $query->{$key}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||
| 532 | 11 | push @removed, splice @$children, $i, 1; | |||||||||||||||||||||||||||
| 533 | 11 | $i--; | |||||||||||||||||||||||||||
| 534 | } continue { | ||||||||||||||||||||||||||||
| 535 | 73 | $i++; | |||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | 10 | return @removed; | |||||||||||||||||||||||||||
| 539 | } | ||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | =head2 find_all_children | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | Loops through all child nodes with recursion looking for nodes of "class" | ||||||||||||||||||||||||||||
| 545 | type. Returns an array of those nodes. You can use the full classname or | ||||||||||||||||||||||||||||
| 546 | just the end part. For subclasses like Host::FixedAddress you would need to | ||||||||||||||||||||||||||||
| 547 | use the whole name. | ||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||
| 549 | my @subnet = $config->find_all_children('subnet'); | ||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||
| 551 | =cut | ||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | 7 | ▶ | sub find_all_children { | ||||||||||||||||||||||||||
|
Subroutine
find_all_children: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 554 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 555 | 7 | my $class = shift; | |||||||||||||||||||||||||||
| 556 | 7 | my @children; | |||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | 7 | ▶ | if ($class !~ /::/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 559 | # strip plural if they put it. | ||||||||||||||||||||||||||||
| 560 | 1 | $class =~ s/s\z//; | |||||||||||||||||||||||||||
| 561 | 1 | $class =~ s/(class|address)e/$1/; | |||||||||||||||||||||||||||
| 562 | 1 | $class = 'Net::ISC::DHCPd::Config::' . ucfirst(lc($class)); | |||||||||||||||||||||||||||
| 563 | } | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | 7 | for my $child (@{ $self->_children }) { | |||||||||||||||||||||||||||
| 566 | 6 | ▶ | if (ref($child) eq $class) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 567 | 3 | push(@children, $child); | |||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | 6 | ▶ | if ($child->_children) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 571 | 6 | push(@children, $child->find_all_children($class)); | |||||||||||||||||||||||||||
| 572 | } | ||||||||||||||||||||||||||||
| 573 | } | ||||||||||||||||||||||||||||
| 574 | 7 | return @children; | |||||||||||||||||||||||||||
| 575 | } | ||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | =head2 generate_config_from_children | ||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||
| 579 | Loops all child nodes in reverse order and calls L</generate> on each | ||||||||||||||||||||||||||||
| 580 | of them. Each L</generate> method must return a list of strings which | ||||||||||||||||||||||||||||
| 581 | will be indented correctly and concatenated with newline inside this | ||||||||||||||||||||||||||||
| 582 | method, before returned as one string. | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | =cut | ||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||
| 586 | 28 | ▶ | sub generate_config_from_children { | ||||||||||||||||||||||||||
|
Subroutine
generate_config_from_children: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 587 | 28 | return join "\n", shift->_generate_config_from_children; | |||||||||||||||||||||||||||
| 588 | } | ||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||
| 590 | 53 | ▶ | sub _generate_config_from_children { | ||||||||||||||||||||||||||
|
Subroutine
_generate_config_from_children: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 591 | 53 | my $self = shift; | |||||||||||||||||||||||||||
| 592 | 53 | my $indent = ''; | |||||||||||||||||||||||||||
| 593 | 53 | my @text; | |||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | 53 | ▶ | if($self->parent and !$self->can('generate_with_include')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->parent and not $self->can("generate_with_include")
MC/DC100%2/2
$self->parent and not $self->can("generate_with_include")
$self->parent
not $self->can("generate_with_include")
Truth table100%3/3
A and not(B)
A= $self->parentB= not $self->can("generate_with_include")
| |||||||||||||||||||||||||||||
| 596 | 34 | $indent = ' ' x 4; | |||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||
| 599 | 53 | for my $child (@{ $self->_children }) { | |||||||||||||||||||||||||||
| 600 | 121 | push @text, map { "$indent# $_" } $child->comments; | |||||||||||||||||||||||||||
| 601 | 121 | push @text, map { "$indent$_" } $child->generate; | |||||||||||||||||||||||||||
| 602 | } | ||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | 53 | return @text; | |||||||||||||||||||||||||||
| 605 | } | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | =head2 generate | ||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | A C<generate()> must be defined in the consuming class. This method | ||||||||||||||||||||||||||||
| 610 | must return a list of lines (zero or more), which will be indented | ||||||||||||||||||||||||||||
| 611 | and concatenated inside L</generate_config_from_children>. | ||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | See L<Net::ISC::DHCPd>. | ||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||
| 619 | =cut | ||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||
| 621 | package # hack to register a new warnings category | ||||||||||||||||||||||||||||
| 622 | net_isc_dhcpd_config_parse; | ||||||||||||||||||||||||||||
| 623 | 27 | ▶ | use warnings::register; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | 27 | 1; | |||||||||||||||||||||||||||