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

I'm working on a small script at the moment the whole of which can be found here. The problem is that I'm trying to pass incoming messages from the clients to a sub in the server (the script):
while ($client) { sysread($client, $incoming, 1024); process_incoming(split /\n|\r/, $incoming); undef $incoming; sleep 1; }
But the problem is now I'm reciveing warnings from the sub itself: Use of uninitialized value in pattern match (m//) at ./monkcb line 215.
sub process_incoming { my (@incoming) = @_; return unless @incoming; for my $incoming (@incoming) { if ($incoming =~ /^JOIN (#\w+)/i) { .........
Of course line 215 is the initial match.

Replies are listed 'Best First'.
Re: Uninitialized array woes.
by Zaxo (Archbishop) on Aug 26, 2002 at 04:47 UTC

    You should check the return from sysread. If not defined you have a $! to act on ( you don't want to die on EAGAIN). It returns zero on eof or when zero bytes are read.

    I don't understand what you mean by while ($client) {...} where $client looks like a filehandle on the following line. If you mean to say while (<$client>) {...} you shouldn't mix standard I/O with sysread, and the newline split is unneeded, as well.

    If you have more than one $client, global the single $incoming is going to bite you.

    After Compline,
    Zaxo

      Hmm I've slashed it down to:
      while (my $recv = <$client>) { process_incoming($recv); }

      But this does not work correctly it randomly disconnects the client for some reason.

      Also it's made for one person to be connected to it, which I will eventually hardcode into it at some point. </code>

Re: Uninitialized array woes.
by tadman (Prior) on Aug 26, 2002 at 09:02 UTC
    If you were using IO::Socket then this might be a bit easier since that class provides built-in read/write routines (part of IO::Handle, of course).

    I'm not sure about these disconnects, but you have to be careful with your buffer. If you cram too much output at the client at once, you can flood it, and this will close the connection. Make sure you send output only when the client is ready to receive. They call this flow-control. You can check if a socket is ready to send, or wait until it is.
Re: Uninitialized array woes.
by Django (Pilgrim) on Aug 26, 2002 at 04:37 UTC
    If the script does what you want, I would simply say
    no warnings 'uninitialized';
    within the closest possible scope.