Perl call to GetOptions twice not working properly

I am trying to find a link to Perl GetOptions that explains this behavior.

If I call GetOptions twice, then the second time I call it, it cannot parse the command line options, and they all return undefined. Was there a first call to GetOptions (which, by the way, failed and returned 0), used command-line options, or did the second call just decide not to disturb the parsing, because he remembered that he had previously failed?

Do not ask why I call GetOptions twice - this is because the code will be difficult to restructure, and I would prefer not to do this if necessary. I just want an easy way, before the “real” call to GetOptions, to check for a single command line parameter. Thank.

+3
source share
3 answers

GetOptionsconsumes and modifies an array @ARGV. After calling this function, all that is usually left in this array are the parameters of the file name.

If you do not keep a copy of the array so that you can later reset, then subsequent calls GetOptionswill not have anything that could be analyzed. You can try calling GetOptionsFromArraywith an arbitrary array instead of using an implicit one @ARGV.

+8

GetOptions @ARGV, , , @ARGV. , , .

, , : --section1-opt1 --section1-opt2 -- --section2-opt1 --section1-opt2 -- <real arguments>. --, --. , .

+5

As already indicated , @ARGVmodified GetOptions. Although you cannot notice, you can declare @ARGVlocal:

{
local(@ARGV) = @ARGV;
GetOptions(...);
}

# @ARGV "restored" here    
GetOptions(...);
+3
source

Source: https://habr.com/ru/post/1786469/


All Articles