niku has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a script to act as a ssh wrapper and I want to have to option of using a config file when offered the command line argument of "--config". According ot the perldoc documentation, using "config" => /$config should set $config to 0, initializing it. Unfortunately, it seems to not get the value assigned to it.

this is the output of the program

./strew.pl --help Use of uninitialized value in concatenation (.) or string at ./strew.p +l line 65. config: ""

Below is code from where I'm dealing w/ command line args.

use Getopt::Long; use Config::Auto; #use Data::Dumper; my @servers = (); my @commands = (); my @files = (); my $user = $ENV{USER}; my $file = (); my $dest = (); my $help = (); my $config = () ; GetOptions ("user=s" => \$user, "server=s" => \@servers, "command=s" = +> \@commands, "file=s" =>\$file, "dest=s"=> \$dest, "config" => \$con +fig, "help" => \$help); print "config: \"$config\"\n";

Thanks!!,
Nick

Replies are listed 'Best First'.
Re: Problem with GetOpts::Long
by Mr. Muskrat (Canon) on Mar 21, 2003 at 23:00 UTC

    Instead of using my $config = (); try my $config = 0;. It makes a big difference.

    use Getopt::Long; use Config::Auto; #use Data::Dumper; my @servers = (); my @commands = (); my @files = (); my $user = $ENV{USER}; my $file = (); my $dest = (); my $help = (); my $config = 0; # 0 not () GetOptions ("user=s" => \$user, "server=s" => \@servers, "command=s" = +> \@commands, "file=s" =>\$fil +e, "dest=s"=> \$dest, "config" => \$config, "help" => \$help); print "config: \"$config\"\n";

    Now if you run it with the --config option, it's set to 1 otherwise it's 0.

      ./strew.pl --config config: "0"

      -----
      I went ahead and did what you reccomended, changing it from = () to = 0;
      It seems to be keeping whatever I set it to, instead of changing it's value.

        Something else is wrong then, I copied the code you have here, changed the default assignment, and it works for me.

        [jason@traveller jason]$ ./test.pl config: "0" [jason@traveller jason]$ ./test.pl --config config: "1"

        We're not surrounded, we're in a target-rich environment!
        Right. Bug. The part (above) that says
        "config" => \$config,
        should actually be
        "config!" => \$config,
        to indicate that the --config option is to be a boolean flag type of value.

        jdporter
        The 6th Rule of Perl Club is -- There is no Rule #6.

Re: Problem with GetOpts::Long
by jasonk (Parson) on Mar 21, 2003 at 23:03 UTC

    Actually the documentation says that arguments which are not specified on the command line are ignored, so if you want it to have a default you need to set that default yourself before calling GetOptions. Simply replacing my $config = (); with my $config = 0; will make it work the way you want.


    We're not surrounded, we're in a target-rich environment!