首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未初始化值$domain在级联(.)中的使用或串

未初始化值$domain在级联(.)中的使用或串
EN

Stack Overflow用户
提问于 2014-04-08 08:56:18
回答 3查看 1.3K关注 0票数 0

我今天写了剧本。这样做的目的是自动向Apache添加新的虚拟主机。应该使用以下命令执行脚本:

代码语言:javascript
复制
./new_vhost.pl --add --domain google.com --client google. 

然而,它不能很好地工作,相反,它会给我带来错误

未初始化值$domain在级联(.)中的使用或串

你们以前有过这种经历吗?我在想原因是“。”不过,在google.com中,我对此并不十分肯定。我仍然在谷歌上寻找答案,但不幸的是,没有什么能帮我解决这个问题。

代码:

代码语言:javascript
复制
#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long;
use Cwd;
use POSIX "strftime";

my $dir = getcwd;
my $conf = 'test.conf';

my $domain = undef;
my $client = undef;
my $help = undef;
my $ltime = undef;

if (-e $conf) {
        sub usage {
                die "Usage: $0 [--add | --remove] [--domain google.com] [--client google].\n";
        }

        usage unless @ARGV > 3;
        GetOptions (
                'add' => \&add_vhost,
#               'remove' => \&rem_vhost,
                'domain=s' => \$domain,
                'client=s' => \$client,
                'help' => \&usage
        ) || usage;

        $domain = lc($domain);
        $client = uc($client);
        $ltime = localtime;

        sub add_vhost {
                open (CONF, ">>$conf") || die "ERROR: unable to open $conf.\n";
                print CONF "#***** Start of configuration for $domain. *****#";
                print CONF "\n# Domain: $domain\n";
                print CONF "# Client: $client\n";
                print CONF "# Date: $ltime\n";
                print CONF "<VirtualHost *:1111>\n";
                print CONF "    ServerAdmin admin\@$domain\n";
                print CONF "    DocumentRoot /var/www/html/$domain\n";
                print CONF "    ServerName $domain\n";
                print CONF "    ServerAlias www.$domain\n";
                print CONF "    ErrorLog /var/log/httpd/$domain/$domain-error_log\n";
                print CONF "    CustomLog /var/log/httpd/$domain/$domain-access_log common\n";
                print CONF "</VirtualHost>\n";
                print CONF "#***** End of configuration for $domain. *****#\n";
                close CONF;
                if ($? != 0) {
                        die "Error: unable to create a new configuration.\n";
                } else {
                        print "OK: new configuration for $domain has been added.\n";
                }
        }
} else {
        die "ERROR: $conf not found.\n";

}

错误:

代码语言:javascript
复制
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 37.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 38.
Use of uninitialized value $client in concatenation (.) or string at ./new_vhost.pl line 39.
Use of uninitialized value $ltime in concatenation (.) or string at ./new_vhost.pl line 40.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 42.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 43.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 44.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 45.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 46.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 46.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 47.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 47.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 49.
Use of uninitialized value $domain in concatenation (.) or string at ./new_vhost.pl line 54.
OK: new configuration for  has been added.

运行脚本后的test.conf。

代码语言:javascript
复制
#***** Start of configuration for . *****#
# Domain:
# Client:
# Date:
<VirtualHost *:1111>
    ServerAdmin admin@
    DocumentRoot /var/www/html/
    ServerName
    ServerAlias www.
    ErrorLog /var/log/httpd//-error_log
    CustomLog /var/log/httpd//-access_log common
</VirtualHost>
#***** End of configuration for . *****#

但我以为会发生这样的事..。

代码语言:javascript
复制
#***** Start of configuration for google.com. *****#
# Domain: google.com
# Client: GOOGLE
# Date: Tue Apr  8 17:27:39 2014
<VirtualHost *:1111>
    ServerAdmin admin@google.com
    DocumentRoot /var/www/html/google.com
    ServerName google.com
    ServerAlias www.google.com
    ErrorLog /var/log/httpd/google.com/google.com-error_log
    CustomLog /var/log/httpd/google.com/google.com-access_log common
</VirtualHost>
#***** End of configuration for google.com. *****#
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-04-08 09:13:24

GetOptions试图调用您的add_vhost时,可能不会初始化变量$domain$client

您可以将代码更改为

代码语言:javascript
复制
my $add = undef;

...

