Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).
like image 579
Zaid Avatar asked Mar 02 '11 07:03

Zaid


People also ask

What is $# ARGV in Perl?

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

Which variable contains the command line arguments passed to a Perl script?

Perl command line arguments stored in the special array called @ARGV . The array @ARGV contains the command-line arguments intended for the script.

What is the use of and options in Perl?

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.


1 Answers

$ 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
like image 65
zoul Avatar answered Oct 13 '22 00:10

zoul