Perl从表单更新.conf文件中的值

时间:2014-02-25 01:53:14

标签: perl parsing cgi updating

我有一个admin.conf文件,文件看起来像这样。例如,

[online_offline_status]
online_offline_status.online_offline_state = ONLINE

我有另一个名为index.cgi的文件,它可以作为一个表单。例如,

#!/usr/bin/perl
#index.cgi

require 'foobar-lib.pl';


ui_print_header(undef, $module_info{'desc'}, "", undef, 1, 1);


ui_print_footer("/", $text{'index'});

print "Content-type:text/html\n\n";
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 Status :</span></div>
</td>
<td>
<div id="form">
<form method="POST" action="index9.cgi">
<input type="radio" name="state" value="online">Online
<br/>
<input type="radio" name="state" value="offline">Offline
</div>
</td>
</tr>
<tr>
<td colspan='2'>
<div id="button">
<input type="submit" value="Submit"><input type="reset" value="Clear">
</div>
</td>
</tr>
</form>
</table>
</div>
</div>

</body>
</html>

~;

当用户选择其中一个单选按钮并提交时。该值将发送到index9.cgi。它将处理此文件中的表单(index9.cgi)

#!/usr/bin/perl
#index.cgi

require 'foobar-lib.pl';


ui_print_header(undef, $module_info{'desc'}, "", undef, 1, 1);


ui_print_footer("/", $text{'index'});

local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "POST")
{
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}else {
$buffer = $ENV{'QUERY_STRING'};
}
# Split information into name/value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
$status = $FORM{state};

print "Content-type:text/html\n\n";
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>Offline / Online state has been changed to : <b>$status</b><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>

~;

当用户选择&#34;离线&#34;在index.cgi中发送并处理index9.cgi中的值(&#34; offline&#34;),.conf文件中的值也会从ONLINE更新为OFFLINE?

1 个答案:

答案 0 :(得分:0)

是的,这是可能的。

最简单的方法是通过从Perl调用的sed脚本来更改文件。

更好的方法是在perl中打开文件,读入一个字符串,用在线替换脱机并再次打印出来,破坏旧文件。

ps:你听说过CGI.pm吗?它可以减轻你的工作。

Ps Ps:最好使用一些现代的网络框架,比如mojolicious,Dancer。

使用CGI.pm

(确保admin.conf可由webserver帐户写入)

在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>

~;