RobertJ has asked for the wisdom of the Perl Monks concerning the following question:

I have a script which requires the URI.pm for a site that hosts my domain. I downloaded the URI package from CPAN and installed it in my own domain. When I try to run the scrip, I get the following error message:

Can't locate object method "host" via package "URI::_generic" at httpview.cgi line 91

Lines 90-93 of my script are

90: my $url = URI->new($a);<br> 91: my $host = $url->host();<br> 92: my $port = $url->port();<br> 93: my $path = $url->path();<br>
This is the first time I've installed a perl module and I haven't a clue what is going on.

Replies are listed 'Best First'.
Re: URI Module can't find method
by ikegami (Patriarch) on Nov 17, 2004 at 21:36 UTC

    I bet $a is a relative uri or not a uri at all. Update: Or maybe the URI uses a scheme URI.pm doesn't recognize.

    use URI (); $uri = URI->new('index.pl'); print('Host of Relative URI ', $uri->as_string(), ':', $/); eval { print($uri->host(), $/); }; print($@) if ($@); print($/); $uri = URI->new('www.perlmonks.com'); print('Host of something (', $uri->as_string(), ') that isn\'t a URI a +t all:', $/); eval { print($uri->host(), $/); }; print($@) if ($@); print($/); $uri = URI->new('ikegami://www.perlmonks.com/'); print('Host of a URI (', $uri->as_string(), ') whose scheme URI.pm doe +sn\'t recognize:', $/); eval { print($uri->host(), $/); }; print($@) if ($@); print($/); $uri = URI->new('http://www.perlmonks.com/'); print('Host of Absolute URI ', $uri->as_string(), ':', $/); eval { print($uri->host(), $/); }; print($@) if ($@); print($/); $base_uri = URI->new('http://www.perlmonks.com/'); $rel_uri = URI->new('index.pl'); $abs_uri = $rel_uri->abs($base_uri); print('Host of Relative URI ', $rel_uri->as_string(), ' converted to a + Absolute URI using Base URI ', $base_uri->as_string(), ':', $/); eval { print($abs_uri->host(), $/); }; print($@) if ($@); print($/); __END__ output ====== Host of Relative URI index.pl: Can't locate object method "host" via package "URI::_generic" (perhaps + you forgot to load "URI::_generic"?) at !.pl line 5. Host of something (www.perlmonks.com) that isn't a URI at all: Can't locate object method "host" via package "URI::_generic" (perhaps + you forgot to load "URI::_generic"?) at !.pl line 11. Host of a URI (ikegami://www.perlmonks.com/) whose scheme URI.pm doesn +'t recognize: Can't locate object method "host" via package "URI::_foreign" (perhaps + you forgot to load "URI::_foreign"?) at !.pl line 17. Host of Absolute URI http://www.perlmonks.com/: www.perlmonks.com Host of Relative URI index.pl converted to a Absolute URI using Base U +RI http://www.perlmonks.com/: www.perlmonks.com
      " I bet $a is a relative uri or not a uri at all."

      $a is a uri. I got this script from the CS department at UNC and I know it works. My only problem is that my hosting company didn't have URI installed to I had to install it locally in my domain.

      My host uses Apache on Unix.

      "perhaps you forgot to load "URI::_generic?"

      I loaded the entire URI module including all 40+ supporting packages.

        Another possibility is that the URI is of a scheme URI.pm doesn't recognize. What does $a contain?

        >perl -MURI::HTTP -e'' >perl -MURI::ikegami -e'' Can't locate URI/ikegami.pm in @INC (@INC contains: ...). BEGIN failed--compilation aborted.
Re: URI Module can't find method
by mojotoad (Monsignor) on Nov 17, 2004 at 22:33 UTC
    The actual URI subclass that gets loaded depends on the scheme (for a string) or the type of object being passed to the constructor. Can you give an example of the type of url you're trying to load?

    Matt

      Here is the code

      sub process_request { my $a = shift @_; if (not $a =~ m!^http://.*!) { $a = "http://$a" } my $url = URI->new($a); my $host = $url->host(); my $port = $url->port(); my $path = $url->path(); if ($port eq "") { $port = 80; } if ($path eq "") { $path = "/" } return ($host, $port, $path); }


      The $a is www.cnn.com or anything like that.
        Are you sure $a will only be that? For example, 'ftp://blah.com' and 'https://somewhere.com' are valid strings for a URI which would blow up in your example since they use different schemes other than 'http'.

        Perhaps a better strategy would be to let the URI module do that work for you since that's what it does best:

        my $str = shift @_; my $uri = URI->new($str); $uri->scheme('http') unless $uri->scheme; ...

        Cheers,
        Matt

Re: URI Module can't find method
by simonm (Vicar) on Nov 18, 2004 at 18:51 UTC
    It sounds like maybe you didn't install the package correctly... What procedure did you follow to install it?