Google
 
 
Home arrow Perl Articles arrow Using Getopt with Perl

Main Menu
 Home
 Linux Articles
 FreeBSD Articles
 Apache Articles
 Perl Articles
 Other Articles
 Program Downloads
 Free Books
 News
 The Web Links
 Contact Us

Most Read
Automating SFTP using expect
FreeBSD PPTP VPN
Shorewall Router on Linux
SnortShorwall - Using Snort And Shorewall Together
Shorewall Stand Alone Firewall

Polls
Favorite Linux/BSD
Fedora
Mandrake
Debian
Slackware
Gentoo
Suse
FreeBSD
Other
  

Syndicate
Latest news direct to your desktop
RSS

Login Form
Username

Password

Remember me
Forgotten your password?
No account yet? Create one

Members Online
 Linux-BSD-Central Has a Total of 701 Members   Members (701) # Online
 We have 5 Guests Online. Guests 5
 We have 0 Users Online. Users 0

Online Users
No Users Online

Statistics
OS: Linux w
PHP: 5.2.9
MySQL: 5.0.91-community
Time: 14:52
Members: 701
Hits: 1324417
News: 277
WebLinks: 15



-->

Using Getopt with Perl   PDF  Print  E-mail 
Contributed by Chad Brandt  
Friday, 02 July 2004

Most of the perl programs I write depend on parameters passed in. An easy way to get parameters in a perl program is to use the Getopt functions.

Here is an example program that shows how you would use Getopt to get the parameters passed to your program

 

#!/usr/bin/perl
########################################################
# Example perl program using Getopt to get command line
# arguments passed to a program.
#
# This example can take 3 parameters
# -h --help
# -v --verbose
# -t=n --timeout=n
#
# using Getopt::Long the -t value can be passed in the
# following ways for a timeout of 5
#
# -t5
# -t=5
# -t 5
# --timeout 5
# --timeout=5
#
########################################################
use
Getopt::Long;

# initialize the parameter options
$help
= '';
$verbose
= '';
$timeout
= 0;

#call the function passing the param name and variable to assign the value to

GetOptions ('h|help' => \$help,
            'v|verbose' => \$verbose,
            'i|timeout=i' => \$timeout);

if
($help){
print "The help parameter was supplied to the programn";
}

if
($verbose){
print "The verbose parameter was supplied to the programn";
}

if
($timeout == 0){
print "timeout was not suppliedn";
}
else {
print "timeout set to $timeoutn";
}

exit(
0);

Comments

Write Comment
Name:Guest
Title:
BBCode:Web AddressEmail AddressBold TextItalic TextUnderlined TextQuoteCodeOpen ListList ItemClose List
Comment:



Powered by AkoComment 1.0 beta 2!




 
Google Ads



 

Check out TwistByte - The best mobile apps available For awesome Android and IPhone applications!!