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

Hi Monks of Wisdom, subject explains. I have the following code:
#!/usr/bin/perl -w $dir = "some_dir"; @file_list = glob ("$dir/*"); GetOptions ( "f=s" => \@file_list, ); foreach (@file_list) { print { $_\n"; }
I want to get following behaviour: If I don't give -f option, script must assume whole list in the directory must be taken to the @file_list. If I give only the number of -f arguments, it must overwrite the default. However, it doesn't. Getopt::Long doesn't overwrite but appends the -f args. For the case:
#!/usr/bin/perl -w $dir = "some_dir"; $file = "$dir/some_file"); GetOptions ( "f=s" => $file, );
... it does overwrite, rather than append. Why is the behaviour for arrays different? Is there a way to switch off append behaviour for arrays? Perl version is 5.8.0. Regards!

Replies are listed 'Best First'.
Re: Getopt::Long, default of multiple options can not be overwritten by argument
by Corion (Patriarch) on Oct 28, 2011 at 18:08 UTC

    Why not change the order? First process all -f options, and if @file_list is still empty, fill it from glob?

      Hi Corion, thanks for the tip. I think your solution is the most practical way to solve this, rather than looking at an option to configure Getopt::Long. I have tried to find a way with Getopt::Long configuration on web, but couldn't find one. Your tip has solved my problem. Thank you very much!