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 -h
Use 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.

Searched for donut and crumpit. Found donate and stumbit instead.

In reply to Want END block to run except when usage()/--help invocation by CoVAX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.