Why do you have 3 elsif where a simple else will be executed?
I mean, they only check what matches the tested if .
if ($host eq '') { ... } elsif ($host ne '') { ... }
if ($config{$_}{host} ne $host) { ... } elsif ($config{$_}{host} eq $host) { ... }
if ($ping_obj->ping($host)) { ... } elsif (! $ping_obj->ping($host)) { ... }
I would use a regular while instead of a do{...}while(...) .
do{ RESTART: if(...){ goto RESTART; } }while(...);
vs
while(...){ if(...){ redo; } }
In this loop, you only use the %config keys to find the associated value, so why not use the values %config .
foreach (keys %config) { if ($config{$_}{host} ne $host) { last; } elsif ($config{$_}{host} eq $host) { warn "Configuration for $host already exists!\n"; } }
vs
for( values %config ){ if( $_->{host} ne $host ){ ... } else { ... } }
If you are using 5.10.0 or later, you can use smart matching ( ~~ ) instead, which would make it clearer what you are testing.
my @hosts = map{ $_->{host} } values %config; if( $host ~~ @hosts ){ ... }
source share