Perl updating a value in .conf file from a form

后端 未结 1 1912
伪装坚强ぢ
伪装坚强ぢ 2021-01-07 14:08

I have a admin.conf file and the file looks like this. For example,

[online_offline_status]
online_offline_status.online_offline_state = ONLINE
相关标签:
1条回答
  • 2021-01-07 14:40

    Yes, it is possible.

    Dirtiest way is to change the file on place via sed script, called from Perl..

    Better way is to open the file in perl, read into a string, replace offline with online and print it out again, clobbering the old file.

    ps: Did you hear about CGI.pm? It could ease your work.

    Ps Ps: It would be better to use some modern web framework, like mojolicious, Dancer.

    with CGI.pm

    (be sure that the admin.conf is writeable by the webserver account)

    In index9.cgi

    use strict;
    use warnings;
    use CGI;
    use Config::Tiny;
    use Data::Dumper;
    use CGI::Carp qw(fatalsToBrowser);
    
    my $q = CGI->new;
    print $q->header();
    my $state = $q->param('state');
    my $file = "/full/path/to/admin.conf";
    my $Config = Config::Tiny->read( $file );
    my $status_in_file = $Config->{online_offline_status}->{online_offline_status.online_offline_state};
    
    my $msg = "No changes being made";
    $msg = "Status changed from $status_in_file to $status" if $status_in_file ne $status;
    $Config->{online_offline_status}->{online_offline_status.online_offline_state} = $status;
    $Config->write( $file );
    
    
    
    print qq~
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="cstyle17.css">
    </head>
    <body>
    <div id="content">
    <div id="bar">
    <span><p>Controller Settings</p></span>
    </div>
    <div id="tab-container">
    <ul>
    <li class="active"><span><a href="index.cgi">Offline / Online Status</a></span></li>
    <li><span><a href="#">Data Online</a></span></li>
    </ul>
    </div>
    <div id="main-container">
    <table width='100%' height='60%'>
    <tr>
    <td width="50%" align="left">
    <div id="title"><span>Offline / Online State :</span></div>
    </td>
    <td>
    <div id="form">
    <form method="link" action="index.cgi">
    <p>$msg</p>
    </div>
    </td>
    </tr>
    <tr>
    <td colspan='2'>
    <div id="button">
    <input type="submit" value="Back">
    </div>
    </td>
    </tr>
    </form>
    </table>
    </div>
    </div>
    
    </body>
    </html>
    
    ~;
    
    0 讨论(0)
提交回复
热议问题