I have inherited a perl code base. Consider the following subroutine;
sub getSysRTable
{
my $iface = shift;
return if not length($iface);
my %ip_routes;
my @routes = `/usr/bin/netstat -rn`;
foreach my $route(@routes) {
if ($route =~ /([\S.]+)\s+([\d.]+.[\d.]+.[\d.]+.[\d.]+)\s+(UGS|UGHS)\s+($iface)/ )
{ $ip_routes {$1} = $2 }
}
return %ip_routes;
}
I want to write unit tests for this code. The testing I have in mind will use sample output from netstat -rn
and check for expected behaviour. The sub as is, invokes a command, so injecting my test data is problematic with this implementation.
What is the idiomatic perlish approach to refactoring this sub for testability?