Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl efficient DB Call 8 81
add a syntax to a csv file 8 98
Writing a parser for java language 4 71
Vb script to unzip a files and rename the files 5 82
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

15 Experts available now in Live!

Get 1:1 Help Now