While statement in perl

Discussion in 'Perl' started by jjliu, Oct 11, 2003.

  1. jjliu

    jjliu Guest

    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};
    }
    }
     
    jjliu, Oct 11, 2003
    #1
    1. Advertising

  2. jjliu

    Guest

    "jjliu" <> wrote in message news:<v3Jhb.23605$>...
    > 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.
     
    , Oct 14, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    807
  2. Jay McGavren
    Replies:
    11
    Views:
    1,139
    Alan Krueger
    Jan 16, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,091
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Xah Lee
    Replies:
    7
    Views:
    397
    Charlton Wilbur
    Jan 13, 2005
  5. Xah Lee
    Replies:
    6
    Views:
    97
    Charlton Wilbur
    Jan 13, 2005
Loading...

Share This Page