in reply to Determining foreign port number

use URI::URL (); my $url = URI::URL->new($url_as_string) or warn "URL error"; print $url->port

$url_as_string would be the full URL you are parsing, such as http://www.example.com/ or http://www.example.com:8080/url.html.