| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::ISC::DHCPd::Config::Include; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | =head1 NAME | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | Net::ISC::DHCPd::Config::Include - Hold content of included file | 
| 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 |  |  |  |  |  |  | include "$file_attribute_value"; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | Example: | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | my $include = $config->includes->[0]; | 
| 19 |  |  |  |  |  |  | $include->parse; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | See L<Net::ISC::DHCPd::Config/SYNOPSIS>. | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | =cut | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 25 |  |  | 25 |  | 16577 | use Moose; | 
|  | 25 |  |  |  |  | 51 |  | 
|  | 25 |  |  |  |  | 201 |  | 
| 28 | 25 |  |  | 25 |  | 140739 | use Path::Class::File; | 
|  | 25 |  |  |  |  | 64 |  | 
|  | 25 |  |  |  |  | 632 |  | 
| 29 | 25 |  |  | 25 |  | 117 | use  Net::ISC::DHCPd::Config; | 
|  | 25 |  |  |  |  | 45 |  | 
|  | 25 |  |  |  |  | 11798 |  | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | with 'Net::ISC::DHCPd::Config::Root'; | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | =head2 children | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | See L<Net::ISC::DHCPd::Config::Role/children>. | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | =cut | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub children { | 
| 40 | 35 |  |  | 35 | 1 | 195 | return Net::ISC::DHCPd::Config::children(); | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | __PACKAGE__->create_children(__PACKAGE__->children()); | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | =head1 ATTRIBUTES | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | =head2 generate_with_include | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | This attribute holds a boolean value. L</generate> will result in | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | include "path/from/file/attribute"; | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | when false, and the included config if true. This attribute is false | 
| 54 |  |  |  |  |  |  | by default. | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | Example: | 
| 57 |  |  |  |  |  |  | $include->generate_with_include(1); | 
| 58 |  |  |  |  |  |  | $include->generate; | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | =cut | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | has generate_with_include => ( | 
| 63 |  |  |  |  |  |  | is => 'rw', | 
| 64 |  |  |  |  |  |  | isa => 'Bool', | 
| 65 |  |  |  |  |  |  | default => 0, | 
| 66 |  |  |  |  |  |  | ); | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | =head2 regex | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | See L<Net::ISC::DHCPd::Config::Role/regex>. | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | =cut | 
| 73 |  |  |  |  |  |  | our $regex = qr{^\s* include \s+ "([^"]+)" ;}x; | 
| 74 | 0 |  |  | 0 |  | 0 | sub _build_root { shift->parent } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | sub _build__filehandle { | 
| 77 | 5 |  |  | 5 |  | 9 | my $self = shift; | 
| 78 | 5 |  |  |  |  | 131 | my $file = $self->file; | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 5 | 100 |  |  |  | 119 | if ($self->root->filename_callback) { | 
| 81 | 1 |  |  |  |  | 2 | $file = Path::Class::File->new(&{$self->root->filename_callback}($file)); | 
|  | 1 |  |  |  |  | 23 |  | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 5 | 100 | 66 |  |  | 90 | if($file->is_relative and !-e $file) { | 
| 85 | 4 |  |  |  |  | 240 | $file = Path::Class::File->new($self->root->file->dir, $file); | 
| 86 | 4 |  |  |  |  | 461 | $self->file($file);  # needed so dir stays updated with recursive includes | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 5 |  |  |  |  | 95 | return $file->openr; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | =head1 METHODS | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | =head2 parse | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | This around method modifier will stop the parser when parsing | 
| 97 |  |  |  |  |  |  | recursively, which will require the user to manually parse the | 
| 98 |  |  |  |  |  |  | included files from the config. Reason for this is that the | 
| 99 |  |  |  |  |  |  | C<parse()> method returns the number of lines in a single file. | 
| 100 |  |  |  |  |  |  | and counting lines from included files will break this behaviour. | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | See also L<Net::ISC::DHCPd::Config::Role/parse> and | 
| 103 |  |  |  |  |  |  | L<Net::ISC::DHCPd::Config/SYNOPSIS>. | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | =cut | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | around parse => sub { | 
| 108 |  |  |  |  |  |  | my $next = shift; | 
| 109 |  |  |  |  |  |  | my $self = shift; | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | if($_[0] and $_[0] eq 'recursive') { | 
| 112 |  |  |  |  |  |  | return '0e0'; | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | return $self->$next(@_); | 
| 116 |  |  |  |  |  |  | }; | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | =head2 captured_to_args | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | See L<Net::ISC::DHCPd::Config::Role/captured_to_args>. | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =cut | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | sub captured_to_args { | 
| 125 | 5 |  |  | 5 | 1 | 15 | return { file => $_[0] }; | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | =head2 generate | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | This method can either result in C<include ...;> line or the whole | 
| 131 |  |  |  |  |  |  | config of the included file. See L</generate_with_include> for how | 
| 132 |  |  |  |  |  |  | to control the behaviour. | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | See also L<Net::ISC::DHCPd::Config::Role/generate>. | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | =cut | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | sub generate { | 
| 139 | 4 |  |  | 4 | 1 | 5 | my $self = shift; | 
| 140 |  |  |  |  |  |  |  | 
| 141 | 4 | 100 |  |  |  | 100 | if($self->generate_with_include) { | 
| 142 | 2 |  |  |  |  | 6 | my $text = $self->generate_config_from_children; | 
| 143 | 2 | 100 |  |  |  | 23 | return $text ? $text : "# forgot to parse " .$self->file ."?"; | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  | else { | 
| 147 | 2 |  |  |  |  | 49 | return qq(include ") .$self->file .qq(";); | 
| 148 |  |  |  |  |  |  | } | 
| 149 |  |  |  |  |  |  | } | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | =head1 COPYRIGHT & LICENSE | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | =head1 AUTHOR | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | See L<Net::ISC::DHCPd>. | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | =cut | 
| 158 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable; | 
| 159 |  |  |  |  |  |  | 1; |