perl/CGI editing file online

昨天在网上看见一个perl脚本,就是打开一个文件显示出来,然后用户编辑后再保存

我自己重写了下,就这样

这个是主页面

#!/usr/bin/perl
use warnings;
#use strict;
use CGI qw/:standard/;
use IO::File;
use CGI::Carp;
use MyConfig;

#页面标题
use constant TITILE => "保存文件";

use constant CSS_FILENAME => "editor_save.css";

#把错误从STDERR重定向到指定文件
BEGIN {
    use CGI::Carp qw(carpout);
    open(LOG, ">>".LOG_FILENAME)
        or die "Unable to append to mycgi-log: $!\n";
    carpout(*LOG);
}

sub save_file_content{
    my $filename=shift;
    my $content=shift;
    my $fh = new IO::File($filename,O_WRONLY);
    my $s="";
    if(defined $fh && defined $content) {
        $fh->write($content);
        $fh->close();
        undef  $fh;
        return 0;
    }else{
        print "打开".$filename."失败: $!";
        return -1;
    }
    return 0;
}


main:{
    my $q=new CGI;
    $q->charset("GB2312");    
    print $q->header
        ,$q->start_html(-title => TITILE,
                    -style => CSS_PATH .  CSS_FILENAME,
                    -lang => "zh-cn",
                    -encoding => "GB2312");

    if(!(defined $q->param)){
        warn "没有收到参数";
    }
    my $filename=$q->param("filename");
    my $file_content=$q->param("file_content");
    if(!(defined $file_content && defined $filename)){
        warn "缺少参数或者参数名不正确";
    }    
    if(save_file_content($filename,$file_content)!=0){
        print $q->h1("失败");
    }
    else{
        print $q->h1("成功");
    }
    print $q->end_html();
}

点提交后转到这个页面

#!/usr/bin/perl
use warnings;
#use strict;
use CGI qw/:standard/;
use IO::File;
use CGI::Carp;
use MyConfig;
#页面标题
use constant TITILE => "保存文件";

use constant CSS_FILENAME => "editor_save.css";

#把错误从STDERR重定向到指定文件
BEGIN {
    use CGI::Carp qw(carpout);
    open(LOG, ">>".LOG_FILENAME)
        or die "Unable to append to mycgi-log: $!\n";
    carpout(*LOG);
}

sub save_file_content{
    my $filename=shift;
    my $content=shift;
    my $fh = new IO::File($filename,O_WRONLY);
    my $s="";
    if(defined $fh && defined $content) {
        $fh->write($content);
        $fh->close();
        undef  $fh;
        return 0;
    }else{
        print "打开".$filename."失败: $!";
        return -1;
    }
    return 0;
}


main:{
    my $q=new CGI;
    $q->charset("GB2312");    
    print $q->header
        ,$q->start_html(-title => TITILE,
                    -style => CSS_PATH .  CSS_FILENAME,
                    -lang => "zh-cn",
                    -encoding => "GB2312");

    if(!(defined $q->param)){
        warn "没有收到参数";
    }
    my $filename=$q->param("filename");
    my $file_content=$q->param("file_content");
    if(!(defined $file_content && defined $filename)){
        warn "缺少参数或者参数名不正确";
    }    
    if(save_file_content($filename,$file_content)!=0){
        print $q->h1("失败");
    }
    else{
        print $q->h1("成功");
    }
    print $q->end_html();
}

我把公共的配置信息放在一个单独的文件中,做成perl模块

#!/usr/bin/perl
use warnings;
#use strict;
package MyConfig;
require Exporter;

our @ISA=qw(Exporter);
#our %EXPORT_TAGS=('all' => qw(CSS_PATH) );

#our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

our @EXPORT = qw(CSS_PATH CGI_PATH);

use constant CSS_PATH => "/snnn/css/";
use constant CGI_PATH => "/snnn/cgi-bin/";
use constant LOG_FILENAME => "../logs/cgi.log";

此博客中的热门博文

少写代码,多读别人写的代码

在windows下使用llvm+clang

tensorflow distributed runtime初窥