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

use IO::Handle; use Socket; use threads; use IO::Socket::INET; open(pctxt,'./pc.txt')||die "error open $!"; while($read_data = <pctxt>) { chomp; $read_data=~s/\s//g; $read_data=~s/" "//g; thread($read_data); } close(pctxt); sub thread { my $net = shift; my @ip = (1..255); my $port = 12345; my $thr0 = threads->new(\\&scan, "$net@ip[0..63]", $port); my $thr1 = threads->new(\\&scan, "$net@ip[64..127]", $port); my $thr2 = threads->new(\\&scan, "$net@ip[128..191]", $port); my $thr3 = threads->new(\\&scan, "$net@ip[192..254]", $port); $thr0->join(); $thr1->join(); $thr2->join(); $thr3->join(); } sub scan { my (@hosts, $port) = @_; foreach my $host (@hosts) { my $socket = new IO::Socket::INET ( PeerHost => $host, PeerPort => $port, Proto => "tcp", ); if($socket) { print $port,"is open!\\n"; $socket->close(); } } }
####################################################
After running,prompt Errors as following;
thread failed to start: Not a CODE reference at pcmnt.pl line 28.
thread failed to start: Not a CODE reference at pcmnt.pl line 29.
thread failed to start: Not a CODE reference at pcmnt.pl line 30.
thread failed to start: Not a CODE reference at pcmnt.pl line 31

Replies are listed 'Best First'.
Re: About "thread failed to start:"
by BrowserUk (Patriarch) on Apr 09, 2015 at 06:54 UTC

    Why are you using two backslashes?

    my $thr0 = threads->new(\\&scan, "$net@ip[0..63]", $port); # ^^

    Remove one of them and you'll at least get past the "Not a CODE reference" error.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

      thx
      I'm a beginner of Perl.I made a low mistake

        So -- how is it working now?

        Just so you know, you generally double the backslashes when they are inside quotation marks and not double them otherwise.