develooper Front page | perl.libwww | Postings from November 2002

Re: URI Bug?

Thread Previous | Thread Next
Gisle Aas
November 23, 2002 20:40
Re: URI Bug?
Message ID:
Darby Cacdac <> writes:

> Lemme give you a code sample:
> #!/usr/bin/perl
> 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'.

  use URI;
  $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
represent that by having the host method unimplemented for those.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About