I am using Getopt::Long. When passing a parameter via the command line, getopts seems to pass the parmeter on the cli followed by a 1 to the identified subroutine. I would be expecting, when the subroutine is called from the option passed from CLI, the parameters passed to the actual subroutine would be null/empty. when passing parameter to a subtroutine from within the script, the expected parameters are passed and printed. Any idea or assistance on how to achieve my expected results?
#!/usr/bin/perl use strict; use Getopt::Long; GetOptions ('demo' => \&demo, 'test' => \&Test) or die("Error in command line arguments\n"); sub demo () { die "Wrong number of args" if (scalar(@_) != 2); my $arg0 = $_[0]; my $arg1 = $_[1]; my $arg2 = $_[2]; my $arg3= $_[3]; my $arg4 = $_[4]; my $arg5 = $_[5]; print "$arg0, $arg1, $arg2, $arg3, $arg4, $arg5\n"; } sub Test () { # call the subroutine my $arg0 = $_[0]; my $arg1 = $_[1]; my $arg2 = $_[2]; my $arg3= $_[3]; my $arg4 = $_[4]; my $arg5 = $_[5]; print "$arg0, $arg1, $arg2, $arg3, $arg4, $arg5\n"; &demo("hello", "world"); } C:\temp>passparm.pl --demo demo, 1, , , , <== would expect nothing to print C:\temp>passparm.pl --test test, 1, , , , <== would expect nothing to print hello, world, , , , <== Expected C:\temp>passparm.pl --demo --test demo, 1, , , , <== would expect nothing to print test, 1, , , , <== would expect nothing to print hello, world, , , , <== Expected

In reply to 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.