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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.