perl – 如何在突破foreach循环后重新启动“do-while”循环?

前端之家收集整理的这篇文章主要介绍了perl – 如何在突破foreach循环后重新启动“do-while”循环?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的配置脚本中有一小段代码,想法是加载配置然后检查每个密钥是否输入了主机名.但是,如果发现配置包含相同的主机名,则会被拒绝,并显示一条警告消息,指出已存在具有该主机名的配置.

问题是我需要foreach循环来检查散列键的存在以重新启动do-while循环,这样可以尝试另一个主机名,或者用户可以从脚本中取出.

这是片段;

my $host;
do {
    print "Enter the hostname or IP of the ESXi server: ";
    chomp($host = <STDIN>);

    if ($host eq '') {
        print "You must enter a hostname or IP address!\n";
    } elsif ($host ne '') {

        # We need to catch duplicate configurations for we don't do the same work twice
        foreach (keys %config) {
            if ($config{$_}{host} ne $host) {
                last;
            } elsif ($config{$_}{host} eq $host) {
                warn "Configuration for $host already exists!\n";
            }
        }

        if ($ping_obj->ping($host)) {
            $config{$config_tag}{host} = $host;
        } elsif (! $ping_obj->ping($host)) {
            print RED . "Ping test for \'$host\' Failed" . RESET . "\n";
        }

        $ping_obj->close();
    }
} while ($config{$config_tag}{host} eq 'undef');

这就是模板哈希的样子.

my %template = (
    host => 'undef',port => 'undef',login => {
        user => 'undef',password => 'undef',},options => {
        snapshots => "0",compress => "0",# This is expressed as an array
        exclude => 'undef',);

解决方法

如果在Perl中有一个 goto LABEL语句的用法,那就是它.

do {
    START:     # could also go right before the "do"
    ...
    if (...) {
        warn "Configuration exists. Start over.\n";
        goto START;
    }
} while (...);

猜你在找的Perl相关文章