perl – 如何在突破foreach循环后重新启动“do-while”循环?
发布时间:2020-12-16 06:20:47 所属栏目:大数据 来源:网络整理
导读:我的配置脚本中有一小段代码,想法是加载配置然后检查每个密钥是否输入了主机名.但是,如果发现配置包含相同的主机名,则会被拒绝,并显示一条警告消息,指出已存在具有该主机名的配置. 问题是我需要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 (...); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |