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

I have doubt about vec function .
+ what vec() does here ?
+ Without calling vec() function also this piece of code work fine what i have expected .
+ split (//, unpack("b*", $OutputBuffer)) what it does?
use IO::Select; use IO::Socket; $lsn = new IO::Socket::INET(proto => 'tcp', Listen => 1, LocalAddr => + '192.168.6.92:9090', Reuse => 1); print "Error in socket :$!\n"; $sel = new IO::Select( $lsn ); $i = 0; $IPHASH= {}; while(@ready = $sel->can_read) { foreach $fh (@ready) { if($fh == $lsn) { # Create a new socket $new = $lsn->accept; $HostAdd = $new->peername(); ($Port , $Myadd) = sockaddr_in($HostAdd); $IPAdd = inet_ntoa($Myadd); $sel->add($new); $IPHASH->{"$IPAdd"} = $new; } else { # Process socket $InputBuffer; $OutputBuffer; $Delay = 0.1; vec($InputBuffer, fileno($fh),1)=1; ($Found, $Temp) = select($OutputBuffer=$Inp +utBuffer, undef, undef, $Delay); my (@Bits) = split (//,unpack("b*", $OutputBuffer)); $handle = fileno($fh); recv ($fh, $Received_Message, 20, 0); $HostAdd = $new->peername(); ($Port , $Myadd) = sockaddr_in($HostAdd); $IPAdd = inet_ntoa($Myadd); $fh = $IPHASH->{$Received_Message}; #vec($InputBuffer, fileno($fh), 1)=1; #($Found, $Temp) = select($OutputBuffer=$I +nputBuffer, undef, undef, $Delay); # my (@Bits) = split (//, unpack("b*", $Out +putBuffer)); # $handle = fileno($fh); print "::: $Received_Message :::: \n"; $fh->autoflush(1); # Maybe we have finished with the socket $sel->remove($fh); $fh->close; } $i++; } }

Replies are listed 'Best First'.
Re: vec and unpack
by ikegami (Patriarch) on Nov 24, 2008 at 08:23 UTC

    what vec() does here ?

    Tells select to which file handles to pay attention.

    By the way, IO::Select provides a much better interface than select.

    Without calling vec() function also this piece of code work fine what i have expected .

    Because the select is redundant with the select in can_read.

    split (//, unpack("b*", $OutputBuffer)) what it does?

    Nothing, @Bits is never used.

Re: vec and unpack
by Corion (Patriarch) on Nov 24, 2008 at 08:19 UTC
Re: vec and unpack
by lakshmananindia (Chaplain) on Dec 04, 2008 at 10:43 UTC
    split (//, unpack("b*", $OutputBuffer)) what it does
    From the vec documentation

    To transform a bit vector into a string or list of 0's and 1's, use
    $bits = unpack("b*", $vector);
    @bits = split(//, unpack("b*", $vector));


    We cannot print vector variables directly.
    In sockets program which uses select will use vec to set a bit.
    So if you want to print the bits we cannot print it directly.
    For that purpose we will use the unpack.