Zaid
Zaid

Reputation: 37136

How can I get Perl to accept negative numbers as command-line arguments?

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).

Upvotes: 6

Views: 1978

Answers (1)

zoul
zoul

Reputation: 104065

$ 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

Upvotes: 14

Related Questions