Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Looking for a faster method

by Drgan (Beadle)
on Dec 28, 2006 at 15:11 UTC ( #592060=perlquestion: print w/replies, xml ) Need Help??

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

Update 1: Corrected formating in post.
Update 2: Updated a for-each loop to have documentation explaining what it does. Also changed it to call a sub-routine rather than do the work manually.

Fellow monks,

I have recently decided to pick up on an older project of mine since I now have to do some more fun stuff with Perl. In the midst of looking over the code, I've found a spot where I'm not sure I should be calling RegEx immediately. Possibly, I should send the string to a sub-routine (callback) and then run the RegEx through the sub-routine? Please provide any incite that you may have.


Side note: I know I may be better serviced using Net::IRC but I have chosen to dabble with IO:Socket instead.

my $sock = IO::Socket::INET->new( Proto=>"$config->{protocol}", PeerAddr=>"$config->{server}", PeerPort=>"$config->{port}", ) or die "Cannot Connect to Server"; $sock->send("NICK $config->{nick}\n"); $sock->send("USER " . $config->{ident} x 3 . ":$config->{ircname}\n"); $sock->autoflush(1); my %userAuthenticatedList; # Authenticated Users and Host +s my %channels; # Stored List of channels kick +ed from my $connected; # Prevent constant "log-on" of + Bot my $salt = $config->{salt}; # md5 key for encryption my $incomingMessage; # Handle incoming signals/events from +server. while (1) { $sock->recv($incomingMessage, 1024); if ($incomingMessage=~/PING :(.*)/i) { $sock->send("PONG :$1\n"); &on_connect_join() if (!$connected); # The portion determines if the channel is # is a valid entry in the channels channels # hash/array before joining the channel. foreach (keys %channels) { &join_chan($_) if ($channels{$_}); } } # Syntax from server is as follows # :NickName!User@host PRIVMSG (individual/#channel) :text if ($incomingMessage=~/:(.*)!(.*)@(.*) PRIVMSG (.*) :$config-> +{CMDCHAR}(.*)/i) { my $userName = "$1"; my $userHost = $1 . "!" . $2 . "@" . $3; $_ = "$5"; my @args = split " ", $_; my $commands = shift(@args); my $args = "@args"; print "Someting was received\n"; print "$userName $userHost : $commands $args\n"; if ($commands eq "identify") { # Check for co +mmand being called. $args = md5_base64($args, $salt); print "Sent to userLogin\n" if userLogin($user +Name, $userHost, $args); } elsif ($userHost eq $userAuthenticatedList{$userName +}) { for (my $i=0; $pFuncsA[$i]->{command}; $i++) { if ($pFuncsA[$i]->{command} eq $comman +ds) { $pFuncsA[$i]->{func}($args); } } } }
"I have said, Ye are gods; and all of you are children of the most High." - Psalms 82:6

Replies are listed 'Best First'.
Re: Looking for a faster method
by rodion (Chaplain) on Dec 28, 2006 at 16:13 UTC
    Your inner for loop
    for (my $i=0; $pFuncsA[$i]->{command}; $i++) { ...
    will stop at the first $pFuncsA[$i] that doesn't have a 'command' entry in its hash. If there are entries with commands at higher indexes of '$i', then they will be skipped. I can't tell if this will be a problem in this case, since it's the only place $pFuncsA shows up in this code fragment.

    Also, to add to the bug alpha identified, you pobably meant to type

    for (keys %channels) {
    It's easier to forget the "keys" built-in function when you type 'foreach' as a synonym for 'for'; that's probably one reason that foreach is less favored these days. (It's going away in Perl6.)
Re: Looking for a faster method
by alpha (Scribe) on Dec 28, 2006 at 15:25 UTC
    This is really confusing:
    foreach (%channels) { $sock->send("JOIN $_\n") if ($channels{$_}); }
    also, $userHost could be probably gained as a result of one match, i.e. !(.*)@(.*) => !([^\s]+)
Re: Looking for a faster method
by ysth (Canon) on Dec 29, 2006 at 03:16 UTC
    You ask a pretty specific question about a regex, but don't identify which regex you are asking about?
      I am trying to determine the best method for handling incoming text/data from the socket. Possibly, that handling the incoming data and ultimately determining what routine to call during the main portion of the program could be causing some sort of delay. Also, would it be better served to split the incoming line before running RegEx over it?
      "I have said, Ye are gods; and all of you are children of the most High." - Psalms 82:6

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://592060]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2020-12-03 11:05 GMT
Find Nodes?
    Voting Booth?
    How often do you use taint mode?

    Results (55 votes). Check out past polls.