Front page | perl.libwww |
Postings from November 2002
Re: URI Bug?
From: Gisle Aas
November 23, 2002 20:40
Re: URI Bug?
Message ID: email@example.com
Darby Cacdac <firstname.lastname@example.org> writes:
> Lemme give you a code sample:
> use URI::URL;
> $attr_value = "products/index.html"; # This is a valid relative link!
> $url = url $attr_value;
> $host = $url->host;
> Running the above code generates the error below. In my mind it
> shouldn't produce an error because $attr_value is a valid url (even if
> it doen't have the host element explicitly included). In the above
> case, I'm expecting $url->host to return null or undefined but not a
> lousy error message!
When constructing relative URIs you should normally pass a second
argument to the constructor. The second argument is used to determine
what class of URI this is, and is either just the base URI or a string
denoting a scheme. If you don't provide this you only have the
generic methods available, and that does not include 'host'.
$attr_value = "products/index.html"; # This is a valid relative link!
$url = URI->new($attr_value, "http");
$host = $url->host;
> If you were to change the $url->host line above to to $url->scheme or
> $url->frag, no errors are generated so why generate and error when
> you try to get the host portion? There has to be consistency.
Because 'scheme' and 'frag' are attributes that always make sense for
any kind of URI. Not all URIs has 'host' attribute and URI.pm
represent that by having the host method unimplemented for those.