cosmicperl has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,
As it turned out what I originally thought was the problem wasn't at all, and instead it was a simple mistake in a foreach loop. So I thought best to re-title this thread and turn it into an explanation encase anyone else makes the same mistake. My original post is at the bottom of this thread.

I'd cobbled together a simple template system that involved a hash and a regexp (should have used HTML::Template instead).
foreach my $key (%HASH) { $html =~ s/<!--$key-->/$HASH{$key}/gis; }#foreach print $html;
Easy I thought... Worked great at first, but suddenly I started getting errors such as:-
Unmatched ) in regex; marked by <-- HERE in..."
?? I though regexp only checked for matching brackets in the first part, which in this case is the $key which would only contain predefined things such as 'name', 'email', etc.

I posted here to find out why I was getting this error with a quick example. To my horror my example of the problem didn't have any problems... As it turned out I'd made a really simple mistake, lots of you have probably already noticed it:-
foreach my $key (%HASH) {
Should be:-
foreach my $key (keys %HASH) {
None of the keys had any brackets in, but the values did. Without specifying 'keys' I was getting both keys and values in $key. Rookie mistake, but after 9yrs still bit me on the ass.


Lyle



---Original Message follows---


  I'm having a little trouble with a quick regexp I put together. Basically users can input text that gets put into a web page. The text is swapped in with a simple regexp:-
my $userinput = 'Some text'; $html =~ s/<!--usertext-->/$userinput/is;

Now this works fine most of the time, until the user decides to input something like a smiley, such as:-
my $userinput = 'Some text :)'; $html =~ s/<!--usertext-->/$userinput/is;
Results in the error "Unmatched ) in regex; marked by <-- HERE in...". I don't quite understand why this is happening as I only thought brackets did something in the first part of the regexp.

Would very much appreciate any help as to what I am doing wrong.


Lyle


Update: I've just checked my examples on the command prompt and they worked for me as well. I'm looking closer as to why this is happening and I'll update you.

Replies are listed 'Best First'.
Re: Regexp swapping with unmatched brackets
by moritz (Cardinal) on Sep 23, 2008 at 14:50 UTC
    Works fine here:
    my $html = 'this is the user text: «<!--usertext-->»'; my $userinput = 'Some text :)'; $html =~ s/<!--usertext-->/$userinput/is; print $html, $/; __END__ this is the user text: «Some text :)»

    What you describe usually happens when you try it the other way round, ie treat $userinput as a regex. Then you should use quotemeta or m/\Q$userinput\E/ instead.

    Also consider using a real template system like HTML::Template or HTML::Template::Compiled, and escaping "evil" characters like <>"& in your user input before you send it back to the browser. Both template engines also offer the default_escape option which you can set to "HTML", then you never worry about such things again.

Re: Regexp swapping with unmatched brackets
by wol (Hermit) on Sep 23, 2008 at 15:05 UTC
    Works for me too. (Perl 5.8 on WinXP)

    Can you let us know what version of Perl and which OS you're running on (just in case).

    Also, can you post the rest of the text that comes with the error, as that might indicate where "HERE" actually is.

    --
    .sig : File not found.

Re: Regexp swapping with unmatched brackets
by ysth (Canon) on Sep 23, 2008 at 15:41 UTC
Re: foreach loop mistake with key as regexp
by Anonymous Monk on Sep 24, 2008 at 06:36 UTC
    Some value of $key has a ")" that isn't preceded by a "(", whats the confusion?