NAME
Net::ISC::DHCPd::Config::Role - Role with generic config methods and attributes
DESCRIPTION
This role contains common methods and attributes for each of the config classes in the Net::ISC::DHCPd::Config namespace.
WARNINGS
This module will warn when a line in the input config could not be parsed. This can be turned off by adding the line below before calling "parse".
no warnings 'net_isc_dhcpd_config_parse';
ATTRIBUTES
parent
The parent node in the config tree. This must be an object which does this role.
root
The root node in the config tree.
depth
Integer value that counts how far this node is from the root node.
children
Holds a list of possible child objects as objects. This list is used when "parse" or "generate_config_from_children" is called. The child list has a default value set from "create_children" in each of the config modules. This is a static list, which reflects the actual documentation from dhcpd.conf(5)
. Example:
package Net::ISC::DHCPd::Config::Foo;
__PACKAGE__->create_children("Net::ISC::DHCPd::Config::Host");
package main;
$config = Net::ISC::DHCPd::Config::Foo->new;
$config->add_host({ ... });
@host_objects = $config->find_hosts({ ... });
$config->remove_host({ ... });
@host_objects = $config->hosts;
The "create_children" method will autogenerate three methods and an attribute. The name of the attribute and methods will be the last part of the config class, with "s" at the end in some cases.
- foos
-
foos
is the name the attribute as well as the accessor. The accessor will auto-deref the array-ref to a list if called in list context. (yes: be aware of this!). - add_foo
-
Instead of pushing values directly to the
foos
list, anadd_foo
method is available. It can take either a hash, hash-ref or an object to add/construct a new child. - find_foos
-
This method will return zero or more objects as a list. It takes a hash-ref which will be matched against the object attributes of the children.
- remove_foo
-
This method will remove zero or more children from the
foos
attribute. The method takes a hash-ref which will be used to match against the child list. It returns the number of child nodes actually matched and removed.
comments
@str = $self->comments;
Will return all the comments before this element appeared in the config file. The comments will not contain leading hash symbol spaces, nor trailing newline.
CHILD METHODS
regex
Regex used to scan a line of config text, which then spawns an a new node to the config tree. This is used inside l</parse>.
THIS IS A STATIC METHOD. SELF is not used.
METHODS
BUILD
Used to convert input arguments to child nodes.
filename_callback
Callback for changing file paths when include files are on different relative paths.
# here is an example:
my $cb = sub {
my $file = shift;
print "We're in callback and file is $file\n";
if ($file =~ /catphotos/) {
return "/dog.conf";
}
};
parse
Will read a line of the time from the current config file. For each line, this method will loop though each object in "children" and try to match the line against a given child and create a new node in the object graph if it match the "regex". This method is called recursively for each child when possible.
_parse_slurp
This is a simplified parser for the slurp method. It's only used when slurp is available in a child method.
captured_to_args
$hash_ref = $self->captured_to_args(@list);
Called when a "regex" matches, with a list of captured strings. This method then returns a hash-ref passed on to the constructor when a new node in the object graph is constructed.
THIS IS A STATIC METHOD. SELF is not used.
create_children
This method takes a list of classes, and creates builder method for the "children" attribute, an attribute and helper methods. See "children" for more details.
find_all_children
Loops through all child nodes with recursion looking for nodes of "class" type. Returns an array of those nodes. You can use the full classname or just the end part. For subclasses like Host::FixedAddress you would need to use the whole name.
my @subnet = $config->find_all_children('subnet');
generate_config_from_children
Loops all child nodes in reverse order and calls "generate" on each of them. Each "generate" method must return a list of strings which will be indented correctly and concatenated with newline inside this method, before returned as one string.
generate
A generate()
must be defined in the consuming class. This method must return a list of lines (zero or more), which will be indented and concatenated inside "generate_config_from_children".
COPYRIGHT & LICENSE
AUTHOR
See Net::ISC::DHCPd.