Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
9.7 KiB

  1. # This is a .pm just to (try to) make some CPAN document converters
  2. # convert it happily as part of the dist's documentation tree.
  3. package HTML::Element::traverse;
  4. # Time-stamp: "2001-03-10 21:32:23 MST"
  5. use HTML::Element ();
  6. $VERSION = $VERSION = $HTML::Element::VERSION;
  7. 1;
  8. __END__
  9. =head1 NAME
  10. HTML::Element traverse - discussion of HTML::Element's traverse method
  11. =head1 SYNOPSIS
  12. # $element->traverse is unnecessary and obscure.
  13. # Don't use it in new code.
  14. =head1 DESCRIPTION
  15. C<HTML::Element> provides a method C<traverse> that traverses the tree
  16. and calls user-specified callbacks for each node, in pre- or
  17. post-order. However, use of the method is quite superfluous: if you
  18. want to recursively visit every node in the tree, it's almost always
  19. simpler to write a subroutine does just that, than it is to bundle up
  20. the pre- and/or post-order code in callbacks for the C<traverse>
  21. method.
  22. =head1 EXAMPLES
  23. Suppose you want to traverse at/under a node $tree and give elements
  24. an 'id' attribute unless they already have one.
  25. You can use the C<traverse> method:
  26. {
  27. my $counter = 'x0000';
  28. $start_node->traverse(
  29. [ # Callbacks;
  30. # pre-order callback:
  31. sub {
  32. my $x = $_[0];
  33. $x->attr('id', $counter++) unless defined $x->attr('id');
  34. return HTML::Element::OK; # keep traversing
  35. },
  36. # post-order callback:
  37. undef
  38. ],
  39. 1, # don't call the callbacks for text nodes
  40. );
  41. }
  42. or you can just be simple and clear (and not have to understand the
  43. calling format for C<traverse>) by writing a sub that traverses the
  44. tree by just calling itself:
  45. {
  46. my $counter = 'x0000';
  47. sub give_id {
  48. my $x = $_[0];
  49. $x->attr('id', $counter++) unless defined $x->attr('id');
  50. foreach my $c ($x->content_list) {
  51. give_id($c) if ref $c; # ignore text nodes
  52. }
  53. };
  54. give_id($start_node);
  55. }
  56. See, isn't that nice and clear?
  57. But, if you really need to know:
  58. =head1 THE TRAVERSE METHOD
  59. The C<traverse()> method is a general object-method for traversing a
  60. tree or subtree and calling user-specified callbacks. It accepts the
  61. following syntaxes:
  62. =over
  63. =item $h->traverse(\&callback)
  64. =item or $h->traverse(\&callback, $ignore_text)
  65. =item or $h->traverse( [\&pre_callback,\&post_callback] , $ignore_text)
  66. =back
  67. These all mean to traverse the element and all of its children. That
  68. is, this method starts at node $h, "pre-order visits" $h, traverses its
  69. children, and then will "post-order visit" $h. "Visiting" means that
  70. the callback routine is called, with these arguments:
  71. $_[0] : the node (element or text segment),
  72. $_[1] : a startflag, and
  73. $_[2] : the depth
  74. If the $ignore_text parameter is given and true, then the pre-order
  75. call I<will not> be happen for text content.
  76. The startflag is 1 when we enter a node (i.e., in pre-order calls) and
  77. 0 when we leave the node (in post-order calls).
  78. Note, however, that post-order calls don't happen for nodes that are
  79. text segments or are elements that are prototypically empty (like "br",
  80. "hr", etc.).
  81. If we visit text nodes (i.e., unless $ignore_text is given and true),
  82. then when text nodes are visited, we will also pass two extra
  83. arguments to the callback:
  84. $_[3] : the element that's the parent
  85. of this text node
  86. $_[4] : the index of this text node
  87. in its parent's content list
  88. Note that you can specify that the pre-order routine can
  89. be a different routine from the post-order one:
  90. $h->traverse( [\&pre_callback,\&post_callback], ...);
  91. You can also specify that no post-order calls are to be made,
  92. by providing a false value as the post-order routine:
  93. $h->traverse([ \&pre_callback,0 ], ...);
  94. And similarly for suppressing pre-order callbacks:
  95. $h->traverse([ 0,\&post_callback ], ...);
  96. Note that these two syntaxes specify the same operation:
  97. $h->traverse([\&foo,\&foo], ...);
  98. $h->traverse( \&foo , ...);
  99. The return values from calls to your pre- or post-order
  100. routines are significant, and are used to control recursion
  101. into the tree.
  102. These are the values you can return, listed in descending order
  103. of my estimation of their usefulness:
  104. =over
  105. =item HTML::Element::OK, 1, or any other true value
  106. ...to keep on traversing.
  107. Note that C<HTML::Element::OK> et
  108. al are constants. So if you're running under C<use strict>
  109. (as I hope you are), and you say:
  110. C<return HTML::Element::PRUEN>
  111. the compiler will flag this as an error (an unallowable
  112. bareword, specifically), whereas if you spell PRUNE correctly,
  113. the compiler will not complain.
  114. =item undef, 0, '0', '', or HTML::Element::PRUNE
  115. ...to block traversing under the current element's content.
  116. (This is ignored if received from a post-order callback,
  117. since by then the recursion has already happened.)
  118. If this is returned by a pre-order callback, no
  119. post-order callback for the current node will happen.
  120. (Recall that if your callback exits with just C<return;>,
  121. it is returning undef -- at least in scalar context, and
  122. C<traverse> always calls your callbacks in scalar context.)
  123. =item HTML::Element::ABORT
  124. ...to abort the whole traversal immediately.
  125. This is often useful when you're looking for just the first
  126. node in the tree that meets some criterion of yours.
  127. =item HTML::Element::PRUNE_UP
  128. ...to abort continued traversal into this node and its parent
  129. node. No post-order callback for the current or parent
  130. node will happen.
  131. =item HTML::Element::PRUNE_SOFTLY
  132. Like PRUNE, except that the post-order call for the current
  133. node is not blocked.
  134. =back
  135. Almost every task to do with extracting information from a tree can be
  136. expressed in terms of traverse operations (usually in only one pass,
  137. and usually paying attention to only pre-order, or to only
  138. post-order), or operations based on traversing. (In fact, many of the
  139. other methods in this class are basically calls to traverse() with
  140. particular arguments.)
  141. The source code for HTML::Element and HTML::TreeBuilder contain
  142. several examples of the use of the "traverse" method to gather
  143. information about the content of trees and subtrees.
  144. (Note: you should not change the structure of a tree I<while> you are
  145. traversing it.)
  146. [End of documentation for the C<traverse()> method]
  147. =head2 Traversing with Recursive Anonymous Routines
  148. Now, if you've been reading
  149. I<Structure and Interpretation of Computer Programs> too much, maybe
  150. you even want a recursive lambda. Go ahead:
  151. {
  152. my $counter = 'x0000';
  153. my $give_id;
  154. $give_id = sub {
  155. my $x = $_[0];
  156. $x->attr('id', $counter++) unless defined $x->attr('id');
  157. foreach my $c ($x->content_list) {
  158. $give_id->($c) if ref $c; # ignore text nodes
  159. }
  160. };
  161. $give_id->($start_node);
  162. undef $give_id;
  163. }
  164. It's a bit nutty, and it's I<still> more concise than a call to the
  165. C<traverse> method!
  166. It is left as an exercise to the reader to figure out how to do the
  167. same thing without using a C<$give_id> symbol at all.
  168. It is also left as an exercise to the reader to figure out why I
  169. undefine C<$give_id>, above; and why I could achieved the same effect
  170. with any of:
  171. $give_id = 'I like pie!';
  172. # or...
  173. $give_id = [];
  174. # or even;
  175. $give_id = sub { print "Mmmm pie!\n" };
  176. But not:
  177. $give_id = sub { print "I'm $give_id and I like pie!\n" };
  178. # nor...
  179. $give_id = \$give_id;
  180. # nor...
  181. $give_id = { 'pie' => \$give_id, 'mode' => 'a la' };
  182. =head2 Doing Recursive Things Iteratively
  183. Note that you may at times see an iterative implementation of
  184. pre-order traversal, like so:
  185. {
  186. my @to_do = ($tree); # start-node
  187. while(@to_do) {
  188. my $this = shift @to_do;
  189. # "Visit" the node:
  190. $this->attr('id', $counter++)
  191. unless defined $this->attr('id');
  192. unshift @to_do, grep ref $_, $this->content_list;
  193. # Put children on the stack -- they'll be visited next
  194. }
  195. }
  196. This can I<under certain circumstances> be more efficient than just a
  197. normal recursive routine, but at the cost of being rather obscure. It
  198. gains efficiency by avoiding the overhead of function-calling, but
  199. since there are several method dispatches however you do it (to
  200. C<attr> and C<content_list>), the overhead for a simple function call
  201. is insignificant.
  202. =head2 Pruning and Whatnot
  203. The C<traverse> method does have the fairly neat features of
  204. the C<ABORT>, C<PRUNE_UP> and C<PRUNE_SOFTLY> signals. None of these
  205. can be implemented I<totally> straightforwardly with recursive
  206. routines, but it is quite possible. C<ABORT>-like behavior can be
  207. implemented either with using non-local returning with C<eval>/C<die>:
  208. my $died_on; # if you need to know where...
  209. sub thing {
  210. ... visits $_[0]...
  211. ... maybe set $died_on to $_[0] and die "ABORT_TRAV" ...
  212. ... else call thing($child) for each child...
  213. ...any post-order visiting $_[0]...
  214. }
  215. eval { thing($node) };
  216. if($@) {
  217. if($@ =~ m<^ABORT_TRAV>) {
  218. ...it died (aborted) on $died_on...
  219. } else {
  220. die $@; # some REAL error happened
  221. }
  222. }
  223. or you can just do it with flags:
  224. my($abort_flag, $died_on);
  225. sub thing {
  226. ... visits $_[0]...
  227. ... maybe set $abort_flag = 1; $died_on = $_[0]; return;
  228. foreach my $c ($_[0]->content_list) {
  229. thing($c);
  230. return if $abort_flag;
  231. }
  232. ...any post-order visiting $_[0]...
  233. return;
  234. }
  235. $abort_flag = $died_on = undef;
  236. thing($node);
  237. ...if defined $abort_flag, it died on $died_on
  238. =head1 SEE ALSO
  239. L<HTML::Element>
  240. =head1 COPYRIGHT
  241. Copyright 2000,2001 Sean M. Burke
  242. =head1 AUTHOR
  243. Sean M. Burke, E<lt>sburke@cpan.orgE<gt>
  244. =cut