in reply to Help with warnings!!!
This is because if a line fails to match, perl will not reset $1, $2, $3, $4, so they will still have a value from the previous match.foreach (@lines) { if(/(\w+)\s+(\w+:\w+)\s+(\d+\.\d+\.\d+\.\d+):\w+\s+(\w+)/) { $protocol = $1; $localAddress = $2; $foreignAddress = $3; $state = $4; print "$protocol, $localAddress, $foreignAddress, $state\n"; } }
Also, if you are not going to use the @lines array for something else, it's much more memory-efficient to combine the loops like this:
open NETSTAT, "$program|" or die "Cannot open netstat $!\n"; while (<NETSTAT>) { if(/(\w+)\s+(\w+:\w+)\s+(\d+\.\d+\.\d+\.\d+):\w+\s+(\w+)/) { print "$1, $2, $3, $4\n"; # protocol, localAddress, foreignAddress, state } }
|
|---|