Solved

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

Posted on 2006-10-20
1
265 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
1 Comment
 
LVL 9

Accepted Solution

by:
Suhas . earned 450 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now