Esteemed Monks:

I'm having a difficulty with a snippet of code. The larger context requires that I replace one image link embedded deep inside each of over 100 HTML recipe files. The current link in each document is a relative link and each image has a different, unknown ID number as such:
<img src='../../images/dbimage.asp?ID=758'>

The replacement string is constructed of a title gleaned from the title of the recipe and munged into a proper HTML format. I've built code that gets the title and gets to the image tag. I just can't seem to get the regex replacement to work correctly. The relevent snippet is this:

while ( $token=$p->get_token() ) { if ($token->is_start_tag('img') ) { my $src = $token->return_attr->{src}; print "SRC IS $src\n"; #For Debugging $src =~ s/$src/$newsrc/; print "SRC NOW: $src\n"; #For Debugging $token->set_attr('src', $src); }

There are two other image references after the one I'm interested in and the looping picks through all of them. (At this point, that's not a problem.) When I run the script on a file, my printed debugging output is this:

got filetitle: ApplePecanBreadStuffing SRC IS ../../images/dbimage.asp?ID=751 SRC NOW: ../../images/dbimage.asp?ID=751 SRC IS ../../images/spacer.gif SRC NOW: 'ApplePecanBreadStuffing.jpg' SRC IS ../../images/spacer.gif SRC NOW: 'ApplePecanBreadStuffing.jpg'

Why is the first tag missed in the replacement step and the following two work correctly? I keep looking at it and I suspect it's something obvious in the structure of the code, but I am out of ideas. Help?


In reply to Loop behavior with HTML::TokeParser::Simple by WhiteBird

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.