Clear questions and runnable code
get the best and fastest answer
Inspecting each element in a tree, specifically HTML::Treeby hulot (Initiate)
|on Jul 24, 2012 at 11:36 UTC||Need Help??|
hulot has asked for the
wisdom of the Perl Monks concerning the following question:
I've only used Perl for 2 weeks, so apologies for my likely ignorance.
I want to write a script that downloads a webpage and renders part of that page in wiki format. I appreciate that there is a HTML:WikiConverter module, but I would like to implement this myself, partly because I only want to render some elements of the html. I will be using HTML::Tree.
The first step is to build the tree. That appears straightforward:
In pseudo (pseudo) code I wish to look at each element of the page. For each element, if the tag is one I'm interested in, then I wish to take the text of the element and render it to wiki format.
I just don't understand how to loop through all the elements. A discussion in the HTML::Tree documentation suggests a recursive method of accessing all the elements:
But I don't understand this code and can't adapt it.
Once I have a 'loop' method of looking at each element I propose processing them like this:
I will have several elsif statements doing something similar with other tags.
My question then is how can write a loop that allows me to look at each element in the tree. (The traverse method is deprecated.)