Simon336697
asked on
Perl GetOptions: Undefined argument in option spec
Hi guys, hope u r all well.
Guys im trying to learn how to read command line arguments by using the GetOpt module, but each time i run the following examples, which im assuming "should" work im getting an error saying:
"Undefined argument in option spec"
Here are the examples:
========================== ========== ========== ========== ========= example1.pl
#!/usr/bin/perl
# import module
use Getopt::Long;
# read options
$result = GetOptions ("age=i" => $age);
# print value
if ($age) { print "Input age is $age years"; }
========================== ========== ========== ========== ========= example2.pl
#!/usr/bin/perl
# import module
use Getopt::Long;
# read options
$result = GetOptions ("name=s" => $name);
# print value
print "Input name is $name";
When I run these examples with or without options specified, im getting the following...
Undefined argument in option spec
Id love to know what is happening.
Thank you :>)
Guys im trying to learn how to read command line arguments by using the GetOpt module, but each time i run the following examples, which im assuming "should" work im getting an error saying:
"Undefined argument in option spec"
Here are the examples:
==========================
#!/usr/bin/perl
# import module
use Getopt::Long;
# read options
$result = GetOptions ("age=i" => $age);
# print value
if ($age) { print "Input age is $age years"; }
==========================
#!/usr/bin/perl
# import module
use Getopt::Long;
# read options
$result = GetOptions ("name=s" => $name);
# print value
print "Input name is $name";
When I run these examples with or without options specified, im getting the following...
Undefined argument in option spec
Id love to know what is happening.
Thank you :>)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hielo,
I tried the following the code snippet, and get the following message:
Name "main::result" used only once: possible typo at /simon_data/scripts/age_ge toptions.p l line 5.
I tried the following the code snippet, and get the following message:
Name "main::result" used only once: possible typo at /simon_data/scripts/age_ge
#!/usr/bin/perl -w
# import module
use Getopt::Long;
# read options
$result = GetOptions("age=i" => \$age);
# print value
if ($age) { print "Input age is $age years"; }
ASKER
hielo,
I think ive got it but dont understand why...
I gave $result an initial value
eg.
$result =0
I think ive got it but dont understand why...
I gave $result an initial value
eg.
$result =0
#!/usr/bin/perl -w
# import module
use Getopt::Long;
# read options
$result = 0;
$result = GetOptions("age=i" => \$age);
# print value
if ($age) { print "Input age is $age years\n"; }
From that page:
my $verbose = ''; # option variable with default value (false)
my $all = ''; # option variable with default value (false)
GetOptions ('verbose' => \$verbose, 'all' => \$all);
I would have expected you to initialize $age rather than $result.
my $verbose = ''; # option variable with default value (false)
my $all = ''; # option variable with default value (false)
GetOptions ('verbose' => \$verbose, 'all' => \$all);
I would have expected you to initialize $age rather than $result.
ASKER
hielo,
You are brilliant mate.
I found this other example here:
#!/usr/bin/perl
# import module
use Getopt::Long;
# set default value for option
$debug = 0;
# get value of debug flag
$result = GetOptions ("debug" => $debug);
# print value
print "Debug flag is $debug";
from following page:
http://www.devshed.com/c/a/Perl/Processing-Command-Line-Options-with-PERL/2/
You are brilliant mate.
I found this other example here:
#!/usr/bin/perl
# import module
use Getopt::Long;
# set default value for option
$debug = 0;
# get value of debug flag
$result = GetOptions ("debug" => $debug);
# print value
print "Debug flag is $debug";
from following page:
http://www.devshed.com/c/a/Perl/Processing-Command-Line-Options-with-PERL/2/
ASKER