Is there a way to get Perl to avoid treating negative values as command-line switches? Neither stringifying nor backslashing the argument seems to help under Linux:
$ perl -e 'print "@ARGV\n";' 4 5
4 5
$ perl -e 'print "@ARGV\n";' -4 5
Unrecognized switch: -4 (-h will show valid options).
$ perl -e 'print "@ARGV\n";' "-4" 5
Unrecognized switch: -4 (-h will show valid options).
$ perl -e 'print "@ARGV\n";' '-4' 5
Unrecognized switch: -4 (-h will show valid options).
$ perl -e 'print "@ARGV\n";' \-4 5
Unrecognized switch: -4 (-h will show valid options).
@ARGV. The array ARGV contains the command line arguments intended for the script. Note that $#ARGV is the generally number of arguments minus one, since $ARGV[0] is the first argument, NOT the command name. See $0 for the command name.
Perl command line arguments stored in the special array called @ARGV . The array @ARGV contains the command-line arguments intended for the script.
Uses descriptions from option-descriptions to retrieve and process the command-line options with which your Perl program was invoked. The options are taken from @ARGV . After GetOptions has processed the options, @ARGV contains only command-line arguments that were not options.
$ perl -E "say join ', ', @ARGV" -- -1 2 3
-1, 2, 3
The trick is using the double-hyphen (--
) to end the option parsing. Double-hyphen is a GNU convention:
$ touch -a
usage: touch [-acfm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...
$ touch -- -a
$ ls
-a
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With