Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Re: Re: How to get HTML::Parser to return a line of parsed text

by donfreenut (Sexton)
on Feb 06, 2001 at 20:31 UTC ( #56689=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: How to get HTML::Parser to return a line of parsed text
in thread How to get HTML::Parser to return a line of parsed text


These are probably extremely stupid questions, but I couldn't find answers to them - I only have merlyn's llama book on my desk; no camel yet.

First of all, what does the backslash do in \my @accum?

Secondly, what is the 'map' keyword, and what does the $_->[0] refer to?

Apologies if these are too trivial...


Comment on Re: Re: Re: How to get HTML::Parser to return a line of parsed text
Select or Download Code
Re: Re: Re: Re: How to get HTML::Parser to return a line of parsed text
by davorg (Chancellor) on Feb 06, 2001 at 21:03 UTC

    You need to pass a reference to an array. I'd probably declare @array first and then use \@array, but merlyn has done both in one step (I didn't know you could do that - never too old to learn new stuff I suppose!)

    map is an operator that takes a block of code and an array. It runs (and returns the results of running) the block of code once for each element in the array. Each element is aliased to $_ within the block.

    When you pass an array ref, instead of a sub ref, to HTML::Parser->new it will store all of the values that would have been parameters to the handler, in the array. In our case it's just one value ('text') but it could well be more. Because of that, the array is in fact an array of arrays (or, more accurately, an array of array references).

    Therefore each time the map block is called, $_ contains a reference to a one-element array and $_->[0] contains the value of the first (and only) element in that array. The whole map call returns the complete list of these elements, effectively flattening the array to one dimension.

    Does that make it clearer?

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      You can use my any place that you can have an lvalue, and it remains an lvalue. I've been using that more and more in my programs, and it's been pretty slick.

      -- Randal L. Schwartz, Perl hacker

        Slick is as slick does.
        use HTML::Parser; use LWP::Simple qw(get); my $html = get("http://anime.com"); my @accum = (); my $parser = HTML::Parser->new( text_h => [\@accum, "text"] ); $parser->parse( $html ); for (@accum) { print $_->[0]; }
        :P
Re: Re: Re: Re: How to get HTML::Parser to return a line of parsed text
by arturo (Vicar) on Feb 06, 2001 at 23:33 UTC

    Just by way of handing you a fishing pole, note that for just about any function in Perl, perldoc -f [function_name] will tell you what it does and how to use it. You can also Search on this site to see other Q&A's. I also like to point folks to the Tutorials section, as much wisdom is contained therein.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://56689]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-12-26 20:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls