getopts function in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getopts function in Perl
# 1  
Old 03-19-2012
getopts function in Perl

Hi All
I have searches getopts function in Perl a lot, but yet i didn't cleared with it.

First I want to know what is the meaning of
getopts('t:c:', \%options);

and please explain getopts function in an easy way..
# 2  
Old 03-19-2012
Is this homework?

The Getopt::Std module comes with sufficient documentation, try perldoc Getopt::Std and be clear about what you do not follow in the documentation if you need further explanation.
# 3  
Old 03-20-2012
no, I am learner of Perl
# 4  
Old 03-20-2012
Code:
getopts('t:c:', \%options);

Is a call to the getopts function, it expects there to be 2 flags (-t and -c) each of these require a para;
meter the values of which will be accessable in the %options hash. So if the Perl script is called as follows my_script.pl -t 5 -c apples then within the scope that %options is defined the values can be accessed as $options{t} and $options{c}. Seriously read perldoc Getopt::Std
# 5  
Old 03-20-2012
'getopts' function from Getopt::Std module would allow you to provide command line options and values to those options.

A simple example.. in echo "hello" | cut -c1 , -c is an option provided to cut and 1 is a value quantifying that option. In this case you're telling cut command to cut string character-wise and specifically just the 1st character.

Here's a sample perl program illustrating the use of getopts:

Code:
#! /usr/bin/perl
use warnings;
use strict;
use Getopt::Std;

my %options;
getopts ("t:c:", \%options);

foreach (keys %options) {
    print "Option '$_' ; Value '$options{$_}'\n";
}

Code:
$ ./test.pl -t hello -c world
Option 'c' ; Value 'world'
Option 't' ; Value 'hello'

The colon in "t:c:" tells 'getopts' that 't' and 'c' options need to have values along with them. If values are not supplied, then the hash %options would have a key as 't' with a null value. This is of no use, right.

You could also give getopts ("tc", \%options) to tell getopts that along with options 't' and 'c', values would not be supplied. In this case, the corresponding values for keys 't' and 'c' in hash %options would be 1.

You should consider Skrynesaver's advice: Seriously read perldoc Getopt::Std
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl qr function usage

I'm not sure why I'm having so much trouble with this. I think I'm really not understanding how this works. I'm trying to store a regex in a variable for use later in a script. Can someone tell me why this doesn't match??? #!/usr/bin/perl # # # $ticket=1212; my $rx_ticket =... (1 Reply)
Discussion started by: timj123
1 Replies

2. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

3. UNIX for Advanced & Expert Users

[BASH] Getopts/shift within a function, unexpected behaviour

Hello Gurus :) I'm "currently" (for the last ~2weeks) writing a script to build ffmpeg with some features from scratch. This said, there are quite a few features, libs, to be downloaded, compiled and installed, so figured, writing functions for some default tasks might help. Specialy since... (3 Replies)
Discussion started by: sea
3 Replies

4. Shell Programming and Scripting

Perl : Getopts and Strict -How to solve

How do I get past the error when using strict and GetOpts ? #!/usr/bin/perl use strict; use Getopt::Std; # Process the command line options die "Usage: $0 -r <router> -u <username> -p <password> -e <enable password>\n" if (@ARGV < 6); exit if (!getopts('r:u:p:e:')); my... (3 Replies)
Discussion started by: popeye
3 Replies

5. Shell Programming and Scripting

Getopts inside a function is not working

Hi All, I am using geopts inside a function in shell script. But it is doesnt seem to read the input args and I always gt empty value in o/p. my code is http://sparshmail.ad.infosys.com/owa/14.2.318.4/themes/base/pgrs-sm.gif This message has not been sent. #!/bin/ksh IFS=' '... (1 Reply)
Discussion started by: prasperl
1 Replies

6. Shell Programming and Scripting

ksh function getopts get leading underscore unless preceded by hyphen

If I call my function with grouped options: "logm -TDIWEFO 'message' ", then only the "T" gets parsed correctly. The subsequent values returned have underscores prefixed to the value: "_D", "_I", etc. If I "logm -T -DIWEFO 'message' ", the "T" and the "D" are OK, but "I" through "O" get the... (2 Replies)
Discussion started by: kchriste
2 Replies

7. Shell Programming and Scripting

How to use parameter with perl function?

am a beginer of shell Unix, plesase tell me how to get parameter with $perl in loop $ perl -lne '$/="DOCEND";print $_."DOCEND" if /$ACC/' file_input > output i can't get parameter in loop with perl fucntion like this pass paramerter to $ACC not accept in this script $ACC = paramerter to... (4 Replies)
Discussion started by: krai
4 Replies

8. Shell Programming and Scripting

Use split function in perl

Hello, if i have file like this: 010000890306932455804 05306977653873 0520080417010520ISMS SMT ZZZZZZZZZZZZZOC30693599000 30971360000 ZZZZZZZZZZZZZZZZZZZZ202011302942311 010000890306946317387 05306977313623 0520080417010520ISMS SMT ZZZZZZZZZZZZZOC306942190000 30971360000... (5 Replies)
Discussion started by: chriss_58
5 Replies

9. Shell Programming and Scripting

ksh: can you use getopts in a function?

I wrote a script that uses getopts and it works fine. However, I would like to put the function in that script in a startup file (.kshrc or .profile). I took the "main" portion of the script and made it a function in the startup script. I source the startup script but it doesn't seem to parse... (4 Replies)
Discussion started by: lyonsd
4 Replies

10. Shell Programming and Scripting

PERL function problem

I have perl script as follow. ------------------------------------------------------------------------ #! /usr/bin/env perl use strict; sub printLines { print "Inside the function.............\n"; my (@file , $count , $key ) = $_; print $count , $ key ; #... (2 Replies)
Discussion started by: avadhani
2 Replies
Login or Register to Ask a Question