Read sockets in an array using <$ socket [i]> in perl
I want to read my sockets and do a "getline" on them.
my @socket1; $socket1[0] = IO::Socket::INET->new( Type => SOCK_STREAM, PeerAddr => "127.0.0.1", Proto => "tcp", PeerPort => $dbase_param{camera_stream} ) or die "Cannot open socket on port " . $dbase_param{camera_stream} . ".\n"; print { $socket1[0] } "\n"; my $ligne = <$socket1[0]>; while ( !( $ligne =~ /Content-Length:/ ) ) { $ligne = <$socket1[0]>; }
He'll tell me Use of uninitialized value $ligne in pattern match (m//)
for a second$ligne = <$socket1[0]>;
I don't understand wy
source share
Angle brackets are also used for glob()
,
perl -MO=Deparse -e '$ligne = <$socket1[0]>;' use File::Glob (); $ligne = glob($socket1[0]);
so if you are not using a normal scalar as a socket you can be more explicit by using readline()
,
$ligne = readline($socket1[0]);
source share
I / O operator<EXPR>
can mean either readline
or glob
depending on EXPR
.
In this case, you need to use explicit readline
to accomplish what you want.
Also, you should always loop read the handle while
and put any additional thread logic inside the loop. This is because the loop while
that reads from the file automatically checks the condition eof
. The way your code is currently written may end up in an endless loop if it never finds that regex.
To fix both problems, I would rewrite your handling as follows:
my $ligne; while ( $ligne = readline $socket1[0] ) { last if $ligne =~ /Content-Length:/; }
source share