in reply to Problem with GetOpts::Long

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.

Replies are listed 'Best First'.
Re: Re: Problem with GetOpts::Long
by niku (Initiate) on Mar 21, 2003 at 23:06 UTC
    ./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!
        I've been looking at this for about 8 hours, and that was what I had narrowed it down to; I hope it's not too much to ask, but if anyone wouldn't mind giving this a one over, the whole script is below.

        #!/usr/bin/perl -w -s 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 ; GetOptions ("user=s" => \$user, "server=s" => \@servers, "command=s" = +> \@commands, "file=s" =>\$file, "dest=s"=> \$dest, "config=s" => \$c +onfig, "help=s" => \$help); print "config: \"$config\"\n"; =pod =============================== + Functions Below: + + + =============================== =cut sub get_config_file_data { # The Config::Auto::parse module returns a reference to a hash wic +h contains references to arrays # (I think) # In order to make life simpler, I'm going to put them into the re +gular arrays, @server, @commands, @files, etc. my $config_return = Config::Auto::parse(); # goes and gets stuf +f from config file, puts it into @{ %config_return } $using_config="TRUE"; # We are using the config if (! @user && @{ $config_return->{user} }){ # move the + field from the referenced array in the hash to a @user = @{$config_return->{user} } ; # regular +array. } if (! @servers && @{$config_return->{server} }){ # ditto @servers = @{$config_return->{server} } ; } if (! @commands && @{$config_return->{command} }){ # ditto @commands = @{$config_return->{command} } ; } if (! @files && @{$config_return->{file} }){ # ditt +o @files = @{$config_return->{file} } ; } } sub run_command(){ chomp($command); system("ssh ${user}\@${server} $command") ; #or print "\nCould not + execute ssh command\n"; } sub run_copy(){ chomp($files[1]); #print "\n----hey i'm in run_copy!-----\n"; #print "\nscp $files[0] ${user}\@${server}:$files[1]\n"; system("scp $files[0] ${user}\@${server}:$files[1]"); } =pod =============================== + MAIN PROGRAM BEGINS: + + + =============================== =cut #print $help; if ($help) { =pod Print's a help message - there was a built in way to do this but I didn't know at the time. =cut print "\n\n STREW HELP MESSAGE:\n \tOPTIONS:\n \t\t--server foo.bar.com *OR* --server =\"foo.bar.com\"\n \t\t--command=\"/etc/init.d/someinit restart\" *OR* command \"/etc +/init.d/someinit restart\"\tUSE QUOTES!\n \t\t--file /home/user/somefile --dest /etc/ *OR* --file=\"/home/u +ser/somefile\" --dest=\"/etc/\"\n \t\t--help : brings you here." ; exit; } =pod This is weird.... if $config has a size > 0 , then run get_config_file_data subroutine However, even if I do not use the --config flag on the command line, i +t runs anyway!! =cut if ($config){get_config_file_data}; if ($config){print "\nCONFIG: \"$config\"\n"}; # We are more than probably going to get servers from --server="foo.do +cmagic.com bar,docmagic.com bah.docmagic.com" # The below takes in the list, and splits on whitespace. # for $server (@servers) { if ($server =~ /\s/){ @server_list=split(/\s/, $server) ; } } if ($using_config && @commands) { $command = join(' ', @commands); #print "$real_command \n"; } if (@servers){ for $server (@servers){ #print "COMMAND: ". $command . "\n"; print "Server: $server \n"; if (@commands){run_command} ; if (@files){run_copy}; } } if (@server_list){ for $server (@server_list){ run_command(); run_copy(); } }

        thanks a lot for the help, btw. I'm pretty new to perl and how quickly you all got back to me with advice was amazing. I appreciate it a lot.

        thanks again,
        Nick

      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.