http://qs1969.pair.com?node_id=671666


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.