You seem to have some . . . let's say misconceptions about what Getopt::Long is for and how to use it. When you pass a sub ref to it you're telling it "Hey, if you see option --test then call this sub". So since you're passing that option, that sub gets called. As is documented, passing a sub is meant to allow more complicated behaviors when receiving an option than (say) "set what this scalar ref points to to the next command line argument" and the sub will receive the option name it's being called for and the value (1, the option was present) as the arguments.

It's not meant to be used to wire up program logic directly.

What you want to be doing would be setting flag variables (start off with my $call_test = undef and then passing ... "test" => \$call_test ...) which then AFTER you've let Getopt::Long parse the options you check and conditionally call whatever subs.

(Also don't add empty paren prototypes willy nilly in your code; they don't do anything useful in this context and would actaully break in normal code calling subs normally if you then try and pass arguments (because you've said they take none).)

Edit: Emphasis added because it's not sinking in.

The cake is a lie.
The cake is a lie.
The cake is a lie.


In reply to Re^3: Passing Parameters to subroutine by Fletch
in thread Passing Parameters to subroutine by g_speran

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.