The Getopt::Long docs have a too trivial example of how to run a subroutine by setting an option. Here's what the doc says:

User-defined subroutines to handle options Ultimate control over what should be done when (actually: each +time) an option is encountered on the command line can be achieved by + designating a reference to a subroutine (or an anonymous subroutine) + as the option destination. When GetOptions() encounters the option, +it will call the sub-routine with two or three arguments. The first a +rgument is the name of the option. For a scalar or array destination, + the second argument is the value to be stored. For a hash destinatio +n, the second arguments is the key to the hash, and the third argumen +t the value to be stored. It is up to the subroutine to store the val +ue, or do whatever it thinks is appropriate. A trivial application of this mechanism is to implement options + that are related to each other. For example: my $verbose = ''; # option variable with default value (f +alse) GetOptions ('verbose' => \$verbose, 'quiet' => sub { $verbose = 0 }); Here "--verbose" and "--quiet" control the same variable $verbo +se, but with opposite values. If the subroutine needs to signal an error, it should call die( +) with the desired error message as its argument. GetOptions() will c +atch the die(), issue the error message, and record that an error res +ult must be returned upon completion. If the text of the error message starts with an exclamantion ma +rk "!" it is interpreted specially by GetOptions(). There is current +ly one special command implemented: "die("!FINISH")" will cause GetOp +tions() to stop processing options, as if it encountered a double das +h "--".

So, here the doc shows usage with an anonymous subroutine. I'm new to Perl and it would be helpful for me to see usage of predefined subroutines with Getopt::Long. If possible, how do I pass arguments to the subroutine? I haven't been able to find any Perl scripts that use Getopt::Long either. Can you share yours with me, so I can glean the information I need? Thank you.

In reply to Getopt::Long subroutine usage by rockneybot

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.