I'm working on an IRC server written in Perl (not for any serious use, but just as a pet project of mine) and I'm running into a little trouble.

I store nicknames as keys in a hash named %fhs. The value of each of those entries is the filehandle for the nickname in question, so when I need to send Bob89 data, I can just grab his filehandle from $fhs{Bob89}. The problem is, if a user wants to send information to Bob89, but sends it to bOb89, $fhs{bOb89} is checked for the filehandle and returns nothing. In order to get around this, I've had to do a case insensitive search and traverse the entire hash until I get a match with this code:
sub nick_ison { my ($nick) = @_; foreach my $nick_check (keys %fhs) { print "NICK: $nick\n"; print "NICK CHECK: $nick_check\n"; if ($nick =~ /^($nick_check)$/i) { return $nick_check } } return; }
That way I can use the returned nickname in the correct case and everything works just fine. The problem is, this doesn't seem very efficient and I was wondering if there was a quicker way to do it.

Also, with this code, I run into problems with nicknames that contain characters used as regex metacharacters and quantifiers.

For example, when Bob89^ is an online nick, nick_ison('Bob89^') prints Bob89^ as both the value of $nick and $nick_check, but the regex doesn't match it and I haven't been able to figure out a way to escape all the characters and have it still work. I tried escaping all the characters by just taking them and putting \ in front of them, but it started getting complicated when someone changed their nick to Bob89\^ and it erroneously matched Bob89^.

So basically, is there a quicker way to do this and if not, what would be the best way of taking the nickname and escaping all the regex characters before using it to check for the filehandle?

I'm really looking for a quicker way to write this subroutine, but if you have a way to improve the code I have, it'd be much appreciated.

In reply to Problem With Perl IRC Server by Veachian64

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.