From: perl-win32-users-boun...@listserv.activestate.com [mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of Ashley Hoff Sent: 02 December 2011 01:47 To: perl-win32-users@listserv.ActiveState.com Subject: Capturing STDERR (Specifically from Getopt::Long)
> 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_long 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] Not entirely surprising, as sub is called with no arguments, i.e. @_ is empty. That example does not appear to have been tested very well! > 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] By screwed up, did you mean that the original STDERR has been closed? > 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 The easiest way may be to use a temporary $SIG{__WARN__} handler to capture warnings from GetOptions, e.g. something like... { # Capture warnings from GetOptions local $SIG{__WARN__} = sub { $message .= $_[0];}; GetOptions('sleep=s' => \$sleep, 'size' => \$size, 'debug' => \$debug); } That should work as long as GetOptions uses 'warn' to report errors. HTH -- Brian Raven Please consider the environment before printing this e-mail. This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy. Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden. _______________________________________________ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs