CoVAX has asked for the wisdom of the Perl Monks concerning the following question:
This is the first time I've used an END block. I need its functionality because I call 'die' at several points throughout this program and I have to always perform the code in the END block.
I was making the final edits to a program's usage() function and noticed the following warnings following a --help invocation:
perl -w ks.pl -hUse of uninitialized value $time in concatenation (.) or string at ks. +pl line 253. Use of uninitialized value in concatenation (.) or string at ks.pl lin +e 258. Use of uninitialized value in concatenation (.) or string at ks.pl lin +e 258. Use of uninitialized value in concatenation (.) or string at ks.pl lin +e 258. Use of uninitialized value in concatenation (.) or string at ks.pl lin +e 258. Character in 'c' format wrapped in pack at ks.pl line 262. Use of uninitialized value $time in concatenation (.) or string at ks. +pl line 283.
Ah so! (German for "I see!") -- these warnings were the result of the END block being (unconditionally) executed after the usage() function.
While I understand why this is happening, my question is: is there a way to have an unconditional END block execute except when the program's usage() function is being invoked? If not, then I propose to create yet another function to be called prior to die()'ing that will execute what's presently in the END block.
Here's the relevant code:
#!/usr/bin/perl -w use strict; use Getopt::Std; ... # Don't want END block to execute when these are invoked: sub HELP_MESSAGE(); # getopts() --help automatically invokes VERSIO +N_MESSAGE() first sub VERSION_MESSAGE(); # getopts() supports arguments --version and -- +help sub usage(); ... INIT { $| = 1; # Make Getopt::Std exit after printing # VERSION_MESSAGE() and/or HELP_MESSAGE() $Getopt::Std::STANDARD_HELP_VERSION = 1; } ...By design, main-line code comes to this END block: # An END block is always executed (barring some exceptions) as # late as possible, even after die() is called END { export_to_file_csv(); export_to_storable(); warn "Pages : $pages of Total Pages: $total_pages"; warn "Projs : $projs of Total Projs: $total_projs"; warn sprintf ("Images: %d amounting to bytes: %s", $imgs, $bytes)); } # END OF PROGRAM (normal or otherwise).
I thank you for your time and assistance.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Want END block to run except when usage()/--help invocation
by BrowserUk (Patriarch) on Feb 27, 2015 at 05:39 UTC | |
by CoVAX (Beadle) on Feb 27, 2015 at 07:31 UTC | |
by Happy-the-monk (Canon) on Feb 27, 2015 at 07:44 UTC | |
by BrowserUk (Patriarch) on Feb 27, 2015 at 09:31 UTC | |
by Laurent_R (Canon) on Feb 27, 2015 at 07:52 UTC | |
by BrowserUk (Patriarch) on Feb 27, 2015 at 09:46 UTC | |
|
Re: Want END block to run except when usage()/--help invocation
by Anonymous Monk on Feb 28, 2015 at 01:01 UTC |