in reply to Regex error when [] occurs in file..

if ($temp =~ /#/){ $temp =~ s/#$'//; }

That's what's causing the problem: $' can contain arbitrary data, but you try to treat it as a regex.

The "good" solution is to use this regex instead: $temp =~ s/#.*$//;

In general you can also quote interpolated variables, then they are treated as text, not as regexes:

my $varaible = '[a-z]'; m/\Q$variable\E/ # matches literal [a-z], not a character class.

If you're not inside a regex, quotemeta does the same job.

Replies are listed 'Best First'.
Re^2: Regex error when [] occurs in file..
by CountZero (Bishop) on Mar 03, 2008 at 17:12 UTC
    If you would happen to have the '#' character inside a (single or double) quoted string in your config file (I don't know if the specs for your config-file even allow this) then the s/#.*$// regex will cause you trouble as it will delete all of the string starting with the '#' character. That is probably not what you want.

    It is not easy to take care of this: not even Regexp::Common gets it right.


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James