Please use <code> code goes here</code> tags, it improves readability a lot;)

Your not far away, lets take a fresh look and decompose the problem, for example like:

  1. reading of a config file
  2. reading of user input
  3. writing/updating of the config file

As a general comment put use strict; use warnings; in your scripts.

1. Let's start with the reading of the file first.

#!/usr/bin/perl use strict; use warnings; my %config; my $filename = "TestConfig.cfg"; open ( FILE,'<', $filename) or die "Cannot open file: $!"; while (<FILE>) { chomp; s/#.*//; # no comments s/^\s+//; # no leading white s/\s+$//; # no trailing white next unless length; # anything left? my ($var, $value) = split(/\s*=\s*/, $_, 2); $config{$var} = $value; } close FILE; while(my ($key, $value) = each(%config)) { print "key: $key, value: $value\n"; }

I used the following config file:

ServerIP=<10.11.200.189> FirewallIP=<10.11.200.189> WebServerListenPort=<5566> ConnectionStartPort=<9400> ConnectionEndPort=<9419> RecordServerIP=<10.11.200.189> MediaPort=<7080> ControlPort=<7081>

Make sure you understand what happens. Why is the 2 used in the split function?

2. Reading of user input

You are on the right way. There are more ways to do it, I would probably use a module from CPAN, but using STDIN should work fine.

3. Writing of the config file

You open the file for * reading * only:) You can work with a temporary file and later overwrite the original or do the update in-place.

HTH

Cheers

Harry


In reply to Re^3: Editing a Config file by dHarry
in thread Editing a Config file by amitonvoip

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.