如何从Perl中的URL中提取方案,主机和端口?

goe*_*goe 3 url perl

我需要能够从URL中提取方案,主机和端口.

所以,如果我在浏览器中的网址是:http://www.example.com:80/something.pl 我需要能够得到:http://www.example.com:80

Sin*_*nür 9

URI模块可以帮助您切片和切块一个URI任何你想要哪种方式.

如果您尝试在CGI脚本中执行此操作,则需要查看$ENV{SERVER_NAME}$ENV{SERVER_PORT}.

使用url您正在使用的CGI模块的方法(例如CGI.pmCGI :: Simple)将使事情变得更加简单.


bri*_*foy 6

我让URI模块弄清楚了,所以我不必创建新的bug:

use 5.010;
use URI;

my $url = 'http://www.example.com:80/something.pl';

my $uri = URI->new( $url );

say $uri->scheme;
say $uri->host;
say $uri->port;
Run Code Online (Sandbox Code Playgroud)