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

Hello there, this is my first program in Perl and i have to say i find it a much simpler language than previous ones i have encountered(i.e java, prolog). My problem has to do with hooks. I am using the WWW:Robot module which acts like a web robot. Information on the module can be found at http://search.cpan.org/dist/WWW-Robot/lib/WWW/Robot.pm. I am writing some code of my own on top of this. When it goes through a web page it extracts its HTML content. Eventually i would want the content to go in a text file. My problem now is that i can't understand how to store the content in a string. The code from the WWW::Robot doc is:
sub invoke_contents { my($webcrawler, $hook, $url, $response, $structure) = @_;
I have the "Programming Perl" book and i have also looked around different tutorials but i can't understand how @_ works. Does it make something like:
$webcrawler = @_; $hook=@_; . . . $structure=@_;
as in to store everything in the hook on its own? I wrote "our $contents = $structure;" in this sub so that to make the $structure variable global and be ale to use it outside the sub so as to store the content in an HTML file. Any idea if i could do what i want to do in a better way or any way that would work? Help is greatly appreciated.

20050718 Edit by ysth: code tags

Replies are listed 'Best First'.
Re: Extracting information from hooks
by ikegami (Patriarch) on Jul 18, 2005 at 22:40 UTC
    my($webcrawler, $hook, $url, $response, $structure) = @_;

    I have the "Programming Perl" book and i have also looked around different tutorials but i can't understand how @_ works. Does it make something like:

    $webcrawler = @_; $hook=@_; . . . $structure=@_;

    No. @_ is an array (which contains the parameters passed to the function). When assigning to a list ((...)), the right hand side of the assignment is converted to a list (($_[0], $_[1], ...) in this case), the the first item in the RHS list is assigned the first item in the LHS list, the second item in the RHS list is assigned to the second item in the LHS list, etc. So,

    ($webcrawler, $hook, $url, $response, $structure) = @_;

    is the same as

    $webcrawler = $_[0]; $hook = $_[1]; $url = $_[2]; $response = $_[3]; $structure = $_[4];

    In other words, it provides names for the arguments. (It has the side effect of making a copy of them.)

     

    My problem now is that i can't understand how to store the content in a string.

    The byte-for-byte response is available from $response->content(). Alternatively, you could traverse the HTML tree provided in $structure. Any of the dumping methods listed in HTML::Element would work. e.g. $structure->as_text().

Re: Extracting information from hooks
by Anonymous Monk on Jul 18, 2005 at 22:46 UTC
    Have you read your book? Look in the index for @_ and read the chapter referenced (2.7).