WhiteBird has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Loop behavior with HTML::TokeParser::Simple
by jsprat (Curate) on Dec 20, 2003 at 02:20 UTC | |
by WhiteBird (Hermit) on Dec 20, 2003 at 02:52 UTC | |
by Roger (Parson) on Dec 20, 2003 at 07:02 UTC | |
by WhiteBird (Hermit) on Dec 20, 2003 at 22:50 UTC |