Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Perl, how can I check for the existence of Socket options without generating warnings?

I'm checking for the existence and default values of various socket options using Perl.

#!/usr/bin/perl -w
use strict;
use Socket;

if (defined(SO_BROADCAST)) {
    print("SO_BROADCAST defined\n");
}

if (defined(SO_REUSEPORT)) {
    print("SO_REUSEPORT defined\n");
}

When I run this it outputs:

SO_BROADCAST defined

Your vendor has not defined Socket macro SO_REUSEPORT, used at ./checkopts.pl line 9

Is there a way to do this without generating warnings in the output?

like image 697
Robert S. Barnes Avatar asked Dec 29 '22 03:12

Robert S. Barnes


1 Answers

That message is coming from AUTOLOAD in Socket.pm. When it finds a constant that isn't supported, it croaks. You can catch that with an eval:

 use Socket;

 if( defined eval { SO_REUSEPORT } ) {
      ...;
      }
like image 109
brian d foy Avatar answered Jan 13 '23 16:01

brian d foy