Nmap::Scanner will do all this for you.
In reply to Re: Capturing the output of nmap from within a Perl script by ghenry in thread Capturing the output of nmap from within a Perl script by spaceout