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

Hello, I am trying to write a small CGI program to edit one line of a very long aliases file. The one line consists of the name: pageoncall and a pager number in the form of: email1234567890@pager.com/email. The pager number listed within that aliases could be in several other locations within the file. I have written most everything with the exception of this. I cannot figure out how to edit just that one line within the file. Here is the code that i have so far:
#!/usr/bin/perl use CGI qw(:standard); open(FILE, "/test/aliases") or die "Couldn't open $0"; print header('text/html'), start_html('Answer the call!'), h1('Select your pager number and click submit:'), start_form, popup_menu(-name=>'pager', -values=>['','1234567890@messaging.sprintpc +s.com', '1234567890@archwireless.net', '0987654321@archwireless.net']),p +, submit, end_form; if (param() ) { my $pager = param('pager'); if($pager == '') { print "You selected an invalid number, please +try again."; } while(<FILE>) { if(/^pageoncall:/) { print FILE "pageoncall: $pager"; } } }
Any help would be gre

Replies are listed 'Best First'.
Re: Edit sendmain aliases file.
by aquarium (Curate) on Jan 07, 2008 at 03:20 UTC
    unless you can bend the implementation to use one of the "tie" methods to handle the file read/write for you, you'll otherwise need to regenerate the whole new file each time you want to modify it...with cocurrency locking to be handled yourself also.
    the hardest line to type correctly is: stty erase ^H

      The synopsis in Tie::File practically gives you the answer you seek.

Re: Edit sendmain aliases file.
by proceng (Scribe) on Jan 08, 2008 at 12:59 UTC
    Have you thought about using an include file? One source I found was http://www.docs.hp.com/en/B2355-90685/ch04s03.html. This would allow you to say:
    pageroncall: :include:/usr/local/etc/oncall
    in the aliases file and put:
    #my turn in the bucket
    1234567890@archwireless.net
    in the /usr/local/etc/oncall </code> file.
    Good Luck