Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: INIT {$SIG{__DIE__} and Getopt::Long

by fishmonger (Chaplain)
on Jul 29, 2015 at 18:22 UTC ( [id://1136777]=note: print w/replies, xml ) Need Help??


in reply to INIT {$SIG{__DIE__} and Getopt::Long

Open the log file before executing GetOptions().

  • Comment on Re: INIT {$SIG{__DIE__} and Getopt::Long

Replies are listed 'Best First'.
Re^2: INIT {$SIG{__DIE__} and Getopt::Long
by Anonymous Monk on Jul 29, 2015 at 18:25 UTC
    I don't want to have a log file generated if somebody is chosing the wrong parameter or just running something like x.pl -V to get the version information.

      Wouldn't it make more sense to create (or open in append mode) but not add an entry in that case?

      Personally, I'd handle the logging via the Log::Log4perl module and instead of your current usage sub, I'd write POD documentation and output that via the Pod::Usage module.

      UPDATE:
      Here's a short example using Pod::Usage instead of your custom die SIG handler.

      use strict; use warnings; use Getopt::Long qw(:config no_ignore_case bundling); use Pod::Usage; my $VERSION = "2.0.0"; # Check Flags my $help; my $man; my $version; my $config; GetOptions ( 'h|help' => \$help, 'm|man' => \$man, 'V|VER' => \$version, 'c|config=s' => \$config, ) or pod2usage(2); pod2usage(1) if $help; pod2usage(-verbose => 2) if $man; pod2usage( { -message => "Version: $VERSION\n" }) if $version; # I forgot to include the open call when I posted this update # This open call won't execute if any of the above pod2usage() stateme +nts get executed. open( my $log_fh, '>', "SCRIPTLOG_FILE") or die ("Can't open SCRIPTLOG_FILE: $!\n"); # note that I used a lexical filehandle and the 3 arg form of open __END__ =head1 NAME sample - Using GetOpt::Long and Pod::Usage =head1 SYNOPSIS sample [options] [file ...] Options: -help brief help message -man full documentation =head1 OPTIONS =over 4 =item B<-help> Print a brief help message and exits. =item B<-man> Prints the manual page and exits. =back =head1 DESCRIPTION B<This program> will read the given input file(s) and do something useful with the contents thereof. =cut

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1136777]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-19 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found