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

I'm using SVGs as report templates for an program I'm working on. In order to make the system as simple as possible for the designer, I'm using simple string substitution on the SVG to replace field names with HTML::Template tags. This works great for the basic stuff. One thing I need to be able to do, though, is identify specific embeded JPEG images in the SVG and replace them with HTML::Template tags as well. I'm trying to do this using a regular expression to match the correct base64 image data, but it isn't working. Here's what I've tried:
my $base64_image = encode_base64($buf); my $text = do { local( @ARGV, $/ ) = "test_template.svg" ; <> } ; $text =~ s/##([a-zA-Z0-9_\.]+)##/<TMPL_VAR name="$1">/g; my $photo_count = 1; while ($text =~ s/\Q$base64_image/<TMPL_VAR name="photo_$photo_count"> +/s){ $photo_count++; }

I've verified that $base64_image is the same as what is found in the SVG. What am I doing wrong? Incorrect modifiers on the regular expression?

Thanks in advance.

Replies are listed 'Best First'.
Re: Substituting base64 encoded text
by oko1 (Deacon) on Jan 09, 2011 at 01:01 UTC

    It might be worth your while to check the content of $base64_image against the actual JPEG string in the SVG file, just in case there's something you missed (random scenario: it might be prefixed/postfixed with some string in the SVG).

    my @jpg = split //, $extracted_jpg_string; my @ref = split //, $base64_image; for (0 .. ($#jpg > $#ref ? $#jpg : $#ref)){ print "Images differ: \$ref[$_]: $ref[$_] | \$jpg[$_]: $jpg[$_]\n +"; }

    Update: I somehow managed to miss that you'd already verified it. As penance :), I hunted up a random SVG on my system that contained a JPG, ran base64_decode() on the JPG string inside it, and saved the result as a JPG file. Although it does indeed start with a JPG header, none of my image viewers were able to open it, indicating that it's probably not just a simple conversion.

    Also, I'd move that first substitution to a position below the JPG replacement. If you happen to have a '##whatever##' in the JPG string, that's going to cause a problem.

    -- 
    Education is not the filling of a pail, but the lighting of a fire.
     -- W. B. Yeats
Re: Substituting base64 encoded text
by zwon (Abbot) on Jan 09, 2011 at 04:37 UTC

    When you verified that $base64_image is the same as in SVG, are you checked that they have the same line endings?