Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force Perl to stop special processing of command line arguments if -e eval switch is used

perl -e 'print(123, @ARGV);' a b
# 123ab

perl -e 'print(123, @ARGV);' --help
# prints Perl's help instead

This is a toy example demonstrating the problem. In my real use-case I'm using -e to execute a large script from an embedded interpreter using perl_parse(...) function, the script has its own processing of --help switch, so I'd like to block any special processing of command line arguments after -e.

Is it possible?

like image 927
Vadim Kantorov Avatar asked Oct 08 '21 14:10

Vadim Kantorov


People also ask

What is Getopt :: Long in Perl?

Getopt::Long is the Perl5 successor of newgetopt.pl . This was the first Perl module that provided support for handling the new style of command line options, in particular long option names, hence the Perl5 name Getopt::Long. This module also supports single-character options and bundling.

What does $@ meaning in Perl?

The variables are shown ordered by the "distance" between the subsystem which reported the error and the Perl process...$@ is set if the string to be eval-ed did not compile (this may happen if open or close were imported with bad prototypes), or if Perl code executed during evaluation die()d.

What is $# ARGV in Perl?

The variable $#ARGV is the subscript of the last element of the @ARGV array, and because the array is zero-based, the number of arguments given on the command line is $#ARGV + 1.

What does $$ mean in Perl?

Yes $$ returns the process id of currently running script.

How to send command line arguments to a Perl program?

Command line arguments are sent to a Perl program in the same way as in any other language. The @ARGV array holds the command line argument. There is no need to use variables even if you use "use strict". By default, this variable always exists and values from the command line are automatically placed inside this variable.

What is the -E command in Perl?

The first one, -e, allows you to define Perl code to be executed by the compiler. For example, it’s not necessary to write a “Hello World” program in Perl when you can just type this at the command line. You can have as many -e options as you like and they will be run in the order that they appear on the command line.

What is the use of Eval in Perl?

The Perl eval function is used in various ways and it has the set of arguments with parameters which is required for validated the strings in Perl scripts. Eval function is used mainly in the loop section because the input values are to be validated and iterated in line by line of the scripts.

How to use $0 in Perl script?

In case you arrive from the world of Unix/Linux Shell programming you will recognize $0 is being the name of the script there too. In shell however $1, $2, etc. hold the rest of the command line parameters. These variables are used by the regular expressions of Perl. The command line arguments are in @ARGV. Similar to $* in the Unix/Linux shell.


1 Answers

Use a double hyphen to stop argument processing:

$ perl -e'print "[@ARGV]\n"' -- --help
[--help]
$
like image 99
Dave Mitchell Avatar answered Oct 28 '22 15:10

Dave Mitchell