Windows上的Perl无法打开文件

时间:2014-10-15 22:12:22

标签: windows perl web io

我编写了这个脚本来为运行脚本的用户创建一个基本网站。它可以在windows或unix系统中执行,但由于某种原因,文件将无法打开以进行写入。

#!/usr/bin/perl
use strict;
use warnings;
use CGI;

#createSite.pl

#Attributes
my $sys;
my $user;
my $site;
my $cgistuff = CGI->new;

#Subroutine for checking system.
sub checkSys {

    if ( $ARGV[0] eq "-w" ) {
        $sys = "\\";
    } elsif ( $ARGV[0] eq "-l" ) {
        $sys = "/";
    } else {
        print "System not defined please use -w or -l flag for Windows or Linux\n\n";
    }

    #Obtains user name based on user input.
    print "What would you like your user name to be: ";
    $user = <STDIN>;
    chomp($user);
    $user = lc($user);

    #Obtains site name based on user input.
    print "What would you like your site/domain to be called: ";
    $site = <STDIN>;
    chomp($site);
    $site = lc($site);

    #Debug for printing user name and site name
    print "Username: $user, Site/Domain: $site\n";

    #After checking what system the user is on create file structure.
    createFS();
}

#Subroutine for file structure creation.
sub createFS {
    my $rootDir;

    if ( $sys eq "\\" ) {
        $rootDir = 'mkdir ' . 'C:\\inetpub\\wwwroot\\~' . $user . $sys;
    } elsif ( $sys eq "/" ) {
        $rootDir = 'mkdir /var/www/html/~' . $user . $sys;
    }
    system($rootDir);
    picDir();
    databaseDir();
    mailDir();
    createPage();
}

#Subroutine to create use OBwebsite.
sub createPage {
    if ( $sys eq "\\" ) {
        system( "echo> C:\\Inetpub\\wwwroot\\~" . $user . "\\index.html" );
        open( WEBFILE, ">", "C:\\Inetpub\\wwwroot\\~" . $user . "\\index.html" )
            or die "index.html could not be opened.\n";
    } elsif ( $sys eq "/" ) {
        system( "touch /var/www/html/~" . $user . "/index.html" );
        open( WEBFILE, ">", "/var/www/html/~" . $user . "/index.html" )
            or die "index.html could not be opened.\n";
    }

    print WEBFILE $cgistuff->start_html(), $cgistuff->h1("Welcome $user!!!"), $cgistuff->end_html;

    close WEBFILE;
}

#Subroutine for creation of Pictures Directory.
sub picDir {
    my $picDir;

    if ( $sys eq "\\" ) {
        $picDir = 'mkdir C:\\Inetpub\\wwwroot\\~' . $user . '\\pictures';
    } elsif ( $sys eq "/" ) {
        $picDir = 'mkdir /var/www/html/~' . $user . '/pictures';
    }
    system($picDir);
}

#Subroutine for creation of Database Directory.
sub databaseDir {
    my $dbDir;

    if ( $sys eq "\\" ) {
        $dbDir = 'mkdir ' . 'C:\\Inetpub\\wwwroot\\~' . $user . '\\database';
    } elsif ( $sys eq "/" ) {
        $dbDir = 'mkdir /var/www/html/~' . $user . '/database';
    }
    system($dbDir);
}

#Subroutine for creation of Mail Directory.
sub mailDir {
    my $mailDir;

    if ( $sys eq "\\" ) {
        $mailDir = 'mkdir C:\\Inetpub\\wwwroot\\~' . $user . '\\mail';
    } elsif ( $sys eq "/" ) {
        $mailDir = 'mkdir /var/www/html/~' . $user . '/mail';
    }
    system($mailDir);
}

#Actions happen here.
checkSys();

我知道堆栈溢出不是放置作业的地方,但我不确定为什么这样的东西不起作用。

1 个答案:

答案 0 :(得分:1)

这不是CGI脚本,因此使用CGI模块(我今后称之为&#34; CGI.pm&#34;)是没有意义的。这是你问题的根源。

在CGI调用中,数据有时通过STDIN传输。该数据可能不是文本,因此CGI.pm确保binmode(STDIN);以保持其完整性。

这意味着当您从STDIN读取一行时,CRLF会更长时间转换为LF,因此您在chomp之后的变量中留下尾随CR。

如果您想继续使用CGI.pm,解决方法是使用

$var =~ s/\r?\n\z//;

或更好

$var =~ s/\s+\z//;

而不是

chomp($var);

为了将来参考,您应该做的第一件事是检查错误消息中包含open返回的错误$!。在错误消息中包含您传递给open的文件名也是一个好主意,这可能会显示此问题。

如果您需要检查变量是否实际包含您认为包含的内容,我建议

use Data::Dumper;
{ local $Data::Dumper::Useqq = 1; print(Dumper($var)); }