-6

I came across a Perl code using HTML Parser like below

my $p = HTML::Parser->new(text_h => [ sub {$text .= shift}, 
                                  'dtext']);

Please help me to understand this.

halfer
  • 19,824
  • 17
  • 99
  • 186
RosAng
  • 1,010
  • 2
  • 18
  • 42
  • 2
    What is your question, exactly?! Do you want a tutorial on the Perl language and its syntax? Do you have questions about the specifics of the `HTML::Parser` module? Or something in-between? – Biffen Jun 17 '14 at 07:30
  • 1
    Please change the title to your actual question, so people who have a similar question will be able to find yours. – reinierpost Jun 17 '14 at 14:44

1 Answers1

2

From the documentation:

$p = HTML::Parser->new(api_version => 3,
                       text_h => [ sub {...}, "dtext" ]);

This creates a new parser object with a text event handler subroutine that receives the original text with general entities decoded.

Edit:

use HTML::Parser;
use LWP::Simple;
my $html = get "http://perltraining.stonehenge.com";
HTML::Parser->new(text_h => [\my @accum, "text"])->parse($html);
print map $_->[0], @accum;

Another

#!/usr/bin/perl -w
use strict;
use HTML::Parser;
my $text;
my $p = HTML::Parser->new(text_h => [ sub {$text .= shift}, 
                                     'dtext']);
$p->parse_file('test.html');
print $text;

Which, when used on a file like this:

<html>
<head>
<title>Test</title>
</head>
<body>
<h1>Test Stuff</h1>
<p>This is a test</p>
<ul>
<li>this</li>
<li>is a</li>
<li>list</li>
</ul>
</body>
</html>

produces the following output:

Test


Test Stuff
This is a test

this
is a
list

Does that help?

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133