Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 在串联(.)或字符串中使用未初始化的值$domain_String_Perl_Initialization_Concatenation - Fatal编程技术网

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

String 在串联(.)或字符串中使用未初始化的值$domain,string,perl,initialization,concatenation,String,Perl,Initialization,Concatenation,我今天写了一个剧本。其目的是自动向Apache添加新的虚拟主机。应使用以下命令执行脚本: ./new_vhost.pl --add --domain google.com --client google. 然而,它并不能很好地工作,反而会给我带来错误 在串联(.)或字符串中使用未初始化的值$domain 你们以前有过这种经历吗?我想原因是google.com上的“.”,虽然我不是很确定。我还在谷歌上搜索答案,但不幸的是,没有什么能帮我解决这个问题 代码: 运行脚本后,test.conf将被删

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

./new_vhost.pl --add --domain google.com --client google. 
然而,它并不能很好地工作,反而会给我带来错误

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

你们以前有过这种经历吗?我想原因是google.com上的“.”,虽然我不是很确定。我还在谷歌上搜索答案,但不幸的是,没有什么能帮我解决这个问题

代码:

运行脚本后,test.conf将被删除

#***** 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 . *****#
#****开始配置*****#
#域:
#客户:
#日期:
服务器管理员@
DocumentRoot/var/www/html/
服务器名
服务器别名www。
ErrorLog/var/log/httpd/-error\u log
CustomLog/var/log/httpd/-access\u log common
#*****的配置结束*****#
但我期待着这样的事情

#***** 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. *****#
#****开始google.com的配置*****#
#域名:google.com
#客户:谷歌
#日期:2014年4月8日星期二17:27:39
服务器管理员admin@google.com
DocumentRoot/var/www/html/google.com
服务器名google.com
ServerAlias www.google.com
ErrorLog/var/log/httpd/google.com/google.com-error\u log
CustomLog/var/log/httpd/google.com/google.com-access\u log common
#*****google.com的配置结束*****#

GetOptions
试图调用您的
add\u vhost
时,变量
$domain
$client
可能无法初始化

您可以将代码更改为

my $add = undef;

...

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

if ($add) {
    add_vhost();
}

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

\35;/usr/bin/perl
#!/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";

}
严格使用; 使用警告; 使用Getopt::Long; 使用化学武器; 使用POSIX“strftime”; my$dir=getcwd; my$conf='test.conf'; 我的$domain=undf; 我的$client=undf; 我的$help=undf; 我的$ltime=未定义; 我的$to_add_vhost=undef; 如果(-e$conf){ 次级用途{ die“用法:$0[--add |--remove][--domain google.com][--client google]。\n”; } 使用,除非@ARGV>3; 获取选项( “添加”=>\$到“添加”vhost, #“删除”=>\&rem\u vhost, 'domain=s'=>\$domain, 'client=s'=>\$client, “帮助”=>\&用法 )| |用法; $domain=lc($domain); $client=uc($client); $ltime=本地时间; &添加成本,如果(定义为$to添加成本); 子添加虚拟主机{ 打开(CONF,“>>$CONF”)| | die“错误:无法打开$CONF。\n”; 打印配置“#****开始$domain.******”的配置; 打印配置“\n#域:$Domain\n”; 打印配置“#客户端:$Client\n”; 打印配置“#日期:$ltime\n”; 打印配置“\n”; 打印CONF“ServerAdmin\@$domain\n”; 打印CONF“DocumentRoot/var/www/html/$domain\n”; 打印CONF“ServerName$domain\n”; 打印CONF“ServerAlias www.$domain\n”; 打印CONF“ErrorLog/var/log/httpd/$domain/$domain-error\u log\n”; 打印CONF“CustomLog/var/log/httpd/$domain/$domain-access\u log common\n”; 打印配置“\n”; 打印配置“#***结束$domain.******\n的配置”; 关闭形态; 如果($?!=0){ die“错误:无法创建新配置。\n”; }否则{ 打印“确定:已添加$domain的新配置。\n”; } } }否则{ die“错误:$conf未找到。\n”; }
我建议您更改一些样式来清理代码

  • 检查错误时,
    die
    立即执行,而不是在
    else
    之后执行
  • 您可以在实际的子例程调用中声明
    GetOptions
    的参数,以简化操作
  • 考虑在打印一大块文本时使用HERE_DOC
  • 在调用
    GetOptions
    之前,您在
    @ARGV
    上检查错误是不必要的。让
    GetOptions
    通过使用所需的参数来处理这个问题。实际上,您当前将在有效的调用方法下失败,
    --domain=value--client=value',因为只有2个元素在
    @ARGV`中,而不是4个元素
这会将代码清理为以下内容:

#!/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";
    }
}
#/usr/bin/perl
严格使用;
使用警告;
使用自动模具;
使用Getopt::Long;
使用化学武器;
使用POSIX“strftime”;
my$dir=getcwd;
my$conf='test.conf';
die“错误:$conf未找到。\n”如果-e$conf;
次级用途{
die“用法:$0[--add |--remove][--domain google.com][--client google]。\n”;
}
获取选项(
“添加”=>\my$add,
#“删除”=>\&rem\u vhost,
'domain=s'=>\my$domain,
'client=s'=>\my$client,
“帮助”=>\&用法
)或用法();
$domain=lc($domain);
$client=uc($client);
my$ltime=本地时间;
如果($add){
添加_vhost();
}
子添加虚拟主机{
打开我的$fh,“>>”,$conf;

print$fh此脚本对我来说运行良好,但您从未调用过
sub add_vhost
。我怀疑这不是您正在运行的脚本。您好@M42!我第一次运行脚本时,它不起作用。我刚刚意识到我忘记声明一个新的未定义变量
add
,我错误地指出了
add
参数添加到
add_vhost
sub,它会立即执行,而不会获得我传递给
$domain
$client
的信息。我很高兴它现在运行良好。也感谢您的时间!:)酷!这很好!感谢您的回答。它现在正在工作。根据建议,我已将我的
add_vhost
移出公司
#!/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";

}
#!/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";
    }
}