Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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 ( [id://11136787]=note: print w/replies, xml ) Need Help??


in reply to Re^4: 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 !

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/;


🦛

Replies are listed 'Best First'.
Re^6: Yet another config file editing programme : Tell me how to make it better !
by dazz (Beadle) on Sep 16, 2021 at 00:56 UTC
    Hi

    OK thanks that worked.


    Dazz

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-28 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found