?
Solved

Using Getopt::Long to call one or more Subroutines..

Posted on 2006-10-20
1
Medium Priority
?
321 Views
Last Modified: 2008-03-06
How do I use Getopt::Long to call one or more sub routines in my program?


__Code__

GetOptions( "one" => .....sub1().... ,
            "two"  => .....sub2()....,
             "three" => ....sub3().... );


sub sub1 {
print "sub one";
}

sub sub2{
print "sub two";
}

sub sub3 {
print "sub three";
}

__End Code__

Also I read in the Getopt::Long's perldoc that you need to use "Permute" to set a subroutine as default if no arguement is passed in the command line.  How would I make it so that subroutine sub1 is called when not arguement is given in the commandline.


















0
Comment
Question by:chinamox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 9

Accepted Solution

by:
Suhas . earned 1800 total points
ID: 17782927
Hi chinamox,

use Getopt::Long;

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


sub sub1 {
print "sub one\n";
}

sub sub2{
print "sub two\n";
}

sub sub3 {
print "sub three\n";
}


on command line,
$perl prog.pl -one
#prints
sub one
$perl prog.pl -one -two
#prints
sub one
sub two

Cheers!
Suhas
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question