in reply to Adding counters for text replacement

Are you sure this will be valid HTML? You may try to write a regexp for this (at least I don't know how can it be archieved using HTML::Entity/HTML::TreeBuilder), like this:
$ cat t1.pl #!/usr/bin/perl -p BEGIN{$i=0} s|<p>((?<!</p>).*)</p>|'<bpt i="'.++$i.'" x="'.$i.'"><p></bpt>'.$1.'<e +pt i="'.$i.'"></p></ept>'|egs; $ seq 1 10 | while read; do echo '<p>text<xczcx>texxxxx</p>'; done | p +erl t1.pl <bpt i="1" x="1"><p></bpt>text<xczcx>texxxxx<ept i="1"></p></ept> <bpt i="2" x="2"><p></bpt>text<xczcx>texxxxx<ept i="2"></p></ept> <bpt i="3" x="3"><p></bpt>text<xczcx>texxxxx<ept i="3"></p></ept> <bpt i="4" x="4"><p></bpt>text<xczcx>texxxxx<ept i="4"></p></ept> <bpt i="5" x="5"><p></bpt>text<xczcx>texxxxx<ept i="5"></p></ept> <bpt i="6" x="6"><p></bpt>text<xczcx>texxxxx<ept i="6"></p></ept> <bpt i="7" x="7"><p></bpt>text<xczcx>texxxxx<ept i="7"></p></ept> <bpt i="8" x="8"><p></bpt>text<xczcx>texxxxx<ept i="8"></p></ept> <bpt i="9" x="9"><p></bpt>text<xczcx>texxxxx<ept i="9"></p></ept> <bpt i="10" x="10"><p></bpt>text<xczcx>texxxxx<ept i="10"></p></ept> $
but this is wrong way (for example, it will break on nested <p>...<p>...</p>...</p> tags). Why do you need such a strange tag combination?
Sorry if my advice was wrong.