简体   繁体   中英

Perl on windows not able to open a file

I wrote this script to create a basic website for a user that runs the script. It can do it in windows or unix systems, but for some reason the file will not open to be written to.

#!/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();

I understand that stack overflow is not a place to put homework, but I'm not sure why something like this isn't working.

This is not a CGI script, so it makes no sense to use the CGI module (which I'll henceforth call "CGI.pm"). This is the source of your problems.

In a CGI call, data is sometimes transferred via STDIN. That data may not be text, so CGI.pm does binmode(STDIN); to preserve its integrity.

This means that when you read a line from STDIN, CRLF is longer converted to LF, so your are left with a trailing CR in the variable after the chomp .

If you want to continue using CGI.pm, the workaround is to use

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

or better yet

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

instead of

chomp($var);

For future reference, the first thing you should have done is check what error open returned by including $! in the error message. It's a good idea to also include the file name you passed to open in the error message, which would have revealed this problem.

If you need to check if a variable actually contains what you think it contains, I recommend

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM