Errormsgs on GetOptions

T

Tom Gur

Hi,

I'm using GetOptions in the following manner:
$getoptions_rc = GetOptions( "host=s" => \$host, "port=n" => \$port);
Now, in case the user inputs something wrong, like a letter in the
port - GetOption prints a default errormsg.
how can I suppress the print and instead store the errormsg in a
variable ?

Thanks,
Tom
 
P

Paul Lalli

I'm using GetOptions in the following manner:
$getoptions_rc = GetOptions( "host=s" => \$host, "port=n" => \$port);
Now, in case the user inputs something wrong, like a letter in the
port - GetOption prints a default errormsg.
how can I suppress the print and instead store the errormsg in a
variable ?

The same way you trap any other warning - by assigning a handler to
the $SIG{__WARN__} variable:

perl -MGetopt::Long -le'
$SIG{__WARN__} = sub { $warn = shift; };
$getoptions_rc = GetOptions("port=n" => \$port);
print "GetOptions returned: <<$getoptions_rc>>";
print "Warning: <<$warn>>" if $warn;
' - --port foo

Result:


Note that you might want to do this in a block and local'ly set
$SIG{__WARN__} so that it resets after the block is over and other
warnings print to STDERR like normal...

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top