Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to pull the text from some HTML::Element objects which in this case happen to be elements of type 'td' or table divisions. I can "dump" these elements properly, but for the sake of cleanliness I would prefer to just have the text written in these 'td' objects. E.g.

use HTML::TreeBuilder; my $tree = HTML::TreeBuilder->new; $filename = "websource2.txt"; $tree->parse_file($filename); @tbodies = $tree->find('tbody'); @tds = @tbodies[0]->find('td'); @data = @tds[1..6]; foreach(@data){ $_->dump(); }

... produces the output I expected, but replacing the method call dump() with as_text() or even as_HTML() produces no output or errors. I'm mystified. Any suggestions?

  • Comment on HTML::Element method as_text() not performing as expected on <td> elements
  • Download Code

Replies are listed 'Best First'.
Re: HTML::Element method as_text() not performing as expected on <td> elements
by Anonymous Monk on Aug 31, 2011 at 04:37 UTC
      Wow, good call :) I think I was too tired to realize that difference before, though I had in fact consulted the manual. Thanks!

        Wow, good call :) I think I was too tired to realize that difference before, though I had in fact consulted the manual. Thanks!

        I've been there many times :) mystified is code for erroneous assumption

        Sometimes you know which op/sub you're fuzzy on, so it means a simple perldoc -f split, but sometimes you can't zero in on your unknown, so it means writing a few programs, even if the assumption turns out not to be yours

        Its a good thing perl has features for checking lots of these assumptions, helps me cut my development time in half!

Re: HTML::Element method as_text() not performing as expected on <td> elements
by charlesboyo (Beadle) on Aug 30, 2011 at 22:48 UTC

    Shouldn't that be

    @tds = $tbodies[0]->find('td'); # meaning find the tds in the the first element from the list of tbodys

    Using use strict; and use warnings; would help.