So this code works perfectly until I come across a blank tag. Any suggestions how to handle a situation where the tag exists, but has no text? Currently when the script comes across this situation it dies instead of inserting the new text. Here's the code with sample html:

Code:

#Find the indext.txt config files #and edit the index.htm files one dir up from the config file use warnings; use HTML::TreeBuilder; use File::Find; my $dir = $ARGV[0]; my $html_file; my $cfg_path; my $index_pref; find(\&file_finds, $dir); sub file_finds{ if ($_=/index.htm/){ $html_file = $File::Find::name; $cfg_path = $File::Find::dir."/index/index.txt"; &config; &edit; } } sub config{ open (CF, "$cfg_path") or die ("Can't open $cfg_path $!"); while (<CF>) { chomp; # no newline s/#.*//; # no comments s/^\s+//; # no leading white s/\s+$//; # no trailing white next unless length; # anything left? my ($var, $value) = split(/\s*=\s*/, $_, 2); $index_pref{$var} = $value; } close CF; } sub edit{ my $root = HTML::TreeBuilder->new_from_file($html_file) or die qq{cant build tree\n}; my $class1 = $root->look_down( _tag => q{div}, class => q{class1}, ); die qq{client not found\n$html_file\n} unless $class1; my $class2 = $root->look_down( _tag => q{div}, class => q{class2}, ); die qq{class2 not found\n$html_file\n} unless $class2; my $class3 = $root->look_down( _tag => q{div}, class => q{class3}, ); die qq{class3 not found\n$html_file\n} unless $class3; my $rep_class1; for my $item_r ($class1->content_refs_list) { next if ref ${$item_r}; ${$item_r} = $index_pref{"class1"}; $rep_class1++; } die qq{Class1 not replaced\n$html_file\n} unless $rep_class1; my $rep_class2; for my $item_r ($class2->content_refs_list) { next if ref ${$item_r}; ${$item_r} = $index_pref{"class2"}; $rep_class2++; } die qq{Class2 not replaced\n$html_file\n} unless $rep_class2; my $rep_class3; for my $item_r ($class3->content_refs_list) { next if ref ${$item_r}; ${$item_r} = $index_pref{"class3"}; $rep_class3++; } die qq{Class3 not replaced\n$html_file\n} unless $rep_class3; my $html = $root->as_HTML(undef, qq{ }, {}); open (FH, ">$html_file") or die $!; print FH $html; close FH; }

HTML: <!--Class 3 has no text--> <div align="center" class="class1">something</div> <div align="center" class="class2">something else</div> <div align="center" class="class3"></div>


In reply to Re^2: Editing HTML files by spivey49
in thread Editing HTML files by spivey49

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.