Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 获得;“基本URL”;一个URL的名称_Perl_Url - Fatal编程技术网

Perl 获得;“基本URL”;一个URL的名称

Perl 获得;“基本URL”;一个URL的名称,perl,url,Perl,Url,在我的应用程序中,我需要获取任何URL的基本URL。到目前为止,我一直是这样理解的: $resp->base; 其中,$resp是一个对象。但是,现在我需要知道URL的基础,而不需要实际请求它们并返回HTTP::Response对象。有人知道这样做的方法吗?您想找到“”相对于您拥有的url展开的绝对url。我能做到 $ perl -MURI -E'say URI->new_abs(".", $_) for @ARGV' \ 'http://www.example.org/di

在我的应用程序中,我需要获取任何URL的基本URL。到目前为止,我一直是这样理解的:

$resp->base;

其中,
$resp
是一个对象。但是,现在我需要知道URL的基础,而不需要实际请求它们并返回
HTTP::Response
对象。有人知道这样做的方法吗?

您想找到“
”相对于您拥有的url展开的绝对url。我能做到

$ perl -MURI -E'say URI->new_abs(".", $_) for @ARGV' \
   'http://www.example.org/dir/' \
   'http://www.example.org/dir/file.html' \
   'http://www.example.org/dir/file.cgi?foo=bar'
http://www.example.org/dir/
http://www.example.org/dir/
http://www.example.org/dir/

HTTP::Response->base
方法返回的值是从HTTP响应消息中获取的URL,用于指定如何解析消息内容中出现的相对URL。如果消息没有指定这样的值,则使用用于请求消息的URL

如果您想要来自
HTTP::Response
的URL,假设响应中没有相反的信息,那么只需按原样使用URL即可。它可以很好地作为一个基本URL

任意URL没有单一的基。基本URL是用于解析相对URL的绝对URL,可以有任意数量的路径步骤

比如说,

http://news.bbc.co.uk/1/hi/programmes/click_online/9735140.stm
可以表示为

9735140.stm
相对于基本URL

http://news.bbc.co.uk/1/hi/programmes/click_online/
http://news.bbc.co.uk/
或作为

相对于基本URL

http://news.bbc.co.uk/1/hi/programmes/click_online/
http://news.bbc.co.uk/
当然,路径也可以在任何一点上划分为基本URL和相对URL

你没有说你想用这个值做什么。您选择的解决方案只是去掉URL路径组件的最后一个元素,该元素后面没有斜杠,因此对于URL来说也是如此

http://news.bbc.co.uk/1/hi/programmes/click_online
它回来了

http://news.bbc.co.uk/1/hi/programmes/
要不是

http://news.bbc.co.uk/1/hi/programmes/click_online/
它返回相同的值

http://news.bbc.co.uk/1/hi/programmes/click_online/

如果这是您想要的行为,那么一切都很好,但我不会将其描述为“获取基本URL”。

它返回一个URI对象,但URI对象会在需要时自动字符串化为它们表示的URI。您还可以使用
->as_string