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

Hi, i have asked about another error message before but now i get a syntax error message about the last line of my code. It says "syntax error at WebCrawler.pl near $webcrawler(".

It says in the documentation for WWW::Mechanize that i need the HTML::TreeBuilder but i have installed that so thats not the problem. The $stripped_html[$x] = $webcrawler( format => "text"); line is part of the module WWW::Mechanize.pm

Thanks

our $webcrawler = WWW::Mechanize->new(); #Grab the contents of the URL given by the user our $webcrawler = get($url_name); # Put the links that exist in the HTML of the URL given by the user i +n an array our @website_links = $webcrawler->links($url_name); # The HTML is stripped off the contents and the text is stored in an +array of strings our $x = 0; my @stripped_html; $stripped_html[$x] = $webcrawler( format => "text");

Replies are listed 'Best First'.
Re: Syntax error for WWW::Mechanize
by GrandFather (Saint) on Jul 30, 2005 at 11:56 UTC

    There looks to be a subroutine name missing there.


    Perl is Huffman encoded by design.
      What subroutine is missing? I am basically trying to save as a string the non-HTML content of a website using that part od the WWW::Mechanize code.

        You had:

        $stripped_html[$x] = $webcrawler( format => "text");

        $webcrawler is a scalar. If it were a scalar code ref, you could do &$webcrawler( format => 'text' ), but it looks to be to be a WWW::Mechanize object. So, what you have is a scalar, then two more scalars in parenthesis.

        You need some sort of operator between the '$webcrawler' and the parenthesis. More than likely, based on the parentheis, you're missing a method call:

        $webcrawler->method( format  => 'text' );

        So, what method call takes a hash as its argument, and one of the keys is format? We look at the documentation for WWW::Mechanize, and search for the string 'format', and we find the following:

        $mech->content( format => "text" )

        So, I can only guess that you should have written:

        $stripped_html[$x] = $webcrawler->content( format => "text");

        The following code may do something like what you are trying to achieve:

        use warnings; use strict; use WWW::Mechanize; use HTML::TreeBuilder; my $webcrawler = WWW::Mechanize->new(); my $page = $webcrawler->get ('http://perlmonks.org/?node_id=479598'); my $tree = HTML::TreeBuilder->new_from_content ($page->as_string ()); print $tree->as_text ();

        Perl is Huffman encoded by design.