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.


In reply to Re^5: Yet another config file editing programme : Tell me how to make it better ! by eyepopslikeamosquito
in thread Yet another config file editing programme : Tell me how to make it better ! by dazz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.