in reply to replacing variable in regex - probs with special chars?

To solve your problem, use \Q and \E. (Read the Camel book to learn what they do.)

Personally, I'd use a templating system like HTML::Template or Mason vs. rolling your own. *shrugs* I don't like re-inventing wheels that do more than I'll ever make them do.

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

  • Comment on Re: replacing variable in regex - probs with special chars?

Replies are listed 'Best First'.
Re: Re: replacing variable in regex - probs with special chars?
by Emanuel (Pilgrim) on Jan 30, 2003 at 18:47 UTC
    unfortunatly the whole thing isn't used for webpages or any of the like. HTML::Template and Mason are not suited for the whole purpose of what this is doing (it's just an excerpt out of the whole thing).

    I'm going to dig out my copy of the camel book and start reading, thanks for the direction!

    Emanuel

      Yes, you want:

      s/\Q$complete\E/...
      because $complete has metacharacters in it. Pretty much any time you want to stick a variable in a regex, you want to use \Q...\E. The only exception is if the variable is supposed to contain a regex itself, but that can get dangerous (arbitrary perl code in regexes, etc).

      Also, maybe Template::Toolkit would be of use. It just does general text templates, making no assumptions about web apps or HTML.