Greetings,

I have been playing around with Getopt::Long and really want to capture
the STDERR message to a variable when a non-existent argument is used.
I will use this variable to form the basis of an email and log entry
when the script falls over, instead of just issuing a die command.

Initially, I started with the code found here -
http://www.perlhowto.com/parsing_command_line_parameters_with_getopt_lon
g to see if I could get it working.  While the code in itself works, the
print statement "print "Unknown option: @_\n" if ( @_ );" in the
subroutine doesn't seen to work (example code below from above link):

[code]
#!/usr/bin/perl
use Getopt::Long;
 
my ($help, @url, $size);
 
#-- prints usage if no command line parameters are passed or there is an
unknown
#   parameter or help option is passed
usage() if ( @ARGV < 1 or
          ! GetOptions('help|?' => \$help, 'url=s' => \@url, 'size=i' =>
\$size)
          or defined $help );
 
sub usage
{
  print "aaaaaUnknown option: @_\n" if ( @_ );
  print "usage: program [--url URL] [--size SIZE] [--help|-?]\n";
  exit;
}
[/code]

I have also tried closing the STDERR and then writing the STDERR to a
variable, but this seemed to screw up the STDERR for the rest of the
script:

[code]
#!/usr/bin/perl
use Getopt::Long;
 
my $sleep = "";
my $size = 0;
my $debug = 0;
my $message = "";
 
close(STDERR);
open(STDERR, ">>", \$message) or die "can't open\n";
GetOptions('sleep=s' => \$sleep, 'size' => \$size, 'debug' => \$debug);

print "It was no good with $message\n";
print "test another line\n";

opendir (DIRS,"C:\\asdf") or die "died $!\n";
[/code]

I have also tried to close and reopen the STDERR.

What would the be the easiest way to capture the STDERR from
Getopt::Long, to be re-used later, without breaking STDERR?

By the way, I am using Perl V5.8.9

Cheers
Ashley
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to