While statement in perl

J

jjliu

Could someone point me out that why the following perl program only print
'title'. I expect the contents in h1, keywords and description to print out
as well. thanks.

JJL


#!/usr/bin/Perl

use LWP::Simple;
use HTML::TokeParser;


my $content = get( "http://www.perl.com" ) or die $!;
my $stream = HTML::TokeParser->new( \$content ) or die $!;

while ( $tag = $stream->get_tag("title") )
{
my $title=$stream->get_trimmed_text;
print "Title: $title\n";
}


while ( $tag = $stream->get_tag("h1") )
{
my $head1=$stream->get_trimmed_text;
print "Heading1: $head1\n";
}

while ( $tag = $stream->get_tag("meta") )
{
if ($tag->[1]{name}=~/keywords/i)
{
print $tag->[1]{content};
}

if ($tag->[1]{name}=~/description/i)
{
print $tag->[1]{content};
}
}
 
N

nobull

jjliu said:
Subject: While statement in perl

Your question has nothing to do with the while statement in Perl.

The fact that you thought it might indicates you have a problem with
partitioning your problems.

Your question is about HTML::TokeParser.
Could someone point me out that why the following perl program only print
'title'.
my $stream = HTML::TokeParser->new( \$content ) or die $!;
while ( $tag = $stream->get_tag("title") )
while ( $tag = $stream->get_tag("h1") )

The HTML::TokeParser object is an iterator. Once it reaches the end
it has reached the end. If you want to go back to the beginning you
need to actively do so (by creating a new iterator). HTML::TokeParser
is not unusual in this respect nor is this perculiar to Perl. This is
normal behaviour for iterator objects in pretty much all languages.

Note the get_tags() method can be used to scan for several tag types
in a single pass.

This newsgroup does not exist (see FAQ). Please do not start threads
here.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top