Solved

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

Posted on 2006-10-20
1
290 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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