dazz: you seem to be succumbing to a
Cargo cult style of programming in harness with Shotgun debugging.
tybalt89 is a Perl expert. You are a Perl beginner.
Instead of making minor tweaks to tybalt89's advanced complete program,
I suggest you start from scratch with a very small program of your own.
Test it. Run it. Read the Perl and CPAN module documentation.
Make sure you understand how every statement works.
Format it in a way that suits your brain (please please don't copy tybalt89's idiosyncratic formatting style ;-).
Liberally add print/dump statements to confirm that your program is working the way you think it should.
Build confidence.
Then gradually grow your program, step by step, until it solves your problem,
making sure that you always have a working program, every step of the way.
And making sure you understand in detail how every line works.
Though not core modules, I suggest you stick with
tybalt89's excellent choice of Path::Tiny and Data::Dump from CPAN (these two modules are also included with Strawberry Perl).
Read and understand their CPAN documentation.
To give a concrete example, your first version might be as simple as:
#!/usr/bin/env perl
use strict;
use warnings;
use Path::Tiny;
use Data::Dump;
###### Output dhcp configuration file
my $dhcpcdfile = 'd.conf';
print "at start of program---\n";
my @rows = path($dhcpcdfile)->lines;
dd( @rows );
print "before loop---\n";
for my $line (@rows) {
$line =~ s/^\s+//; # remove leading
$line =~ s/\s+$//; # and trailing whitespace
next unless length $line; # ignore empty lines
next if $line =~ /^#/; # ignore comment lines
my @fields = split /=/, $line, 2;
dd( @fields );
}
print "after loop---\n";
With the file d.conf containing:
#IP Configuration
#Fri Aug 27 16:07:40 NZST 2021
routers=192.130.1.1
interface=eth0
domain_name_servers=8.8.8. 8.8.1.1
ip_address=192.130.1.10/24
and running this program producing:
at start of program---
(
"#IP Configuration\n",
"#Fri Aug 27 16:07:40 NZST 2021\n",
"\n",
"routers=192.130.1.1\n",
"interface=eth0\n",
"domain_name_servers=8.8.8. 8.8.1.1\n",
"ip_address=192.130.1.10/24\n",
)
before loop---
("routers", "192.130.1.1")
("interface", "eth0")
("domain_name_servers", "8.8.8. 8.8.1.1")
("ip_address", "192.130.1.10/24")
after loop---
Finally, please study and follow Short, Self-Contained, Correct Example when you post asking for assistance.
Updated: minor improvements to wording.
|