in reply to Strange undefined scalars in array

push (@switches, my $portcount);
is (mostly) equal to
push (@switches, undef);
And that's likely where your undefs come from.