Russ has asked for the wisdom of the Perl Monks concerning the following question:

Moved from Categorized Q&A by Q&AEditors.
Please do not vote for this node; the votes will affect the wrong user. Thank you.

I am using webmin on a system which I am setting up for a client, this system will be a remote mail server (dialing up to the internet to download mail). My problem is I am new to perl and wish to modify one script where the user of webmin adds / removes / or modifies a user on the system. I need to update a clear text file on my system with the users username:password so that another script I have written (shell script) can use fetchmail to get the users mailbox from my mailserver over the internet.

The two variables that webmin passes are $u{'user'} and $u{'pass'}

Can someone please help me?

  • Comment on Writing and modifying (change ordelete) a file

Replies are listed 'Best First'.
Re: Writing and modifying (change ordelete) a file
by Abigail (Deacon) on Jul 16, 2000 at 08:58 UTC
    Your question is a bit vague. All I can suggest is that you use open, print and flock. But how, and what else, I cannot determine from your description. It all depends on what's in that text file, what needs to be kept and what needs to be used before removed.

    -- Abigail

Re: Writing and modifying (change ordelete) a file
by splinky (Hermit) on Jul 16, 2000 at 09:06 UTC
    The short answer is that you have to rewrite the whole file. You can't modify stuff in place.

    The slightly longer answer is that you can modify files in place, but you can't alter the length of the file at all.

    Of course, Perl makes rewriting the whole file really easy, especially with the -i and -p command-line switches. Something like the following might do what you want:

    perl -i -pe 's/(user):.*?:/$1:passwd:/' filename

    *Woof*

Re: Writing and modifying (change ordelete) a file
by gaggio (Friar) on Jul 17, 2000 at 17:17 UTC
    It seems like you want to rewrite the configuration file of your mail program so that it takes the parameters of the person who is logged on on your system through the Perl script.

    Well, this is certainly an easy task to realize in Perl, sonce Perl is so good at text file operations...
    But it makes it a little funny that you expect a Monk to guess what to write for you just by giving $u{'user'} and $u{'pass'} for information... Everybody knows that Monks do like to communicate using telepathy, but for this problem I'm not sure somebody will help you.

    Monks are certainly willing to help you even if you want the solution "cooked for you" so that you just have to "cut-and-paste", but in this case you will have to at least give the name of the configuration file and show us what are the lines that you want to change inside your Perl script!
    Good luck!