Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^4: Yet another config file editing programme : Tell me how to make it better !

by dazz (Beadle)
on Sep 15, 2021 at 09:52 UTC ( [id://11136785]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Yet another config file editing programme : Tell me how to make it better !
in thread Yet another config file editing programme : Tell me how to make it better !

Hi
I have working at writing better perl based on the advice received here but I am stuck.
I slurp in the configuration file I want to update OK.
I load it to ARGV to run it through a while( <> ) loop OK.
I search and edit the file line by line OK.
When I try and spew it, the file is blank.

The code sample below is not complete, and probably not working.
I have stripped out everything that I don't think is relevant to the slurp/process/spew problem.
I am wondering if the explicit use of $line is the problem??
I have tried removing all references to $line but no change. The output file is still blank.
#!/usr/bin/perl use strict; use warnings; use Path::Tiny; ###### Output dhcp configuration file # my $dhcpcdfile = '/etc/dhcpcd.conf'; my $dhcpcdfile = 'd.conf'; # TEST ####################################### Load the output config file path($dhcpcdfile)->slurp; ### ####################################### { # block to limit scope @ARGV = $dhcpcdfile; while( my $line = <> ) { ### Is the explicit use of $line the pro +blem ??? my @ip_fields = split( /=/,$line); # look for profile with matching interface name if ( $line =~ /^\s*profile\s+static_$ip_params{interface}\b.*\n/m + ) { # format matches 'profile static_eth0' } elsif( $foundinterface and $line =~ /^.*static\s*ip_address=/ ) { $line = "static ip_address=$ip_params{'ip_address'}\n"; } elsif( $foundinterface ){ last; # No need to continue looking through the file } } ########################################### path('spew.cfg')->spew(@ARGV); ### This saves a blank file ########################################### }


Dazz
  • Comment on Re^4: Yet another config file editing programme : Tell me how to make it better !
  • Download Code

Replies are listed 'Best First'.
Re^5: Yet another config file editing programme : Tell me how to make it better !
by hippo (Bishop) on Sep 15, 2021 at 12:38 UTC
    I load it to ARGV to run it through a while( <> ) loop

    This is the cause of your problems. Don't do this. If you want to operate on the individual lines use slurp lines and a for loop instead.

    #!/usr/bin/env perl use strict; use warnings; use Path::Tiny; ###### Output dhcp configuration file my $dhcpcdfile = 'd.conf'; # TEST my @rows = path($dhcpcdfile)->lines; my %ip_params = (); # YOU MUST POPULATE THIS FIRST my $foundinterface; # YOU MUST ASSIGN A VALUE TO THIS for my $line (@rows) { ### YOUR CODE HERE my @ip_fields = split( /=/,$line); # look for profile with matching interface name if ( $line =~ /^\s*profile\s+static_$ip_params{interface}\b.*\n/m + ) { # format matches 'profile static_eth0' } elsif( $foundinterface and $line =~ /^.*static\s*ip_address=/ ) { $line = "static ip_address=$ip_params{'ip_address'}\n"; } elsif( $foundinterface ){ last; # No need to continue looking through the file } ### YOUR CODE ENDS HERE } path('spew.cfg')->spew(@rows);

    Note, untested because what was supplied doesn't compile and isn't an SSCCE.

    Update: s/slurp/lines/;


    🦛

      Hi

      OK thanks that worked.


      Dazz
Re^5: Yet another config file editing programme : Tell me how to make it better !
by eyepopslikeamosquito (Archbishop) on Sep 16, 2021 at 00:58 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11136785]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-03-29 15:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found