3

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]>;
}

It will tell me Use of uninitialized value $ligne in pattern match (m//) for the second $ligne = <$socket1[0]>;

I don't understand wy

2 Answers 2

5

Angle brackets are used also for glob(),

perl -MO=Deparse -e '$ligne = <$socket1[0]>;'
use File::Glob ();
$ligne = glob($socket1[0]);

so if you're not using plain scalar as socket, you might to be more explicit by using readline(),

$ligne = readline($socket1[0]);
Sign up to request clarification or add additional context in comments.

Comments

2

The I/O Operator <EXPR> can mean either readline or glob depending on EXPR.

In this instance, you need to use an explicit readline in order to accomplish what you want.

Additionally, you should always perform the reading of a handle in a while loop and put any additional flow logic inside the loop. This is because a while loop that is reading from a file will automatically check for an eof condition. The way that your code is currently written you could end up with an infinite loop if it never finds that regular expression.

To fix both issues, I would therefore rewrite your processing to the following:

my $ligne;

while ( $ligne = readline $socket1[0] ) {
    last if $ligne =~ /Content-Length:/;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.