从窗体中检索值并写入文件 [英] Retrieve value from a form and write to a file

查看:151
本文介绍了从窗体中检索值并写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为


index.cgi

以及一个名为



$ b的配置文件
$ b

用户从下拉菜单中选择并按下提交后,该值将被读取并写入配置文件。以下是我的表单中的代码片段:

 < tr>< td> 
< form method =POSTaction =index.cgi>
< table>
< tr>< th colspan =2>视频< / th>< / tr>
< tr>< td align =right>< b>视频来源:< / b>< / td>< td align =left>
< select name =channel1size =1>

以下是来自 index.cgi :

  my $ file =video.start; 

open(my $ in,+<,$ file);
open(my $ out,+<,$ file);

my $ a = CGI-> new();
$ video = $ a-> param('channel1'); $(< $ in>)
{
print $ out $ _;


最后如果$。== 3;


while(my $ data =< $ in>)
{
#logic

print $ out $ data;
最后如果$。== 5;

$ / code>

最后,这是我的配置文件的样子:

 #仅在[udp://#]段更改
#此配置文件适用于视频

ffmpeg -f video4linux2 -i / dev / video0 -vcodec libx264 -preset ultrafast -tune zerolatency -s qvga -r 30 -qscale 5 -an -flags low_delay -bsf:v h264_mp4toannexb -maxrate 750k -bufsize 3000k -rtbufsize 300k -f h264 udp://#

如果您从配置表单中可以看到用户提交值后,根据用户提交的值, / dev / video0 将在配置文件中更改。但是我怎么能在我的逻辑中实现这一点?

解决方案

最大的窍门是你需要 flock 编辑文件之前,比赛条件。

  use strict; 
使用警告;
使用autodie;

使用Fcntl qw(:flock);

打开我的$ fh,'< +','foobar.txt';
flock($ fh,LOCK_EX)或死无法锁定邮箱 - $!\\\
;

之后,请阅读 如何在文件中更改,删除或插入一行,或者追加到文件的开头? 。我个人会选择 Tie :: File ,因为我认为这可能是编辑文件的最简单方法。


i have a form named

index.cgi

and a configuration file named

video.start

After the user select from the drop down menu and press submit, the value will be read and write into the configuration file. Here is a snippet from my form:

<tr><td>
<form method="POST" action="index.cgi">
<table>
<tr><th colspan="2">Video</th></tr>
<tr><td align="right"><b>Video Source :</b></td><td align="left">
<select name="channel1" size="1">
<option value="/dev/video0">/dev/video0</option>
<option value="/dev/video1">/dev/video1</option>
<option value="/dev/video2">/dev/video2</option>
<option value="/dev/video3">/dev/video3</option>
</select>
</td></tr>
</table>
</td></tr>
<tr>
<td colspan="2">
<div id="button">
<input type="submit" value="Submit"><input type="reset" value="Clear">
</div>
</form>
</td>
</tr>

and here is the code from index.cgi:

my $file = "video.start";

open (my $in, "+<", $file);
open (my $out, "+<", $file);

my $a = CGI->new();
$video = $a->param('channel1');

while(<$in>)
{
    print $out $_;
    last if $.==3;
}

while(my $data = <$in>)   
{
    #logic

    print $out $data;
    last if $.==5;
}

Finally, here is what my configuration file looks like:

#Only change at [udp://#] segment 
#This config file is for video

ffmpeg -f video4linux2 -i /dev/video0 -vcodec libx264 -preset ultrafast -tune zerolatency -s qvga -r 30 -qscale 5 -an -flags low_delay -bsf:v h264_mp4toannexb -maxrate 750k -bufsize 3000k -rtbufsize 300k -f h264 udp://# 

If you can see from the configuration form, after the user submitted the value, the /dev/video0 will change in the configuration file according to the value submitted by the user. But how can i achieve this in my logic?

解决方案

The biggest trick to this is that you need to flock your files before editing them so there isn't a race condition.

use strict;
use warnings;
use autodie;

use Fcntl qw(:flock);

open my $fh, '<+', 'foobar.txt';
flock($fh, LOCK_EX) or die "Cannot lock mailbox - $!\n";

After that, just read How do I change, delete, or insert a line in a file, or append to the beginning of a file?. I personally would go with Tie::File as I think that's probably the easiest way to edit a file in this context.

这篇关于从窗体中检索值并写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