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

my code is,

<ul> <li class="orangeTxt1"><a name="a3" id="a3">question-1</a></li> <li>Answer1</li> </ul> <ul> <li class="orangeTxt1"><a name="a4" id="a4">question-2</a></li> <li>Answer2</li> </ul>

I need output of the form,

<ul> <li class="orangeTxt1"><a name="a3" id="a3">question-1</a></li> <li>Answer1</li> <span class="backTotop"> <a href="#a4">Back to top</a></span> <div class="border"> </div> </ul> <ul> <li class="orangeTxt1"><a name="a4" id="a4">question-2</a></li> <li>Answer2</li> <span class="backTotop"> <a href="#a4">Back to top</a></span> <div class="border"> </div> </ul>

The logic i used using regular expression is,

use strict; use warnings; my $data = do {local $/; <DATA>}; my $tag = 'ul'; my $count = () = $data =~ /<\Q$tag\E(?:\s.*?)?>/g; print "Total number of $tag tags are: " . $count . "\n"; my @tags_to_insert = qq{ <span class="backTotop"> <a href="#a2">Back to top</a></span> <div class="borderGoal"> </div> }; my $insert=~s (<\Q$tag\E(?:\s.*?)?)(\@tags_to_insert\E)gs; __DATA__ Data goes here...

i get error message when i execute. kindly help me out.

Replies are listed 'Best First'.
Re: insert html tags
by moritz (Cardinal) on Jul 18, 2011 at 09:57 UTC
    i get error message when i execute.

    Which one? There are lots of error messages out there, should we guess which one you get?

    Anyway, manipulating HTML document is usually best done with some tools that make it easier. For example with a DOM representation:

    #!/usr/bin/perl use strict; use warnings; use 5.010; use Mojo::DOM; my $dom = Mojo::DOM->new(do { local $/; <DATA> }); $dom->find('ul')->each(sub { $_->append_content('<span class="backTotop"> <a href="#a4">Bac +k to top</a></span>'); }); say $dom->to_xml; __DATA__ <ul> <li class="orangeTxt1"><a name="a3" id="a3">question-1</a></li> <li>Answer1</li> </ul> <ul> <li class="orangeTxt1"><a name="a4" id="a4">question-2</a></li> <li>Answer2</li> </ul>
Re: insert html tags
by JavaFan (Canon) on Jul 18, 2011 at 10:54 UTC
    Seems to me that you're inserting a fixed string before each </ul>. In which case a trivial:
    s{(</ul>)}{$tags_to_insert$1}g;
    should do. Note that I'm assuming @tags_to_insert is a typo, and you actually meant $tags_to_insert.

    Note though that the resulting string doesn't seem to be valid HTML to me.

Re: insert html tags
by Anonymous Monk on Jul 18, 2011 at 11:34 UTC

    I will second moritz’s suggestion.   No matter what CPAN package(s) you use, you do want to use CPAN packages.   You want to parse the existing HTML into a data structure, modify that data structure, then generate the output HTML from that.

    The problem that you will always have with “riding the pony bareback” is creating a successful, generalized solution that will work with every well-formed HTML document that you might throw at it (and, with those that are not).   A whole lot of CPAN implementors have lost hair-follicles building the answers to those questions, so that you might not have to lose so many of yours.

Re: insert html tags
by techtween (Novice) on Jul 18, 2011 at 10:05 UTC
    what you did is right but could i know how to do the same using regular expressions for the same logic(i.e., append two line of tags after every ul tag in a page)