Changeset 2019

Show
Ignore:
Timestamp:
10/10/06 03:03:17
Author:
miyagawa
Message:

added walkdown(). Added benchmark script

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • XML-OPML-LibXML/trunk/lib/XML/OPML/LibXML.pm

    r2018 r2019  
    5555      if ($outline->is_container) { 
    5656          my @outline = $outline->children; 
    57           # do some recursive stuff 
     57          # do some recursive stuff, see also walkdown() 
    5858      } else { 
    5959          $outline->type; 
     
    6161          $outline->html_url; 
    6262      } 
     63  } 
     64 
     65  # depth-first walkdown the tree 
     66  $doc->walkdown(\&callback); 
     67  sub callback { 
     68      my $outline = shift; 
     69      # ... 
    6370  } 
    6471 
     
    7077For now, all this module does is just parsing an OPML document. The 
    7178API is very simple and limited to low-level access, yet. 
     79 
     80B<NOTE>: This module is not designed to be a drop-in replacement of 
     81XML::OPML. 
    7282 
    7383=head1 AUTHOR 
  • XML-OPML-LibXML/trunk/lib/XML/OPML/LibXML/Document.pm

    r2017 r2019  
    3232} 
    3333 
     34sub walkdown { 
     35    my $self = shift; 
     36    my($cb)  = @_; 
     37 
     38    my $body = first_node($self->{doc}, 'body') or return; 
     39    my @outline = $body->getChildrenByTagName('outline'); 
     40 
     41    # eval so callback can die to return immediately 
     42    eval { 
     43        for my $node (@outline) { 
     44            $self->_walk($node, $cb); 
     45        } 
     46    }; 
     47} 
     48 
     49sub _walk { 
     50    my($self, $node, $cb) = @_; 
     51 
     52    # some duplicated code to save memory 
     53    my $outline = XML::OPML::LibXML::Outline->new_from_elem($node); 
     54    $cb->($outline); 
     55 
     56    if ($outline->is_container) { 
     57        for my $child ($outline->getChildrenByTagName('outline')) { 
     58            $self->_walk($child, $cb); 
     59        } 
     60    } 
     61} 
     62 
    3463sub AUTOLOAD { 
    3564    my $self = shift;