in reply to Re: perlmonks downloadable code blocks
in thread perlmonks downloadable code blocks

That's pretty snazzy. And I guess I could modify the code inline as well with something like this:
my $match = 0; my $post_body = ...from db...; $match++ while $post_body =~ s/<c>(.*?)<\/c>/<a href='download?part=$m +atch'>Download Me<\/a><pre>$1<\/pre>/i;

Replies are listed 'Best First'.
Re^3: perlmonks downloadable code blocks
by ikegami (Patriarch) on Apr 11, 2008 at 07:55 UTC
    PM does a bit more than that — it escapes "<", ">" and "&", at least — but that's the idea.

    By the way, I'd add the "g" modifier to your s///. There's no reason to search from the start of the string every pass.

      Well if I use the global "/g" modifier it will replace all of my tags at once and $i will never be incremented.

        True, unless you get rid of the while entirely. The first paragraph of s/// documentation reads: (Emphasis mine)

        Searches a string for a pattern, and if found, replaces that pattern with the replacement text and returns the number of substitutions made.

        So you get

        my $post_body = ...from db...; my $match = s/.../.../ig;

        By the way, I don't think $match should be singular. $matches is better but not nearly as descriptive as something like $num_blks.