Link to home
Start Free TrialLog in
Avatar of Simon336697
Simon336697Flag for Australia

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  :>)
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Simon336697

ASKER

You are so fast and so generous hielo....ill give that a go mate
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_getoptions.pl line 5.



 #!/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"; }

Open in new window

hielo,
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"; }

Open in new window

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.
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/