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

Dear monks

I get the error Undefined Subroutine &main::get called at Webcrawler.pl line 38, <STDIN> line 1. The code is below:

#!/usr/bin/perl use strict; #Module used to go through the web pages, Can extract links, save them + and also strip the HTML of its contents use WWW::Mechanize; use HTML::TreeBuilder; print "WEB CRAWLER AND HTML EXTRACTOR \n"; print "Please input the URL of the site to be searched \n"; print "Please use a full URL (eg. http://www.dcs.shef.ac.uk/) \n"; #Create an instance of the webcrawler my $webcrawler = WWW::Mechanize->new(); my $url_name = <STDIN>; # The user inputs the URL to be searched #Grab the contents of the URL given by the user $webcrawler = get($url_name);

There is more code in the program but i don't think it matters. Line 38 is the $webcrawler = get($url_name); line. Also, the <STDIN> is not on line 1 which buffles me too, i have quite a few comments written at the start and the my $url_name = <STDIN>; bit is line 35. I thought the problem here was because i didn't have installed the LWP::Simple module but i do have it after all. Any ideas?Thanks

Replies are listed 'Best First'.
Re: Subroutine &main::get error
by jhourcle (Prior) on Jul 31, 2005 at 01:12 UTC

    Let's decompose the error message...

    Undefined Subroutine &main::get called at Webcrawler.pl line 38, <STDIN> line 1.
    <STDIN> line 1
    One line has been read from STDIN. (which is true at line 38)
    at Webcrawler.pl line 38
    The problem is in the file 'Webcrawler.pl' on line 38.
    Undefined Subroutine &main::get called
    You attempted to call the function 'get' in the 'main' (default) namespace, but the function wasn't defined.

    So ... looking at line 38:

     $webcrawler = get($url_name);

    You're attempting to assign to the scalar $webcrawler the returned value from get($url_name)

    More than likely, what you wanted to do was to call the 'get' method on $webcrawler, which is a WWW::Mechanize object:

    $webcrawler->get($url_name);

    Of course, from looking at the documentation from WWW::Mechanize, you'd see that get returns an HTTP::Response object, which you'll most likely want to assign to something, so you can interact with it.

Re: Subroutine &main::get error
by GrandFather (Saint) on Jul 31, 2005 at 01:17 UTC
Re: Subroutine &main::get error
by Tanktalus (Canon) on Jul 31, 2005 at 01:11 UTC

    I highly doubt that the get routine is exported from WWW::Mechanize. Further, you're assigning back to your $webcrawler, so you're going to lose your object created by WWW::Mechanize->new(). So try something like:

    my $page = $webcrawler->get($url_name);
    At least, that's the guess I have for not having used, or even installed, W::M.

Re: Subroutine &main::get error
by fauria (Deacon) on Jul 31, 2005 at 01:18 UTC
    I think you might use LWP, or LWP::Simple to grab the site to a scalar $webcrawler var by using 'get', altough it is not necessary since WWW::Mechanize provides a get methot for the $webcrawler object you already have. That can be invoked by using $webcrawler->get($url_name).
      Wow, that was a daft mistake...Anyway,thanks to everyone getting back to me in replies to my questions, this script is the first time i have used Perl and you lot are making my life a lot easier...Thanks!