[PERL] Check if socket is connected before sending data

Hello community,
I'm programming a simple code using socket connection in perl:

$sock = new IO::Socket::INET(
                  PeerAddr => '192.168.10.7',
                  PeerPort => 8000,
                  Proto    => 'tcp');
$sock or die "no socket :$!";

Then sending data using a loop:

while...
print $sock $str;
...loop

Is there a way to insert into the loop a command to check connection?
Something like:

while...
   socket is up?
   yes => send data
   no => connect again and the continue with loop
...loop

Thank you
Lucas

No one? :frowning:

Adding the code I tried, just in case. With that, after server disconnection I got:

Bad arg length for Socket::pack_sockaddr_in, length is 14, should be 4  at /usr/lib/perl5/5.8.8/IO/Socket/INET.pm line 224, <$fh> line  35302.
my $sock = new IO::Socket::INET(
                    PeerAddr => '192.168.10.152',
                    PeerPort => 8000,
                    Proto    => 'tcp');
  $sock or die "no socket :$!";

  open(my $fh, '<:encoding(UTF-8)', 'list1.txt')
      or die "Could not open file $!";

  while (my $msdn = <$fh>) {
        my $port="8000";
        my $ip="192.168.10.152";
        unless ($sock->connected) {
          $sock->connect($port, $ip) or die $!;
    }
    my $str="DATA TO SEND: " . $msdn;
    print $sock $str;
  }
  close($sock);