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
###########################################
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.