Question

I'm trying to do a multithread client for a chat application. The are 2 threads: keyboard reading (reads input from the keyboard and sends it through a socket) and server reading (reads lines from the server and prints them to the screen). So, this is what I do to create the keyboard reading one:

my $threadKeyboard = threads->create(\&readKeyboard, $socket);
$threadKeyboard->join();


sub readKeyboard {
    my $socket = @_;
    my $keydata;

    do{
        $keydata=<STDIN>;
        print $socket "$keydata\n";
    } while($data ne "bye\n");
}

But, when I try to run it I get this:

Thread 1 terminated abnormally: Can't use string ("1") as a symbol ref 
while "strict refs" in use at client.pl line 41, <STDIN> line 1.

Line 41 is:

$keydata = <stdin>;

I guess the problem is that I'm not giving the socket properly to the function, but I don't know how to solve it :( Any suggestions? Thanks in advance!

Was it helpful?

Solution

Try any of these,

my( $socket ) = @_ ; 


my $socket = $_[0] ; 


my $socket = shift ; 

When you assign an Array to a scalar, you don't get what you expect.

http://perlmaven.com/scalar-and-list-context-in-perl

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top