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

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.

Replies are listed 'Best First'.
Re^4: perlmonks downloadable code blocks
by debiandude (Scribe) on Apr 11, 2008 at 09:19 UTC
    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.