I am new to Perl and trying to package some of my earlier programs into a new program as subroutines. I would like to use Getopt::Long to then specify which subroutines to run. So far I have something that looks kind of like this:

GetOptions( "one" => .....sub1().... , "two" => .....sub2()...., "three" => ....sub3().... ); sub sub1 { print "sub one"; } sub sub2{ print "sub two"; } sub sub3 { print "sub three"; }

I guess I could set each option equal to a $scalar like this:

GetOptions( "one" => $three, "two" => $two, "three" => $three );

I could then call the subroutines using simple if statements. However the last few weeks of working with Perl has led me to suspect that Perl programmers, being clever and brilliant people, have a direct way just calling the subroutines without using if statements at all. Could anyone confirm or dispell this suspicion?

Also I read in the Getopt::Long's perldoc that you need to use "Permute" to set a subroutine as default if no argument is passed in the command line. How would I make it so that subroutine sub1 is called when not argument is given in the command line. Could anyone point me towards an example?

Thank you all for your time and attention.

-mox


In reply to Using Getopt::Long to call subroutines by chinamox

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.