GetOptions (
    'add' => \$add,
...

if ($add) {
    add_vhost();
}

...

顺便说一句,最好将add_vhost的定义从该条件语句中移出,并将这些信息(如$domain作为参数传递给add_vhost,而不是使用全局变量)。

票数 1
EN

Stack Overflow用户

发布于 2014-04-08 09:17:20

代码语言:javascript
复制
#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long;
use Cwd;
use POSIX "strftime";

my $dir = getcwd;
my $conf = 'test.conf';

my $domain = undef;
my $client = undef;
my $help = undef;
my $ltime = undef;
my $to_add_vhost = undef;

if (-e $conf) {
        sub usage {
                die "Usage: $0 [--add | --remove] [--domain google.com] [--client google].\n";
        }

        usage unless @ARGV > 3;
        GetOptions (
                'add' => \$to_add_vhost,
#               'remove' => \&rem_vhost,
                'domain=s' => \$domain,
                'client=s' => \$client,
                'help' => \&usage
        ) || usage;

        $domain = lc($domain);
        $client = uc($client);
        $ltime = localtime;

    &add_vhost if (defined $to_add_vhost);

        sub add_vhost {
                open (CONF, ">>$conf") || die "ERROR: unable to open $conf.\n";
                print CONF "#***** Start of configuration for $domain. *****#";
                print CONF "\n# Domain: $domain\n";
                print CONF "# Client: $client\n";
                print CONF "# Date: $ltime\n";
                print CONF "<VirtualHost *:1111>\n";
                print CONF "    ServerAdmin admin\@$domain\n";
                print CONF "    DocumentRoot /var/www/html/$domain\n";
                print CONF "    ServerName $domain\n";
                print CONF "    ServerAlias www.$domain\n";
                print CONF "    ErrorLog /var/log/httpd/$domain/$domain-error_log\n";
                print CONF "    CustomLog /var/log/httpd/$domain/$domain-access_log common\n";
                print CONF "</VirtualHost>\n";
                print CONF "#***** End of configuration for $domain. *****#\n";
                close CONF;
                if ($? != 0) {
                        die "Error: unable to create a new configuration.\n";
                } else {
                        print "OK: new configuration for $domain has been added.\n";
                }
        }
} else {
        die "ERROR: $conf not found.\n";

}
票数 0
EN

Stack Overflow用户

发布于 2014-04-09 01:46:47

我建议您进行一些样式更改,以清理您的代码。

  • 当检查错误时,die立即而不是在else之后。
  • 您可以在实际的子例程调用中声明GetOptions的参数,以简化操作。
  • 在打印一个大文本块时考虑使用HERE_DOC。
  • 在调用@ARGV之前对GetOptions进行错误检查是不必要的。让GetOptions通过拥有所需的参数来处理这个问题。目前,在有效的调用方法--domain=value --client=value' because only 2 elements will be in@ARGV` ` is 4下,您将失败。

这会将您的代码清理为以下内容:

代码语言:javascript
复制
#!/usr/bin/perl

use strict;
use warnings;
use autodie;

use Getopt::Long;
use Cwd;
use POSIX "strftime";

my $dir = getcwd;
my $conf = 'test.conf';

die "ERROR: $conf not found.\n" if ! -e $conf;

sub usage {
    die "Usage: $0 [--add | --remove] [--domain google.com] [--client google].\n";
}

GetOptions(
    'add'      => \my $add,
#   'remove'   => \&rem_vhost,
    'domain=s' => \my $domain,
    'client=s' => \my $client,
    'help'     => \&usage
) or usage();

$domain = lc($domain);
$client = uc($client);
my $ltime = localtime;

if ($add) {
    add_vhost();
}

sub add_vhost {
    open my $fh, '>>', $conf;

    print $fh <<"END_CONF";
#***** Start of configuration for $domain. *****#
# Domain: $domain
# Client: $client
# Date: $ltime
<VirtualHost *:1111>
    ServerAdmin admin\@$domain
    DocumentRoot /var/www/html/$domain
    ServerName $domain
    ServerAlias www.$domain
    ErrorLog /var/log/httpd/$domain/${domain}-error_log
    CustomLog /var/log/httpd/$domain/${domain}-access_log common
</VirtualHost>
#***** End of configuration for $domain. *****#
END_CONF

    if ($? != 0) {
        die "Error: unable to create a new configuration.\n";
    } else {
        print "OK: new configuration for $domain has been added.\n";
    }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22932183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档