tags:

views:

28

answers:

1

I'm trying to get the tags that occur immediately after a particular div tag. For e.g., I have html code

<div id="example">
     <h2>Example</h2>
     <p>Hello !World</p>
</div>

I'm doing the following,

while ( $tag = $stream->get_tag('div') ) {
    if( $tag->[1]{id} eq 'Example' ) {
        $tag = $stream->get_tag;
        $tag = $stream->get_tag;
        if ( $tag->[0] eq 'div' ) {
        ...
        }
    }
}

But this throws the error Can't use string ("</h2>") as a HASH ref while "strict refs" in use

It works fine if I say $tag = $stream->get_tag('h2'); $tag = $stream->get_tag('p');

But I can't have that because I need to get the immediate two tags and verify if they are what i expect them to be.

+1  A: 

It would be easier to tell if you posted a runnable example program, but it looks like the problem is you didn't realize that get_tag returns both start and end tags. End tags don't have attributes. Start tags are returned as [$tag, $attr, $attrseq, $text], and end tags are returned as ["/$tag", $text].

cjm
I see. so in this case get_tag returns h2,/h2,div etc. yes?
Rajesh
@Rajesh, Yes. And if you try to do `$tag->[1]{id}` on an end tag, you'll get that error, because `$tag->[1]` is not a hash ref. You have to check `$tag->[0]` first to see whether you have a start or end tag whenever you use `get_tag` with either no tag list, or a tag list that includes end tags.
cjm