in reply to multiple checks on a conditional

An additional note on general coding practice. Apparently $regex is a global. That means it's floating around in your general name space and strange and unhappy things can happen later as the program expands -- OR you will come back to the program later and it may be tricky to figure out exactly where the value of $regex is being set.

None of this may seem like it's important here, but imagine a different program with a global var that is altered in one sub and used in another. There's no trail of bread crumbs to lead you from the using sub to the altering sub.

So declare it as a parameter and have pity on the next person to work with this code. (It will likely be you!) Good habits are built by practicing them on the easy stuff!

Including lestrrat's excellent suggestions.

# Main Code my ($i_val, $a_regex); # ...set $a_regex to some useful value (or not) # ...set $i_val execute($i_val, $a_regex); sub execute { my ($i, $regex) = @_; return unless $regex; my $compiled = qr/$regex/; my $mib = "$oid{$i}$tftpserver"; for my $host(@routers) { chomp $host; next if $host !~ /$compiled/; ... } }
Also: Another approach to the optimization lestrrat offers is to use the /o operator:
sub execute { my ($i, $regex) = @_; return unless $regex; my $mib = "$oid{$i}$tftpserver"; for my $host(@routers) { chomp $host; next if $host !~ /$regex/o; # HERE ... } }