I don't think that you understood what the Monks are saying. This is not "resolved". Do not use GetOpts in this way because amongst other things, you do not get proper checking of the input command line for errors. The entire command line should be checked for errors before you do anything. Calling a sub by GetOpts is part of command line checking and you don't need that to do what you want to do - or my understanding of what you want to do.

Here is one possible implementation:

use strict; use warnings; use Getopt::Long; my $demo = 0; # default value (false) my $test = 0; # default value (false) GetOptions ('demo' => \$demo, 'test' => \$test); ### Once command line is parsed using GetOpts, THEN execute code ### based upon the flags which are set. ### GetOpts removes what it knows about from @ARGV - if there is ### anything left, then you have a syntax error. ### It is up to you to check for contradictory options, like ### perhaps asking for --demo and --test at the same time! if (@ARGV) { print "Illegal syntax \'@ARGV\' is bogus!\n"; printHelp(); } if (!($demo or $test)) { print "No option specified!\n"; printHelp(); } if (!($demo xor $test)) #test for inconsistent options { print "Only one option allowed!\n"; printHelp(); } sub printHelp { print "*** print something useful for help message**\n"; exit (1); #error exit to shell - the command "didn't work" } ### actual "code" is here ### demo() if $demo; #simple for this scenario test() if $test; sub demo { print "doing a demo\n"; } sub test { print "doing a test\n"; } __END__ example runs: >perl longopts.pl No option specified! *** print something useful for help message** >perl longopts.pl -d abc Illegal syntax 'abc' is bogus! *** print something useful for help message** >perl longopts.pl -d -t Only one option allowed! *** print something useful for help message** >perl longopts.pl adf Illegal syntax 'adf' is bogus! *** print something useful for help message** >perl longopts.pl -d doing a demo >perl longopts.pl -t doing a test
How to get GetOpts to parse say: command --test 23 --fancyprint is more complicated than the above syntax, but certainly possible.

In reply to Re^3: Passing Parameters to subroutine by Marshall
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.