HTML::HeadParser to print the title

D

Dan Jacobson

/usr/share/doc/libhtml-parser-perl/examples/htitle will print out the
title of an HTML document. How do I use the lighter weight
HTML::HeadParser to do the same? I want to rewrite examples/htitle but
just don't get it.

P.S. wouldn't the example be better if it used the more efficient
tool for that job, HTML::HeadParser?
 
T

Tad McClellan

Dan Jacobson said:
/usr/share/doc/libhtml-parser-perl/examples/htitle


We do not have access to your filesystem, so we don't know what
"example" it is that you are talking about.

But that's OK. We don't need to see the example to answer your question.

will print out the
title of an HTML document. How do I use the lighter weight
HTML::HeadParser to do the same?


By copying the first 4 lines of code displayed when you typed:

perldoc HTML::HeadParser

and modifying them for your situation.

So what do you need us for?


--------------------------------
#!/usr/bin/perl
use strict;
use warnings;

my $text = '
<html>

<head>
<title>Perl.org</title>
<base href="http://www.perl.org/index.shtml">
<link rel="shortcut icon" href="favicon.ico">
</head>

<body bgcolor="#FFFFFF">
<p>this is the body of the document</p>
</body>

</html>
';


# code from the module's documentation

require HTML::HeadParser;
my $p = HTML::HeadParser->new;
$p->parse($text) and print "not finished";

print $p->header('Title'), "\n"; # to access <title>....</title>
 
G

Gisle Aas

Dan Jacobson said:
P.S. wouldn't the example be better if it used the more efficient
tool for that job, HTML::HeadParser?

The 'htitle' example script is actually more efficient than
HTML::HeadParser if there is a <title> tag in the document. It has
much less fat than HTML::HeadParser, but HTML::HeadParser is able to
abort the parsing much earlier on title-less documents.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top