List Info

Thread: Can't get my command line option via GetOpt::Long




Can't get my command line option via GetOpt::Long
country flaguser name
United States
2007-02-21 14:32:53
I have a utility I want to write and decided that proper
option 
handling would be a Good Thing. Looking around, I found the
following 
reference to GetOpt::Long and a nice little example. 

http://www.devshed.com/c/a/Perl/Processing-
Command-Line-Options-with-
PERL/

So, I entered it into my cygwin environment (under Windows
XP Pro) to 
give it a try... no dice. I've updated my cygwin environment
to no 
avail. I've tracked down the Long module & turned on
debug. It looks 
like the module is extracting out my command line option,
but it 
isn't getting passed back to the calling function. (There is
also a 
couple of warnings generated I don't understand....)

This example does nothing except (try to) show how options
can be 
extracted from the command line.

I'll be eternally grateful for a tap with a clue stick....

Thanks

Eric

------------------------

$ cat colors.pl
#!/usr/bin/perl  -W

{
# import module
#include <getopt_long.h>;
use Getopt::Long;

my $result=0;
my $red = '';
my $blue = '';
my $green = '';

print (" Argument is: $ARGV[0]n");


# read options
$result = GetOptions (  "red!" => $red,
                        "blue" => $blue,
                        "green" => $green );

$result ? print "Was able to process command
line.n" : 
print "Something is Honked.n";

# print options

$red ? print "Red is presentn" : print "Red
is absentn";
$blue ? print
"Blue is presentn" : print "Blue is
absentn";
$green ? print "Green is
presentn" : print "Green is absentn";
}

--------------------------------
Output with debug 

--------------------------------

$ ./colors.pl --red
 Argument is: --red
Getopt::Long 2.34 ($Revision: 2.68 $) called from package
"main".
  ARGV: (--red)
 
autoabbrev=1,bundling=0,getopt_compat=1,gnu_compat=0,order=1
,
 
ignorecase=1,requested_version=0,passthrough=0,genprefix=&qu
ot;(--|-
|+)".
=> link "red" to $main::opt_red
=> link "" to $main::opt_
=> link "blue" to $main::opt_blue
Duplicate specification "" for option
""
=> link "" to $main::opt_
=> link "green" to $main::opt_green
Duplicate specification "" for option
""
=> link "" to $main::opt_
=> $opctl = ARRAY(0x10010dec)
["","green",M,$,"<undef>"
;]
   $opctl{} = ARRAY(0x100cdf70)
["","",M,$,"<undef>"]
   $opctl = ARRAY(0x10011020)
["","blue",M,$,"<undef>"
]
   $opctl = ARRAY(0x100cddc0)
["","red",M,$,"<undef>"]

   $opctl = ARRAY(0x100cdd54)
["!","red",M,$,"<undef>"
]
   $opctl = ARRAY(0x100cdd54)
["!","red",M,$,"<undef>"
]
=> arg "--red"
=> find "--red"
=> split "--"+"red"
=> 1 hits (red) with "red" out of 6
=> found
["","red",M,$,"<undef>"]
for "red"
=> cname for "red" is "red"
=> ref($L) -> SCALAR
=> $$L = "1"
Was able to process command line.
Red is absent
Blue is absent
Green is absent



------------------------ Yahoo! Groups Sponsor
--------------------~--> 
Great things are happening at Yahoo! Groups.  See the new
email design.
http://us.click.yahoo.com/lOt0.A/hOaOAA/yQLSAA/ndFolB/TM

------------------------------------------------------------
--------~-> 

Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html 
Yahoo! Groups - Join or create groups, clubs, forums
&amp; communities. Links

<*> To visit your group on the web, go to:
    http://g
roups.yahoo.com/group/perl-beginner/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http
://groups.yahoo.com/group/perl-beginner/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:perl-beginner-digest@yahoogroups.com 
    mailto:perl-beginner-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    perl-beginner-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups - Join or create groups,
clubs, forums &amp; communities. is subject to:
    http://docs.yahoo.c
om/info/terms/
 

Re: Can't get my command line option via GetOpt::Long
country flaguser name
United States
2007-02-21 14:45:48
8:32pm, Eric Roellig wrote:

> $ cat colors.pl
> #!/usr/bin/perl  -W
>
Don't forget 'use strict;'

> {
> # import module
> #include <getopt_long.h>;
> use Getopt::Long;
>
> my $result=0;
> my $red = '';
> my $blue = '';
> my $green = '';
>
> print (" Argument is: $ARGV[0]n");
>
>
> # read options
> $result = GetOptions (  "red!" => $red,
>                        "blue" => $blue,
>                        "green" => $green
);
>

You're pretty close. But you need to pass a reference to the
variable, not 
the content of the variable:

"blue" => $blue,


Paul


Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://g
roups.yahoo.com/group/perl-beginner/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http
://groups.yahoo.com/group/perl-beginner/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:perl-beginner-digest@yahoogroups.com 
    mailto:perl-beginner-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    perl-beginner-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )