getopt in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getopt in perl
# 1  
Old 03-02-2009
getopt in perl

Hi,

I have a perl script with two functions say func a and func b.

sub a {
-----------
---------
}

sub b {
---------
---------
}

I want to use this function on command line as we can do in shell script using getopt.

My motto here is to run the script like this

my_perl_script -a(function a is called) -b(function b is called) -h(help)

i have the functions ready but confused how to use getopt here or some similar utility which is there in perl.

Any kind of help is much appericiated.

Thanks
NT
# 2  
Old 03-02-2009
# 3  
Old 03-03-2009
Hi,

Can you give me a psuedo code, i will write myself later.
Which one to use here in my code.

Thanks
NT
# 4  
Old 03-04-2009
Hi,

Can anyone tell me which getopt to use in my case.
A little syntax help is much appericiated.


Thanks
NT
# 5  
Old 03-04-2009
I prefer to help people that show effort, but here is a not very good example:

Code:
use warnings;
use strict;
use Getopt::Std;
my $opt = 'abh';
my %opt;
getopts( $opt, \%opt );

a() if $opt{a};
b() if $opt{b};
help() if $opt{h};  

sub a {
   print "We are in a\n";
}
sub b {
   print "We are in b\n";
}

sub help {
   print We are in h\n";
}


See GetOpt::Std for more details
# 6  
Old 03-06-2009
Hi All,

I am using getopt like this --
Code:
use Getopt::Std;
my $opt = "epu";
my %options;
getopts( $opt , \%options );
evaluate_risk_action_time() if  defined  $options(e) ;
post_evaluate_action_time() if defined  $options(p) ;
usage() if defined  $options(u) ;

but when i run this as --

Code:
perl filename.pl -e -p -u

I am getting these errors--

Code:
C:\>perl evaluate.pl -e -p -u
Global symbol "$options" requires explicit package name at evaluate.pl line 100.

syntax error at evaluate.pl line 100, near "$options("
Global symbol "$options" requires explicit package name at evaluate.pl line 101.

syntax error at evaluate.pl line 101, near "$options("
Global symbol "$options" requires explicit package name at evaluate.pl line 102.

syntax error at evaluate.pl line 102, near "$options("
Execution of evaluate.pl aborted due to compilation errors.

Kindly advise some tips..

Thanks
NT
# 7  
Old 03-06-2009
You didn't look at my example very closely. You are using parentheses () where you should be using curly brackets {}

$options('e') should be $options{'e'} same for the other ones
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getopt Help

Hi All, An old work friend wrote a script which I've been trying to understand how a section of it currently works and work out how i can add some command line switches which i can use later in the script to append the output depending on the command line arguements. Currently it works by... (1 Reply)
Discussion started by: mutley2202
1 Replies

2. Shell Programming and Scripting

Perl :: Getopt::Long in the program

While going through some of the perl script... I had found the below line.. use Getopt::Long; my $GetOptionsReturnCode = GetOptions ( '<>' => sub { push(@unknownArg, @_); }, 'h|help' => sub { &helpMessage(); exit 0; }, ); Could anyone please explain the above one ... (1 Reply)
Discussion started by: scriptscript
1 Replies

3. Programming

Perl Getopt::Long

Hi All I am using Getopt::Long in perl and i am trying to have it so if i dont supply a switch after the progname is will do a defult option i have the following GetOptions($OPT, 'debug|d', 'mail|m', ) or info(); i want it run the debug if it is not given a switch ... (1 Reply)
Discussion started by: ab52
1 Replies

4. Shell Programming and Scripting

Perl Getopt::Long question - stopping multiple args

Hi there, I have an example basic script (below) and ive been trying to figure out how to stop multiple arguments to my options occuring. for example using the example script below I can issue two arguments for, say the --surname option and it will not barf at me (although thats what i want it to... (11 Replies)
Discussion started by: rethink
11 Replies

5. Shell Programming and Scripting

Help with getopt

Hi, I want to use the getopt function to parse some arguments for a script. while getopts "i:f:r:" OPTION do case $OPTION in i) iter=$OPTARG;; f) frame=$OPTARG;; r) roi=$OPTARG;; ?) echo Usage: ...... exit 2;; esac done However, I... (5 Replies)
Discussion started by: giorgos193
5 Replies

6. Solaris

use of getopt command

Hi All, Could anyone tell me how to use getopt command.....? Thanks, Pintu (2 Replies)
Discussion started by: pintupatro
2 Replies

7. Shell Programming and Scripting

getopt help

:) Can anybody help me about how to use getopt in shell scripting. (3 Replies)
Discussion started by: darshakraut
3 Replies

8. Shell Programming and Scripting

getopt help

I m trying to use getopt This is my script, but it doesn't take argument in variable, Please help. set - - `getopt mscl: $*` if then echo "Exiting...." exit 2 fi for i in $* do case $i in -m) MAIL="$i"; shift;; -s) SCRIPT=$OPTARG; shift;; -c) COB=$OPTARG; shift;;... (2 Replies)
Discussion started by: darshakraut
2 Replies

9. Shell Programming and Scripting

getopt

#!/bin/sh set -- `getopt "abco:" "$@"` a= b= c= o= while : do case "$1" in -a) a=1;; -b) b=1;; -c) c=1;; -o) shift; o="$1";; --) break;; esac shift done shift # get rid of -- # rest of script... # e.g. ls -l $@ (6 Replies)
Discussion started by: Hitori
6 Replies

10. Shell Programming and Scripting

getopt help

scriptname i have made a script to perform so tasks and i managed to complete the tasks for all the options the problem i am facing is that i can run the scripts individually but i would like to make it such that it can accept multiple options and give me the appropriate output e.g.... (1 Reply)
Discussion started by: problems
1 Replies
Login or Register to Ask a Question