Perl updating a value in .conf file from a form

后端 未结 1 1924
伪装坚强ぢ
伪装坚强ぢ 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~
    
    
    
    
    
    

    Controller Settings

    Offline / Online State :

    $msg

    ~;

    0 讨论(0)
提交回复
热议问题